title: Android - BroadcastReceivers registered inside Service do not work
body: <p>I am developing an Android application, which uses Bluetooth to connect to a remote device (specifically HC-05 Bluetooth module). Currently I'm struggling with registering a few <code>BroadcastReceiver</code> inside a <code>Service</code> class properly.</p>
  
  <p>What I want to achieve is:</p>
  
  <ol>
  <li>In <code>MainActivity</code> there is a <code>Button</code> with <code>onClickListener</code>, which launches the <code>ConnectionService</code>.</li>
  <li>In <code>onStartCommand</code> method of the instantiated service I register the receivers previously created (as fields in the service class).</li>
  <li>When a Bluetooth device is found, I want to execute some code. The code should be included in <code>onReceive</code> method of the receiver, which is set to listen for <code>BluetoothDevice.ACTION_FOUND</code>.
  [I know, this is not something really complex...]</li>
  </ol>
  
  <p>BUT:
  When I register the receivers and start discovery, it seems that the receivers are not working. I mean, they do not receive actions.</p>
  
  <p><strong>My code looks like this:</strong></p>
  
  <p>In MainActivity:</p>
  
  <pre class="lang-java prettyprint-override"><code>connectButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Log.d(Constants.TAG,"CONNECT button clicked. Starting ConnectionService");
                  Intent startConnectionServiceIntent = new Intent(MainActivity.this, ConnectionService.class);
                  Context context = getApplicationContext();
                  context.startService(startConnectionServiceIntent);
  </code></pre>
  
  <p>In ConnectionService:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ConnectionService extends Service {
      private BluetoothAdapter BA;
      private ConnectionStarterThread connectionStarter;
      private ConnectThread connectThread;
      private ConnectedThread connectedThread;
      private BluetoothSocket socket;
      private boolean deviceFound;
  
      private final BroadcastReceiver actionFoundReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              // TODO Auto-generated method stub
              String action = intent.getAction();
              if(BluetoothDevice.ACTION_FOUND.equals(action)) {
                      [...]
                      deviceFound = true;
                      BA.cancelDiscovery();
                      [...]
                  }
                  else {
                      deviceFound = false;
                  }
              }
          }
      };
  
      private final BroadcastReceiver discoveryStartedReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              String action = intent.getAction();
              if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
  
              // do something
  
              }
          }
      };
  
      private final BroadcastReceiver discoveryFinishedReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              String action = intent.getAction();
              if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
  
                  // do something else
  
              }
          }
      };
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          BA = BluetoothAdapter.getDefaultAdapter();
          registerReceiver(actionFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
          registerReceiver(discoveryStartedReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
          registerReceiver(discoveryFinishedReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
          Log.d(Constants.TAG, "ConnectionService started");
  
          BA.startDiscovery();
  
          connectionStarter = new ConnectionStarterThread();
          connectionStarter.run();
  
          return super.onStartCommand(intent, flags, startId);
      }
  </code></pre>
  
  <p><strong>The question is: what am I doing wrong?</strong></p>
  
  <p>To specify - this code:</p>
  
  <pre class="lang-java prettyprint-override"><code>connectionStarter = new ConnectionStarterThread();
  connectionStarter.run();
  </code></pre>
  
  <p>is responsible for creating a <code>Thread</code> which repeatably checks if <code>deviceFound</code> variable has been set to <code>true</code> (should be handled by <code>actionFoundReceiver</code>) and if so, starts Bluetooth connection (this is done as suggested at Android Developers examples). Maybe the problem lies here?</p>
  
  <p><strong>What I already tried:</strong></p>
  
  <ol>
  <li>Googling the topic - people generally suggest to do it the same way as above.</li>
  <li>I have Bluetooth permissions in manifest file.</li>
  <li>Registered <code>BroadcastReceivers</code> as subclasses of <code>BroadcastReceiver</code> class - did not help.</li>
  </ol>
  
  <p>Could someone help me with this issue? Is there a simplier way to do it?</p>