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

  • -----------------continue listen message of logcat---------------------

    D BtGatt.GattService: got characteristic with UUID=00002a29-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a26-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a50-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got service with UUID=0000180f-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a19-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got descriptor with UUID=00002902-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got service with UUID=0000fe55-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00000001-1000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00000001-1000-1000-8000-00805f9b34fb
    
  • -----------------continue listen message of logcat---------------------

    D BtGatt.GattService: got descriptor with UUID=00002902-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00000002-1000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00000003-1000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got service with UUID=0000fef5-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=8082caa8-41a6-4021-91c6-56f9b954cc34
    D BtGatt.GattService: got characteristic with UUID=724249f0-5ec3-4b5f-8804-42345af08651
    D BtGatt.GattService: got characteristic with UUID=6c53db25-47a1-45fe-a022-7c92fb334fd4
    D BtGatt.GattService: got characteristic with UUID=9d84b9a3-000c-49d8-9183-855b673fda31
    
  • -----------------continue listen message of logcat---------------------

    D BtGatt.GattService: got characteristic with UUID=457871e8-d516-4ca1-9116-57d0b17b9cb2
    D BtGatt.GattService: got characteristic with UUID=5f78df94-798c-46f5-990a-b3eb6a065c88
    D BtGatt.GattService: got descriptor with UUID=00002902-0000-1000-8000-00805f9b34fb
    D nRFUART : UART_DISCONNECT_MSG
    D BluetoothGatt: onSearchComplete() = Device=E9:6A:37:30:9D:D9 Status=0
    W UartService: mBluetoothGatt = android.bluetooth.BluetoothGatt@19a31dc
    W UartService: mBluetoothGatt closed
    D BluetoothGatt: close()
    D BluetoothGatt: unregisterApp() - mClientIf=6
    D BtGatt.GattService: unregisterClient() - clientIf=6
    D BtGatt.GattService: onDisconnected() - clientIf=6, connId=6, address=E9:6A:37:30:9D:D9
    
  • -----------------continue listen message of logcat---------------------

    E BtGatt.ContextMap: Context not found for ID 6
    D nRFUART : UART_CONNECT_MSG
    D AndroidRuntime: Shutting down VM
    E AndroidRuntime: FATAL EXCEPTION: main
    E AndroidRuntime: Process: com.nordicsemi.nrfUARTv2, PID: 8257
    E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothGattService android.bluetooth.BluetoothGatt.getService(java.util.UUID)' on a null object reference
    E AndroidRuntime:        at com.nordicsemi.nrfUARTv2.UartService.enableTXNotification(UartService.java:311)
    E AndroidRuntime:        at com.nordicsemi.nrfUARTv2.MainActivity$5.onReceive(MainActivity.java:237)
    

    We can find that message of UART_CONNECT_MSG which is shown it reconnects again when I disconnect from device.

  • -----------------continue listen message of logcat---------------------

    E AndroidRuntime:        at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
    E AndroidRuntime:        at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
    E AndroidRuntime:        at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
    E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:102)
    E AndroidRuntime:        at android.os.Looper.loop(Looper.java:154)
    E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6119)
    E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
    E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    
Related