public class MainActivity extends AppCompatActivity{ public static String ACTION_BROADCAST="com.eranewgames.bluetoothdoor.broadcast"; BroadcastReceiver broadcastReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); broadcastReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.e("MainActivity=onReceive", intent.getStringExtra("text")); } }; IntentFilter intentFilter=new IntentFilter(MainActivity.ACTION_BROADCAST); registerReceiver(broadcastReceiver,intentFilter); startService(new Intent(MainActivity.this,ServiceBluetoothDoor.class)); } @Override protected void onDestroy() { super.onDestroy(); stopService(new Intent(MainActivity.this,ServiceBluetoothDoor.class)); unregisterReceiver(broadcastReceiver); } } 

// Service

  public class ServiceBluetoothDoor extends Service { String macAddress = "70:72:0D:44:E6:1B"; // SPP UUID сервиса private UUID MY_UUID = UUID.fromString("00001101-0000-6666-8000-00805F23534B"); BluetoothSocket bluetoothSocket; @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public void onCreate() { super.onCreate(); BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); BluetoothDevice bluetoothDevice=bluetoothAdapter.getRemoteDevice(macAddress); bluetoothAdapter.cancelDiscovery(); try{ bluetoothSocket=bluetoothDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID); bluetoothSocket.connect(); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(bluetoothSocket.getInputStream())); String line; while ( (line=bufferedReader.readLine()) !=null ){ Log.e("ServiceBluetoothDoor=onCreate", line); // Intent intent=new Intent(MainActivity.ACTION_BROADCAST); // intent.putExtra("text",dataInputStream.readUTF()); // sendBroadcast(intent); } bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onDestroy() { super.onDestroy(); try { bluetoothSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } 

MainActivity hangs, but why is it not connected to the service?

Closed due to the fact that the essence of the question is not clear to the participants temq , fori1ton , user194374, rjhdby , Kromster 11 Feb '17 at 20:14 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    Service fulfills in a UI flow. Do the work inside the service in a new thread, or use the IntentService

    • Isn't Service a separate background thread? something did not understand you. - Andro
    • @xTIGRx, no, according to the documentation, the service works mainly on the UI stream - YuriiSPb
    • Can IntentService interact with an Activity via BroadCast so that the Activity does not hang? This is necessary to get data from the service - Andro
    • @xTIGRx, with the IntentService the problem is that it only suits for single operations - it will complete the task and die. Better use your service and just carry out all the work in a separate thread. IMHO is better to do through RxJava - YuriySPb
    • one
      I agree with @YuriySPb that it is better to work through using Rx. - pavel163