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

  • enter code here

    D BtGatt.GattService: got service with UUID=00001800-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a00-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a01-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a04-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got service with UUID=00001801-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got service with UUID=6e400001-b5a3-f393-e0a9-e50e24dcca9e
    D BtGatt.GattService: got characteristic with UUID=6e400003-b5a3-f393-e0a9-e50e24dcca9e
    D BtGatt.GattService: got descriptor with UUID=00002902-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=6e400002-b5a3-f393-e0a9-e50e24dcca9e
    D BluetoothGatt: onSearchComplete() = Device=F1:C1:E2:24:ED:57 Status=0
    W UartService: mBluetoothGatt = android.bluetooth.BluetoothGatt@7cfef34
    D BluetoothGatt: setCharacteristicNotification() - uuid: 6e400003-b5a3-f393-e0a9-e50e24dcca9e enable: true
    D BtGatt.GattService: registerForNotification() - address=F1:C1:E2:24:ED:57 enable: true
    
  • enter code here

    W bt_btif : notification already registered
    D BtGatt.GattService: onRegisterForNotifications() - address=null, status=0, registered=1, handle=11
    
  • @berniechen: Please try to reproduce the "null point operation" issue with the nordic stock app with no modification. We need to fix that, instead of comment out mService.close();

    You may also want to test on other Android phone.

    The nRFUART app is pretty old, you should try to use nRFToolbox as reference app.

  • @Hung Bui: Hi, Sorry, I still do some revises about UUID. We are using google pixel phone( native android 7.1.1) instead of Xperia x compact. And I only revise the code below //public static final UUID RX_SERVICE_UUID = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); public static final UUID RX_SERVICE_UUID = UUID.fromString("0000fe55-0000-1000-8000-00805f9b34fb");

    //public static final UUID TX_CHAR_UUID = UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
    public static final UUID TX_CHAR_UUID = UUID.fromString("00000001-1000-1000-8000-00805f9b34fb");
    

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

    Then, I got the error message of application forced closed when I press the Disconnect button and result like picture third above. The result of I am listening logcat shows the root cause is the phone will reconnect from callback but we have executed mService.close() to release resource before.

    I UartService: Connected to GATT server.
    D BluetoothGatt: discoverServices() - device: E9:6A:37:30:9D:D9
    D BtGatt.GattService: discoverServices() - address=E9:6A:37:30:9D:D9, connId=6
    
  • -----------------continue listen message of logcat---------------------

    D BtGatt.GattService: onSearchCompleted() - connId=6, status=0
    I UartService: Attempting to start service discovery:true
    D bt_bta_gattc: bta_gattc_get_gatt_db
    D BtGatt.GattService: onGetGattDb() - address=E9:6A:37:30:9D:D9
    D BtGatt.GattService: got service with UUID=00001800-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a00-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a01-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got characteristic with UUID=00002a04-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got service with UUID=00001801-0000-1000-8000-00805f9b34fb
    D BtGatt.GattService: got service with UUID=0000180a-0000-1000-8000-00805f9b34fb
    
1 2 3 4 5 »