Android bluetooth: list of paired devices

I have a bluetooth device with SPP profile and Bluetooth version 2.1.
I have an application that connects to this device and communicates with it. The device uses the Just Works pairing method.

I ran into a problem on some phones like Samsung Galaxy, Galaxy S.

The problem is that after the user exits the application, I close the sockets and disconnect from the device. After a successful disconnection, it is noted that the device record is deleted from the list of paired devices.

+6
source share
3 answers

I did not work with tablets, but I wrote an application using SPP for Android phones. I found that in order for Bluetooth to be stable, I need to manually pair it with the device I want to communicate with. We used the code below to initiate communication from the application, and it should maintain the connection as if you had manually mated in the settings menu.

Here is the general stream: 1) Register BroadcastReceiver to listen to BluetoothDevice.ACTION_BOND_STATE_CHANGED
2) After detecting the device, you should have a BluetoothDevice object.
3) Use reflection to call the createBond method on the BluetoothDeviceObject device
3a) Wait until the communication state changes before opening the connectors.

BluetoothDevice device = {obtained from device discovery}; Method m = device.getClass().getMethod("createBond", (Class[])null); m.invoke(device, (Object[])null); int bondState = device.getBondState(); if (bondState == BluetoothDevice.BOND_NONE || bondState == BluetoothDevice.BOND_BONDING) { waitingForBonding = true; // Class variable used later in the broadcast receiver // Also...I have the whole bluetooth session running on a thread. This was a key point for me. If the bond state is not BOND_BONDED, I wait here. Then see the snippets below synchronized(this) { wait(); } } 

4) Wait for the link state to change from BOND_BONDING to BOND_BONDED

Inside BroadcastReciever:

 public void onReceive(Context context, Intent intent) { if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) { int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1); int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1); if (waitingForBonding) { if (prevBondState == BluetoothDevice.BOND_BONDING) { // check for both BONDED and NONE here because in some error cases the bonding fails and we need to fail gracefully. if (bondState == BluetoothDevice.BOND_BONDED || bondState == BluetoothDevice.BOND_NONE) { // safely notify your thread to continue } } } } } 

5) Open the sockets and inform

You can also use the removeBond method via reflection to remove the device from the pairing list.

Hope this helps!

+13
source

If pairing is due to the connection of your application, I assume that some devices will consider it as temporary pairing and will not support the device in a pair list after disconnecting the connection. To save the device in a pair list, you must manually pair using the Bluetooth settings menu. After pairing, your program can connect / disconnect, and the device will save itself in a pair list.

0
source

I also had the same issue with the Sony Xperia X10. I managed to make him “remember” the pairing by changing the security level settings on the side of the Bluetooth device (just as I design the device).

I’m not sure about the explanation of “temporary pairing”, which would be manufacturer dependent, it doesn’t really matter that different phones will react differently to the connection with the same device.

However, for me this is a problem related to the unlimited part. Typically, the Bluetooth stack seems to fail when the user abandons the device when the application is connected in the background. I still have not figured out how to properly manage the ACTION_BOND_STATE_CHANGED event.

0
source

Source: https://habr.com/ru/post/900915/


All Articles