Goal: 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.
What I've done:
- Pored over documentation.
- Implemented a local instance of the host device's BT adapter.
- Implemented a notification to enable BT if it isn't enabled.
- Registered Broadcast Receivers and Intents to parse the
ACTION_FOUNDs
coming off of startDiscovery(). - Registered BLUETOOTH and BLUETOOTH_ADMIN permissions in the manifest.
Things work (as tested with incremental console logging) up until startDiscovery()
.
Frustration:
- startDiscovery() -- I suspect I am passing this in the wrong context. What context does this method need to be placed within to function properly?
If you have been able to get this method working, I would very much appreciate your wisdom.
UPDATE - 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 cat.log
errors or other errors, it simply doesn't give any output.
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);
}
}
}
}