【发布时间】:2012-08-08 15:58:16
【问题描述】:
我创建了一个辅助类,它扩展了 BroadcastReceiver 以侦听找到的 BluetoothDevice 和发现完成意图。我有两个活动通过传递一个处理程序来使用这个类。处理程序根据意图接收消息。我像这样实例化类和 registerReceiver:
来自 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();
来自 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 类:
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);
}
}
处理程序:
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;
}}
我在这里遗漏了什么吗?感谢您的帮助。
【问题讨论】:
-
Manifest 中有接收器类声明“DevicesHelper”吗?究竟是什么不工作?你能在接收器中打断点吗?
-
@mojorisinify。首先创建一个
IntentFilter并使用addAction添加任何额外的意图操作。 -
@Maxim 我没有在清单中声明接收者,它在代码中注册。 DevicesHelper 不是一项活动,因此我认为不必在清单中声明它。参考:link。 DevicesHelper 被实例化,注册但当蓝牙设备在范围内时它不会被调用。
-
@techiServices:是的,IntentFilters 注册得很好。向上看。
-
@mojorisinify。你误会了。您使用一个
IntentFilter和一个registerReceiver。 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(deviceHelper, filter);
标签: java android eclipse broadcastreceiver