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

  • Could you post the full log ? Maybe not related but did you set Auto_connect = 1 when you connect ?

    It's pretty strange that service discovery was executed when disconnected. Also I don't see the event when it's connected again. You can see at onConnectionStateChange() in UartService.java , we only do service discovery when the state is STATE_CONNECTED = 2. You can add a breakpoint there and test.

    Why you don't want to close the mService when it's disconnected? It's more clean to close the service there.

  • Could you post the full log ? Please kind let me know What key word you want me type?

    Maybe not related but did you set Auto_connect = 1 when you connect ?

    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    

    the parament that I set is false. Indeed, I only uncomment mService.close() at MainActivity.java, there are no revising at another source code.

    Why you don't want to close the mService when it's disconnected? It's more clean to close the service there.

    because we have another product, the frequency of characteristic(notify) to fast, about 10ms once, it still have same issue about disconnect from device and then reconnect again immediatly, if I don't comment mService.close(), the custom apk modifed from nordic uart will occur null point operation.

    And I just reflect this problem to our firmware engineer, he said the method we can try is that when I disconnect to device, the device receive this command, and then set device to normal mode, do not reconnect again. But I really want know what happen, because the nordic sdk default setting is that trying reconnect when disconnect command received. It is not make sense the offical apk have this issue. Thank you.

  • Blow is I type logcat, and I comment the line mService.close()

    D BluetoothAdapter: stopLeScan()
    D BluetoothAdapter: scan not started yet
    D BluetoothGatt: cancelOpen() - device: F1:C1:E2:24:ED:57
    D BtGatt.GattService: clientDisconnect() - address=F1:C1:E2:24:ED:57, connId=6
    E bt_btif : bta_gattc_mark_bg_conn unable to find the bg connection mask for: f1:c1:e2:24:ed:57
    D BtGatt.GattService: onDisconnected() - clientIf=6, connId=6, address=F1:C1:E2:24:ED:57
    D BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=F1:C1:E2:24:ED:57
    I UartService: Disconnected from GATT server.
    D nRFUART : UART_DISCONNECT_MSG
    I BroadcastQueue: am_broadcast_enqueue: [background,0,Intent { act=com.sdl.android.newservice 
      flg=0x10 (has extras) },1]
    I BroadcastQueue: am_broadcast_finished: [background,0,Intent { act=com.sdl.android.newservice 
       flg=0x10 (has extras) }]
    I BroadcastQueue: am_broadcast_finished: [background,1,Intent { 
       act=android.bluetooth.device.action.ACL_CONNECTED flg=0x4000010 (has extras) }]
    beacuse limit of characters, so we continue at blow comment.
    
  • enter code here

    I BroadcastQueue: am_broadcast_finished: [background,1,Intent 
      act=android.bluetooth.adapter.action.DISCOVERY_STARTED flg=0x10 }]
    I BroadcastQueue: am_broadcast_finished: [background,1,Intent { act=null.CONNECTION_ALARMflg=0x14 (has extras) }]
    E bt_btif : bta_gattc_cache_load: can't open GATT cache file 
    /data/misc/bluetooth/gatt_cache_f1c1e224ed57 for reading, error: No such file or directory
    W SearchService: Abort, client detached.
    I ServiceManager: Waiting for service AtCmdFwd...
    E bt_btif : bta_gattc_process_indicate, ignore HID ind/notificiation
    D BtGatt.GattService: onConnected() - clientIf=6, connId=6, address=F1:C1:E2:24:ED:57
    D BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=F1:C1:E2:24:ED:57
    I UartService: Connected to GATT server.
    D BluetoothGatt: discoverServices() - device: F1:C1:E2:24:ED:57
    
  • enter code here

    D BtGatt.GattService: discoverServices() - address=F1:C1:E2:24:ED:57, connId=6
    I UartService: Attempting to start service discovery:true
    D nRFUART : UART_CONNECT_MSG
    W Atfwd_Sendcmd: AtCmdFwd service not published, waiting... retryCnt : 5
    W bt_bta_gattc: bta_gattc_explore_srvc no more services found
    I bt_bta_dm: bta_dm_gatt_disc_result service_id_uuid_len=2
    I bt_bta_dm: bta_dm_gatt_disc_result service_id_uuid_len=2
    I bt_bta_dm: bta_dm_gatt_disc_result service_id_uuid_len=16
    E bt_btif_gattc: btif_gattc_upstreams_evt: Unhandled event (7)!
    E bt_btif_gattc: btif_gattc_upstreams_evt: Unhandled event (7)!
    E bt_btif_gattc: btif_gattc_upstreams_evt: Unhandled event (7)!
    D BtGatt.GattService: onSearchCompleted() - connId=6, status=0
    D bt_bta_gattc: bta_gattc_get_gatt_db
    D BtGatt.GattService: onGetGattDb() - address=F1:C1:E2:24:ED:57
    
Related