I'm developing an Android app. At the start this app show to the user a list of bluetooth devices and when he chooses one of they opens an other activity.
My goal is : after some day when the user will reopen the app the app will have to remember the ble device and it have to try to reconnect to it.
For now I have obtained my goal in this way :
There is a best way to obtain the same thing ?
Off topic : The function onScanResult in the ScanResult some time return to me null device, is it normal ? I have uses startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) and after lollipop I use the new version of the startLeScan with Scanfilter. Can it be the problem ?
Once your app has permission to use Bluetooth, your app needs to access the BluetoothAdapter and determine if Bluetooth is available on the device. If Bluetooth is available, the device will scan for nearby BLE devices.
In contrast to Classic Bluetooth, Bluetooth Low Energy (BLE) is designed to provide significantly lower power consumption. This allows Android apps to communicate with BLE devices that have low power requirements, such as proximity sensors, heart rate monitors, fitness devices, and so on.
For privacy reasongs the mac address keeps on changing-refer below snippet from the ble specs:
5.4.5 Privacy Feature
Bluetooth LE supports a feature that reduces the ability to track a LE device over a period of time by changing the Bluetooth device address on a frequent basis. The privacy feature is not used in the GAP discovery mode and procedures but it is used, when supported, during connection mode and connection procedures. In order for a device using the privacy feature to reconnect to known devices, the device address, referred to as the private address, must be resolvable by the other device.
The private address is generated using the device’s resolving identity key (IRK) exchanged during the bonding procedure.
So the ideal way is to use the IRK to gerate the actual address of the device.However I didnt find any API to do it.
Currently,the work around- which I am using in mny app- to re-connect to the device,
Repeat step 2 for all the devices.
This work around will only work if the device exposes serial number through some service.
You can also try setting the autoconnect flag to true when you call connectGatt (Context context, boolean autoConnect, BluetoothGattCallback callback)
From the docs:
public BluetoothGatt connectGatt (Context context, boolean autoConnect, BluetoothGattCallback callback) Added in API level 18
Connect to GATT Server hosted by this device. Caller acts as GATT client. The callback is used to deliver results to Caller, such as connection status as well as any further GATT client operations. The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct GATT client operations. Parameters
autoConnect Whether to directly connect to the remote device (false) or to automatically connect as soon as the remote device becomes available (true).
callback GATT callback handler that will receive asynchronous callbacks. Throws IllegalArgumentException if callback is null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With