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

Nordic nRF Master avoids disconnection

We have noticed that when our application is trying to call GATT disconnect while nRF master app is running, our app fails to disconnect. More over, when our app connects to a device we noticed that nRF is connecting to our device as well. How can we force disconnect even if nRF is connected to our device?

Parents
  • Hi, On Android, and perhaps on iOS as well, each app sees a BLE device independently. The system takes care of the physical connection. It disconnects only if the last app has stopped using it. From the app perspective, when it calls the disconnect() method, it get a callback with state DISCONNECTED, and should be happy with it. It does not mean, that the device has been in fact disconnected, but only this app stopped using it. nRF MCP, when configured correctly, can in fact connect to any connected device (when in background, or launched when a connection is active). But it's not only nRF MCP. Any app can open the GATT Server and then receive onConnectionStateChanged(device, CONNECTED) callback for each connected device that connects to the phone, or is already connected. Actually, we sometimes observe, that when one of our apps wants to connect to a device, when MCP is in the background, it fails to get onServicesDiscovered(...), but MCP succeeds, preventing the device from starting advertise again. The workaround is to wait 2 sec after getting onConnStateChanged before calling gatt.discoverServices().

    There are 2 ways of closing the physical connections:

    1. Make sure all apps (including Android Bluetooth Share or Android Bluetooth Settings) has stopped using it,
    2. Implement a command on your device that will disconnect on request. Graceful disconnect is preferred, as the timeout may be around 20 sec on Android. Then, when you want to disconnect, instead of calling gatt.disconnect() method, you have to write this value of yours to a characteristic and wait for onConnectionStateChange callback with status = 0x13 (19) = GATT CONN TERMINATE PEER USER.

    To disable the "autoconnect" feature on Android, there are 2 ways:

    1. Disable the GATT Server (Go to Configure GATT Server and click the Disable icon on the main drop down menu)
    2. Disable "Show incoming connections" in Settings->Connectivity.

    I'm not sure if the last one will allow you to close the physical connection from your app as I'm not calling cancelConnection(), but at least MCP will not connect using connectGatt(...). The device may still be connected to MCP's GATT Server. Actually you could give me a feedback on this one.

    BR, Aleksander

Reply
  • Hi, On Android, and perhaps on iOS as well, each app sees a BLE device independently. The system takes care of the physical connection. It disconnects only if the last app has stopped using it. From the app perspective, when it calls the disconnect() method, it get a callback with state DISCONNECTED, and should be happy with it. It does not mean, that the device has been in fact disconnected, but only this app stopped using it. nRF MCP, when configured correctly, can in fact connect to any connected device (when in background, or launched when a connection is active). But it's not only nRF MCP. Any app can open the GATT Server and then receive onConnectionStateChanged(device, CONNECTED) callback for each connected device that connects to the phone, or is already connected. Actually, we sometimes observe, that when one of our apps wants to connect to a device, when MCP is in the background, it fails to get onServicesDiscovered(...), but MCP succeeds, preventing the device from starting advertise again. The workaround is to wait 2 sec after getting onConnStateChanged before calling gatt.discoverServices().

    There are 2 ways of closing the physical connections:

    1. Make sure all apps (including Android Bluetooth Share or Android Bluetooth Settings) has stopped using it,
    2. Implement a command on your device that will disconnect on request. Graceful disconnect is preferred, as the timeout may be around 20 sec on Android. Then, when you want to disconnect, instead of calling gatt.disconnect() method, you have to write this value of yours to a characteristic and wait for onConnectionStateChange callback with status = 0x13 (19) = GATT CONN TERMINATE PEER USER.

    To disable the "autoconnect" feature on Android, there are 2 ways:

    1. Disable the GATT Server (Go to Configure GATT Server and click the Disable icon on the main drop down menu)
    2. Disable "Show incoming connections" in Settings->Connectivity.

    I'm not sure if the last one will allow you to close the physical connection from your app as I'm not calling cancelConnection(), but at least MCP will not connect using connectGatt(...). The device may still be connected to MCP's GATT Server. Actually you could give me a feedback on this one.

    BR, Aleksander

Children
No Data
Related