Android BluetoothDevice notify of deleted instant messaging states
Problem: I get a notification when a Bluetooth device connects or becomes paired. However, I don't know how to get notifications when the device is unpaired. How can I know how my application recognizes when it is unpaired with my device?
Example: Suppose I connect my tablet and my Android phone via code. My broadcast receiver will register the intent ACTION_BOND_STATE_CHANGED
and let me handle it, but I want to. Now if I want to turn off my tablet, I don't get a notification on my phone that the device has become unpaired. If I checked, device.getBondState()
for the device after it was unpaired, it is BOND_BONDED
(which clearly does not match) I can get it to recognize that it is unpaste again after trying to connect to the unpaired device, then the app crashes and then when I turn it back on application, only then will it recognize it as unpaired.
Script: I have BroadcastReceiver
registered to listen on intents called by 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));
I handle these actions with this 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();
}
}
}
}
};
source to share
No one has answered this question yet
Check out similar questions: