Nordic Android Ble won't reconect after I manually turn bluetooth of the device off

I have been writing an app and now changing the bluetooth library to Nordic Android BLE Library. W hile connecting to the device I keep the autoConnect flag on, which lets the device reconnect when it disconnects after going out of range and then coming back in range, however when I turn off bluetooth manually and when I turn it on it won't reconnect. What can I do to resolve this issue?

Parents
  • Hello,

    That is expected behavior. When you turn Bluetooth off, you effectively call close() on your BluetoothGatt object (this is done by the system).

    To solve the problem you need to listen to broadcast messages for Bluetooth state, and if ON is detected start scanning (preferably with PendingIntent (Android 8+)). If a device is found, connect to it with autoConnect=true. For bonded devices it would be enough to just initiate connection with autoConnect=true, but the BLE library does not support that use case. Instead, it saves the flag value for a reconnection, but the initial connection is done with autoConnect=false, which timeouts after 30 second. For non-bonded devices scanning is necessary after each Bluetooth state toggle so that the system could store the address type (public or private). Just connecting to a BluetoothDevice with a saved MAC address will initiate a connection to a public address, which will fail if the device advertises with a random address (even if the 48-bt MAC is the same).

Reply
  • Hello,

    That is expected behavior. When you turn Bluetooth off, you effectively call close() on your BluetoothGatt object (this is done by the system).

    To solve the problem you need to listen to broadcast messages for Bluetooth state, and if ON is detected start scanning (preferably with PendingIntent (Android 8+)). If a device is found, connect to it with autoConnect=true. For bonded devices it would be enough to just initiate connection with autoConnect=true, but the BLE library does not support that use case. Instead, it saves the flag value for a reconnection, but the initial connection is done with autoConnect=false, which timeouts after 30 second. For non-bonded devices scanning is necessary after each Bluetooth state toggle so that the system could store the address type (public or private). Just connecting to a BluetoothDevice with a saved MAC address will initiate a connection to a public address, which will fail if the device advertises with a random address (even if the 48-bt MAC is the same).

Children
No Data
Related