我正试图为BluetoothDevice事件(如ACTION_ACL_CONNECTED,ACTION_ACL_DISCONNECTED)注册接收器registerReceiver
。
但由于我使用registerReceiver
的类既不是Activity
类也不是Service
类,因此我将Context
传递给该类,并按照以下方式注册接收器:
context.registerReceiver(ActionFoundReceiver,
new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
context.registerReceiver(ActionFoundReceiver,
new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
context.registerReceiver(ActionFoundReceiver,
new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED));
并按照以下方式处理事件
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// do some stuff
}
}
};
但是当设备连接或断开时,我无法跟踪它,我的意思是 ActionFoundReceiver
无法被调用。