Android 4.4: Low Energy Bluetooth; Connect without scan for BLE device

My app connects to a Bluetooth LE device. Typically, you scan your device using mBluetoothAdapter.startLeScan(mLeScanCallback); . The callback provides information about available devices.

If you want to connect to a dedicated device, you are doing something like

 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 

and then

 mBluetoothGatt = device.connectGatt(this, false, mGattCallback); 

It seems to me that the only thing you need to connect to a BLE device is to know the BLE address, and then connect to it using the two above steps. Therefore, if I already know the BLE address (for example, it is written on the label of the BLE device), I do not need to perform a BLE check.

But what I came across is that if I have a BLE device that I have never found through a BLE scan before, it is not possible to connect directly to it using your BLE address. I have to find it through scanning at least once with my Android phone. Subsequently, I no longer need verification, and I can connect to the BLE device simply by using its BLE address.

Is this supposed to be the case or am I watching something?

Thanks a lot Stefan

+7
source share
2 answers

The device address is just a unique identifier for the Bluetooth device, it does not contain information for connecting. Scanning is required to retrieve information in the broadcast signal from the Bluetooth device so that a connection can be established. After the scan is completed, information is stored somewhere on the device and is tied to the device address.

I think that if you try to get the value for the bluetooth address, it will return null until it is checked once.

+4
source

The answer from Hoa Do is not entirely correct.

Due to some terrible design flaws in the Android BLE API, there is no way to tell if a given address is public or random. (You can learn more about the different types of addresses at https://devzone.nordicsemi.com/question/43670/how-to-distinguish-between-random-and-public-gap-addresses/ ). The getRemoteDevice method must accept the optional parameter "arbitrary address / public address", but this is not so. Without the correct address type, the Bluetooth controller cannot connect to the device.

There is some internal heuristic in the Android BLE stack that allows you to β€œguess” whether the address is public or random, but, unfortunately, it differs in different versions of Android, as well as when using autoConnect = true or false. However, if you linked the device ( https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createBond () ), then it will store in its internal database whether this address is public or random. If the peripheral device you want to connect to uses an arbitrarily resolved address, it can handle this if you use binding. Therefore, I highly recommend using bonding.

If you do not use pairing, you will need to scan before connecting to the device, because when you start scanning and discover the device, the Android BLE stack temporarily (until the next Bluetooth restart) remembers the type of address for the address. If you do not scan the device before connecting, it’s all will try to connect equally, but there is a chance that it is trying to connect with the wrong address type and, therefore, fails.

0
source

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


All Articles