title: Android Service's method onDestroy called before Bluetooth scanning ends
body: <p>I have a problem that I start a service with an Intent, in <code>onCreate()</code> method register <code>BroadcastReceiver</code> and then in <code>onDestroy()</code> unregister the <code>BroadcastReceiver</code>. In Method <code>onHandleIntent()</code> I start discovery for bluetooth devices. THe problem is, that I my <code>BroadcastReceiver</code> is destroyed before any device is found so I cant really do anything with the <code>BluetoothDevice.ACTION_FOUND</code> intent. I tried to move <code>startDiscovery()</code> to <code>onCreate()</code> but no results. My code looks like this:</p>
  
  <pre><code>public class BluetoothService extends IntentService{
  
  private BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
  private final String SMART_TOKEN_ADDRESS = "F0:E7:7E:5F:63:70";
  private final BroadcastReceiver receiver = new BroadcastReceiver(){
  
      @Override
      public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          Log.v("BLUETOOTH","found it!");
      }
  
  };
  
  public BluetoothService() {
      super("BluetoothService");
      // TODO Auto-generated constructor stub
  }
  
  @Override
  public void onCreate(){
      super.onCreate();
      Log.v("SERVICE", "Just got created");
      IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
      registerReceiver(receiver, filter);
      btAdapter.startDiscovery();
  }
  
  @Override
  protected void onHandleIntent(Intent intent) {
      // TODO Auto-generated method stub  
      Log.v("BLUETOOTH", "Discovery just started");
      //btAdapter.startDiscovery();
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      Log.v("BLUETOOTH","Just got destroyed!");
      unregisterReceiver(receiver);        
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>Log looks like this:
    1. Just got created
    2. Discovery just started
    3. Just got destroyed!
    4. Received android.bluetooth.device.action.FOUND</p>
  
  <p>Thanks for help!
  T.</p>