3

Problem: I am being notified when a bluetooth device is pairing, or becomes paired. However, I do not know how to get notified when a device is being unpaired. How could I get my app to recognize when it has become unpaired with my device?

Example: Let's say that I pair my surface tablet and my android phone through code. My broadcast receiver will register the ACTION_BOND_STATE_CHANGED intent and allow me to process it however I want. Now, if I want to unpair my surface tablet, I do not get notified on my phone that the device has become unpaired. If I check, device.getBondState() for the device after it has been unpaired, it equals BOND_BONDED (which it clearly isnt) I can get it to recognize it is unpaired again after I try to connect to the unpaired device, then the app crashes and then when I turn the app on again, only then does it recognize it as unpaired.

Scenario: I have a BroadcastReceiver registered to listen for intents caused by a 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 process 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();
            }
        }
    }
}
};
Jurko Guba
  • 630
  • 7
  • 17

0 Answers0