我正在尝试registerReceiver
针对 BluetoothDevice 事件,例如ACTION_ACL_CONNECTED
, ACTION_ACL_DISCONNECTED
。
但是我使用的类既不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
是无法被调用