title: Bluetooth discovery inside a AsyncTask
body: <p>I am trying to search for nearby bluetooth devices, to accomplish this task am using an <code>AsyncTask</code> The code for my AsyncTask is underneath</p>
  
  <pre><code>public class DeviceScan extends AsyncTask&lt;String, Void, ArrayList&lt;String&gt;&gt; {
  
  
      ProgressDialog dialog;
      Context _context; 
      BluetoothAdapter adapter; 
      final BroadcastReceiver blueToothReceiver;
  
  
      /**
       * The constructor of this class
       * @param context The context of whatever activity that calls this AsyncTask. For instance MyClass.this or getApplicationContext()
       */
  
      public DeviceScan(Context context) {
  
          _context = context; 
          dialog = new ProgressDialog(_context); 
          adapter = BluetoothAdapter.getDefaultAdapter();
          blueToothReceiver = null; 
  
      }
  
  
      protected void onPreExecute() {
  
            dialog.setTitle("Please Wait");
            dialog.setMessage("Searching for devices..");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.show();
  
      }
  
      /*
       * Operations "behind the scene", do not interfere with the UI here!
       * (non-Javadoc)
       * @see android.os.AsyncTask#doInBackground(Params[])
       */
      @Override
      protected ArrayList&lt;String&gt; doInBackground(String... params) {
  
          //This arraylist should contain all the discovered devices. 
          final ArrayList&lt;String&gt; devices = new ArrayList&lt;String&gt;();
  
          // Create a BroadcastReceiver for ACTION_FOUND
          final BroadcastReceiver blueToothReceiver = new BroadcastReceiver() {
              public void onReceive(Context context, Intent intent) {
                  String action = intent.getAction();
                  // When discovery finds a device
                  if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                      // Get the BluetoothDevice object from the Intent
                      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                      // Add the name and address to an array adapter to show in a ListView
                      devices.add(device.getName() + "\n" + device.getAddress());
                  }
              }
          };
          // Register the BroadcastReceiver
          IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
          _context.registerReceiver(blueToothReceiver, filter);
  
          return devices;
  
      }
  
  
      /*
       * After the background thread has finished, this class will be called automatically 
       * (non-Javadoc)
       * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
       */
       protected void onPostExecute(ArrayList&lt;String&gt; result) {
           dialog.dismiss(); 
           Toast.makeText(_context, "Finished with the discovery!", Toast.LENGTH_LONG).show(); 
  
  
       }
  
       protected void onDestory() {
            _context.unregisterReceiver(blueToothReceiver);
       }
  }
  </code></pre>
  
  <p>As you can see the <code>doInBackground</code> function returns an Arraylist of all the devices. My problem is that this list doesn't contain any objects at all. I'am sure that I have enabled devices which are <code>Discoverable</code>  Any tips will be appreciated</p>
  
  <p>Thanks in advance! </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Underneath is my Manifest file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.com.com"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk android:minSdkVersion="11" /&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/&gt;
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name" &gt;
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>EDIT #2</strong></p>
  
  <p>I tried to add a dummy object into the list, instead of following line of code</p>
  
  <pre><code>devices.add(device.getName().toString() + "\n" + device.getAddress().toString());
  </code></pre>
  
  <p>I tried</p>
  
  <pre><code>devices.add("Test");
  </code></pre>
  
  <p>But when I debug, the arraylist is still empty? </p>