Pairing with the low-power Android Bluetooth device

Can I automatically connect to devices with low energy Bluetooth (BLE)?

Android documentation indicates that [ BluetoothDevice.connectGatt() ] ( https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context , logical, android .bluetooth.BluetoothGattCallback)) has an autoConnect parameter:

boolean indicates whether to automatically connect to a BLE device as soon as it becomes available

However, in order to call this you first need a BluetoothDevice . AFAIK is the only way to get this by scanning available devices. Setting up a scan each time to connect to the device does not seem desirable. In addition, I tried using the nRF control panel to connect to my peripheral device using autoConnect = true , but this does not connect to the device. A connection without autoConnect , however, makes it plug-in, and I was able to successfully read and write data from my peripheral device.

A common Bluetooth method for connecting two devices. However, finding my BLE device and using BluetoothDevice.createBond() does not work. In my ACTION_BOND_STATE_CHANGED callback, EXTRA_BOND_STATE and EXTRA_PREVIOUS_BOND_STATE just go from BOND_BONDING to BOND_NONE and vice versa. I am not reading out a mistake or anything else - so maybe I missed something. Here's the callback:

 private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); Log.e(TAG, "prevState " + prevState + ", state " + state); } } }; 

Therefore, this type of connection does not work.

My question is: am I doing something wrong for pairing or autoConnect? Or how do I now work only with the correct rule? It seems that the real pain (and battery leak) is to check the devices every time, see if there is a device, if so, read the data and check tomorrow, otherwise check after an hour or so. The point of Bluetooth is that it should touch directly when it is close, right?

+5
source share
1 answer

It works without re-scanning. You do not need to mate at all. Just call BluetoothGatt.connect() again for the gatt object you received from the first connection. You will receive the onConnectionStateChange event in your BluetoothGattCallback as soon as the device is available again. If you use the auto-connect option, you don’t even need to call the BluetoothGatt.connect() method. Just check your cllback and remember to close BluetoothGatt with close() if you don't see any connection for too long.

And yes, in order to get the first connection, you must scan devices with BluetoothAdapter.startLeScan , not ordinary Bluetooth devices.

+5
source

All Articles