Android BluetoothDevice уведомляет удаленное состояние отмены соединения
Проблема: я получаю уведомление, когда устройство Bluetooth подключается или становится сопряженным. Однако я не знаю, как получить уведомление, когда устройство не подключено. Как я могу заставить свое приложение распознавать, когда оно перестало работать с моим устройством?
Пример: допустим, что я связываю свой планшетный компьютер с телефоном на базе Android с помощью кода. Мой приемник вещания зарегистрирует ACTION_BOND_STATE_CHANGED
намерение и позвольте мне обрабатывать его так, как я хочу. Теперь, если я хочу отключить планшет, я не получаю уведомления о том, что устройство перестало работать. Если я проверю, device.getBondState()
для устройства после того, как оно было непарным, оно равно BOND_BONDED
(что явно не так). Я могу заставить его распознать, что он снова непарен, после того, как я пытаюсь подключиться к непарному устройству, затем приложение падает, а затем, когда я снова включаю приложение, только тогда оно распознает его как непарное.
Сценарий: у меня есть BroadcastReceiver
зарегистрирован для прослушивания намерений, вызванных BluetoothDevice
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED));
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
Я обрабатываю эти Действия с этим BroadcastReciever
:
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("ACTION: "+ action);
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
list();
// Get the BluetoothDevice object from the intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println("FOUND: " + device.getName() + " STATE: " + device.getBondState());
mapInput(device);
// Add the name and the mac address of the object to the array adapter
BTSimpleAdapter.notifyDataSetChanged();
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println("UPDATE Name " + device.getName() + " Value " + device.getAddress() + " Bond state " + device.getBondState());
for (Map<String, String> entry : data) {
if (entry.get("Name").equals(device.getName()) && entry.get("Value").equals(device.getAddress())) {
if (device.getBondState() == BluetoothDevice.BOND_NONE) {
entry.put("Paired", "Unpaired");
} else if (device.getBondState() == BluetoothDevice.BOND_BONDING) {
entry.put("Paired", "Pairing");
} else {
entry.put("Paired", "Paired");
}
BTSimpleAdapter.notifyDataSetChanged();
}
}
}
}
};