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

Why the device will connect again when we diconnect from device?

I modified the nordic uart apk from this website

github.com/.../Android-nRF-UART

and I do some revising below

MainActivity.java:
if (action.equals(WearableService.ACTION_GATT_DISCONNECTED)) {
    runOnUiThread(new Runnable() {
     public void run() {
     Log.d(tAg, "UART_DISCONNECT_MSG");
     mState = UART_PROFILE_DISCONNECTED;
     //mService.close(); <--- I comment this
     }
     });
 }

When I disconnect from device, I can see log on my phone Xperia X compact (android 7.0)

BluetoothGatt: cancelOpen() - device: F1:C1:E2:24:ED:57
BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=F1:C1:E2:24:ED:57
BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=F1:C1:E2:24:ED:57
BluetoothGatt: discoverServices() - device: F1:C1:E2:24:ED:57
BluetoothGatt: onSearchComplete() = Device=F1:C1:E2:24:ED:57 Status=0
BluetoothGatt: setCharacteristicNotification() - uuid: 6e400003-b5a3-f393-e0a9-e50e24dcca9e enable: true

I find It will connect again after I disconnect from the device and then do discoverServi

ce and finally disconnect, why???? The workaround method is uncomment the mService.close() to release the resource when MainActivity.java gets DISCONNECT broadcast.

Below is the screen shot of my phone(we only revised UUID for UartService, there are no revising at Uart apk). When I disconnect from device at 10:18:45 and then reconnect at 10:18:47 and then send one command(this device automatically notify data to apk per 3 second) and then finally disconnect.image description

Another product we develop is below, because it will reconnect, so the button still show Disconnect, not Connect, and we can't change button state anymore, because it has disconnect, we can't send disconnect command to it.image description

below is operate null pointer.image description

  • I test the google pixel with custom system image with no Spotify app, but it still has the same problem....

  • What happens if you uninstall your app, connect to your device with nRF Connect, and disconnect again? Do you still experience that the phone keeps reconnecting by itself?

    If it is something similar to the Spotify issue it shouldn't matter what app you use to connect. You just need to connect and disconnect once, with e.g. nRF Connect, and then Spotify (or maybe a different app) will continue the cycle in the background.

  • Hi, I force close Spotify on my Xeperia XC, and using nRF Connect to scanning device and connect to it, then I disconnect from device. It will not reconnect again. If I connect it and open CCCD enable, then the characteristic send me some values 100 times per sec, it will reconnect again after I disconnecting from device. If I disable the CCCD, then pressing the disconnect, it will not reconnect again. why..?

  • There is my solution at nRF UART APK.

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
        if(RxService != null) {
            BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
            BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
            descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    
            mBluetoothGatt.writeDescriptor(descriptor);
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mBluetoothGatt.disconnect();
            }
        }, 1000);
    

    at the function of disconnect(), We need disable descriptor first, and post delay 1 sec to disconnect() then close the gatt.

    But I hope the bluetooth expert director me why the device will reconnect again when we turn on notify characteristic??? Thanks.

  • Hi berniechen ,

    Thanks for sharing your workaround with us. It's still strange that you need to disable notification before you disconnect.

    May I ask do you still have the reconnecting problem if you don't implement the workaround. In our test here our devices didn't have problem after we remove Spotify app.

    Have you tried to test on a fresh phone with minimum app installed ? We are suspecting there could be an app that detect if there is notification receives from a device and then the device get disconnected it will try to re-connect again.

Related