This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Nrf Connect App and Autoconnect

Hello,

I'm trying to autoconnect my device to my phone using nrf Connect app. I connect to my board selecting autoconnect flag, then insert my passkey and devices are correctly bonded and connected. 

In the case that it is my board to reboot, the phone correctly autoconnect when it scans the board again. But if i turn off the bluetooth on my phone and then on (or when i turn off my phone), nothing happens.

Also when i manually disconnect the board, i would expect the phone to autoreconnect.

Here, https://stackoverflow.com/questions/48836902/hm-10-ble-module-automatic-reconnect it's written that autoconnect wont work if bluetooth is turned off and on, is it correct?

Thank you

Parents
  • Hello ,

    You would have to implement this feature on your own. For bonded devices it's not that difficult, as you don't need to scan it before connecting after a reboot. The device address type (private/public) is saved for bonded devices. For non-bonded devices scanning and finding the device is required before you initiate connection.

    As you are using a bonded device, you have to do the following:

    1. When user opens your app for the first time, scan for your device and with autoConnect=false. This is much faster way of connecting.

    2. Create Bond to your device. This will add the device address and type (and keys) to the security table in Android system.

    3. When the connection drops, reconnect using gatt.connect(); instead of closing the BluetootGatt object and creating a new one with device.connectGatt(...). The connect() method switches to autoConnect=true. *

    4. Register a BroadcastReceiver in AndroidManifest to receive to this: https://developer.android.com/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED broadcast. When you receive this broadcast, obtain your device from getBondedDevices() (or by address) and call device.connectGatt(..., autoConnect = true, ...). This will make sure that your device will connect when it's in range. Not immediately, as this is a low-power feature and may take some time after you get into device's range before it's picked up.

    5. Do not call gatt.close(), as this stops autoconnection. If Bluetooth goes OFF do not try to connect (this is actually possible, LE is still enabled on recent phones). But that makes user angry. When Bluetooth goes off call gatt.close() and call connectGatt(autoConnect=true) again when Bluetooth is ON again.

    * We recommend using BLE Library (https://github.com/NordicSemiconductor/Android-BLE-Library/) as it handles some of those automatically.,

    Edit:

    Additionally, for Android Oreo+, if you got disconnected, you may initiate start scanning with IntentFilter (https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html#startScan(java.util.List%3Candroid.bluetooth.le.ScanFilter%3E,%20android.bluetooth.le.ScanSettings,%20android.app.PendingIntent)) which will work even if the app gets killed. When the device gets scanned again, a the intent will be triggered and you may start your app/connect to this device with autoConnect=false. Android Q introduces some restrictions to how an app may be started without user explicit action, but you can then just show a notification that your device is around. Clicking a notification will work.

Reply
  • Hello ,

    You would have to implement this feature on your own. For bonded devices it's not that difficult, as you don't need to scan it before connecting after a reboot. The device address type (private/public) is saved for bonded devices. For non-bonded devices scanning and finding the device is required before you initiate connection.

    As you are using a bonded device, you have to do the following:

    1. When user opens your app for the first time, scan for your device and with autoConnect=false. This is much faster way of connecting.

    2. Create Bond to your device. This will add the device address and type (and keys) to the security table in Android system.

    3. When the connection drops, reconnect using gatt.connect(); instead of closing the BluetootGatt object and creating a new one with device.connectGatt(...). The connect() method switches to autoConnect=true. *

    4. Register a BroadcastReceiver in AndroidManifest to receive to this: https://developer.android.com/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED broadcast. When you receive this broadcast, obtain your device from getBondedDevices() (or by address) and call device.connectGatt(..., autoConnect = true, ...). This will make sure that your device will connect when it's in range. Not immediately, as this is a low-power feature and may take some time after you get into device's range before it's picked up.

    5. Do not call gatt.close(), as this stops autoconnection. If Bluetooth goes OFF do not try to connect (this is actually possible, LE is still enabled on recent phones). But that makes user angry. When Bluetooth goes off call gatt.close() and call connectGatt(autoConnect=true) again when Bluetooth is ON again.

    * We recommend using BLE Library (https://github.com/NordicSemiconductor/Android-BLE-Library/) as it handles some of those automatically.,

    Edit:

    Additionally, for Android Oreo+, if you got disconnected, you may initiate start scanning with IntentFilter (https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html#startScan(java.util.List%3Candroid.bluetooth.le.ScanFilter%3E,%20android.bluetooth.le.ScanSettings,%20android.app.PendingIntent)) which will work even if the app gets killed. When the device gets scanned again, a the intent will be triggered and you may start your app/connect to this device with autoConnect=false. Android Q introduces some restrictions to how an app may be started without user explicit action, but you can then just show a notification that your device is around. Clicking a notification will work.

Children
Related