Reputation: 405
I made a helper class that extends BroadcastReceiver to listen for BluetoothDevice found and discovery finish intents. I have two activities which use this class by passing a handler. The handler receives messages as per the intent. I instantiate the class and registerReceiver like this:
From mainActivity:
deviceHelper=new DevicesHelper(myHandler,DevicesHelper.REQUEST_DETECT_DEVICES_IN_RANGE);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(deviceHelper, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(deviceHelper, filter);
if(myBluetoothAdapter.isDiscovering())myBluetoothAdapter.cancelDiscovery();
myBluetoothAdapter.startDiscovery();
From ListActivity:
deviceHelper=new DevicesHelper(deviceListHandler,DevicesHelper.REQUEST_DEVICE_LIST_ACTIVITY);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(deviceHelper, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(deviceHelper, filter);
DeviceHelper class:
public class DevicesHelper extends BroadcastReceiver {
public final static int REQUEST_DEVICE_LIST_ACTIVITY=1;
public final static int REQUEST_DETECT_DEVICES_IN_RANGE=2;
Handler myHandler;
int requestCode;
public DevicesHelper(){
}
public DevicesHelper(Handler handler,int requestCode){
this.requestCode=requestCode;
myHandler=handler;
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
// When discovery finds a device
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
switch(requestCode){
case REQUEST_DEVICE_LIST_ACTIVITY:
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
//newDevicesCount++;
String[] deviceInfo={device.getName(),device.getAddress()};
myHandler.obtainMessage(DeviceListActivity.MESSAGE_NEW_DEVICE,deviceInfo);
};
break;
case REQUEST_DETECT_DEVICES_IN_RANGE:
String[] deviceInfo={device.getName(),device.getAddress()};
myHandler.obtainMessage(StripChartRecorder.MESSAGE_NEW_DEVICE,deviceInfo);
break;
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
myHandler.obtainMessage(StripChartRecorder.MESSAGE_DISCOVERY_FINISHED);
}
}
Handler:
private final Handler myHandler=new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case MESSAGE_NEW_DEVICE:
doOtherStuff();
case MESSAGE_DISCOVERY_FINISHED:
dostuff();
}
break;
}}
Am I missing something here? I appreciate any help.
Upvotes: 1
Views: 587
Reputation: 405
The problem was how I was handling the message received from the handler. Turned out Set<> was initially set to null as per eclipse's instruction, so it was never adding the devices received from the BroadcastReceiver helper class. I appreciate anyone who tried to help.
Upvotes: 0
Reputation: 8533
You need to sendMessage(Message msg)
where msg is the Message
you get with obtainMessage
.
Upvotes: 1