我的代码应该收听蓝牙耳机配对时发出的回调。
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver bReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
Toast.makeText(getBaseContext(), "paired", Toast.LENGTH_LONG).show();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Toast.makeText(getBaseContext(), "unpaired", Toast.LENGTH_LONG).show();
}
}
};
连接蓝牙设备时BluetoothDevice.ACTION_ACL_CONNECTED
会触发,但BluetoothDevice.ACTION_ACL_DISCONNECTED
从不显示任何祝酒词。
我猜这是因为我没有在onCreate
中正确分配过滤器/操作:
IntentFilter BluetoothFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
registerReceiver(bReceiver, BluetoothFilter);
如何在ACTION_ACL_DISCONNECTED
中添加oncreate
?
答案 0 :(得分:0)
<强> EDITED 强>
在BluetoothFilter.addAction(ACTION_ACL_DISCONNECTED)
之前添加registerReceiver
。