title: Bluetooth device discovery in Android -- startDiscovery()
body: <p><strong>Goal:</strong> Build an Android app that discovers the names and addresses of BT devices within range and submits their values to a webservice. BT devices have not been previously bonded to the host device, I just want to poll everything as I walk about.</p>
  
  <p>What I've done:</p>
  
  <ol>
  <li>Pored over documentation.</li>
  <li>Implemented a local instance of the host device's BT adapter.</li>
  <li>Implemented a notification to enable BT if it isn't enabled.</li>
  <li>Registered Broadcast Receivers and Intents to parse the <code>ACTION_FOUNDs</code> coming off of <strong>startDiscovery()</strong>.</li>
  <li>Registered <strong>BLUETOOTH</strong> and <strong>BLUETOOTH_ADMIN</strong> permissions in the manifest.</li>
  </ol>
  
  <p>Things work (as tested with incremental console logging) up until <code>startDiscovery()</code>.</p>
  
  <p><br>
  <strong>Frustration:</strong></p>
  
  <ul>
  <li><strong>startDiscovery()</strong> -- I suspect I am passing this in the wrong context. What context does this method need to be placed within to function properly?</li>
  </ul>
  
  <p>If you have been able to get this method working, I would very much appreciate your wisdom.</p>
  
  <p><strong>UPDATE</strong> - here's a stripped down simplified version of the code that is causing me grief; this simplification recapitulates my error. This code runs, it throws no <code>cat.log</code> errors or other errors, it simply doesn't give any output.</p>
  
  <pre><code>package aqu.bttest;
  
  import android.app.Activity;
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.os.Bundle;
  import android.widget.Toast;
  
  public class BT2Activity extends Activity {
  
  private BluetoothAdapter mBTA;
  private SingBroadcastReceiver mReceiver;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
    //register local BT adapter
      mBTA = BluetoothAdapter.getDefaultAdapter();
      //check to see if there is BT on the Android device at all
      if (mBTA == null){
          int duration = Toast.LENGTH_SHORT;
          Toast.makeText(this, "No Bluetooth on this handset", duration).show();
      }
      //let's make the user enable BT if it isn't already
      if (!mBTA.isEnabled()){
          Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
          startActivityForResult(enableBT, 0xDEADBEEF);
      }
      //cancel any prior BT device discovery
      if (mBTA.isDiscovering()){
          mBTA.cancelDiscovery();
      }
      //re-start discovery
      mBTA.startDiscovery();
  
      //let's make a broadcast receiver to register our things
      mReceiver = new SingBroadcastReceiver();
      IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
      this.registerReceiver(mReceiver, ifilter);
  }
  
  private class SingBroadcastReceiver extends BroadcastReceiver {
  
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction(); //may need to chain this to a recognizing function
          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 Toast
              String derp = device.getName() + " - " + device.getAddress();
              Toast.makeText(context, derp, Toast.LENGTH_LONG);
          }
      }
  }
  </code></pre>
  
  <p>}</p>