2

I'm working on BLE based android application. Where I'm testing it on Samsung S4. When I close application from recent app list, what app do is close Bluetooth GATT.

Problem is if I unpair bluetooth device from the bluetooth setting. Then on second try if user try to disconnect connection it also closes Bluetooth Gatt or On closing BluetoothGatt it doen't close the bluetooth gatt. So, the behavior of Bluetooth gatt operation is very unstable on Samsung S4.

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 

        if (status == BluetoothGatt.GATT_SUCCESS
                            && newState == BluetoothProfile.STATE_CONNECTED) {
            // Do something
        } else if (status == BluetoothGatt.GATT_SUCCESS
                            && newState == BluetoothProfile.STATE_DISCONNECTED) {
            mBluetoothDeviceAddress = null;
            mBluetoothGatt.disconnect();
            mBluetoothGatt.close();
            //....

        } else if (status != BluetoothGatt.GATT_SUCCESS) { 
            mBluetoothDeviceAddress = null;
            mBluetoothGatt.disconnect();
            mBluetoothGatt.close();
            //...
        }

    }   
}

To disconnect Bluetooth connection.

public void disconnect() {
    if (mBtAdapter == null) {
        Log.w(TAG, "disconnect: BluetoothAdapter not initialized");
        return;
    }

    if (mBluetoothGatt != null) {
        Log.i(TAG, "disconnect");

        // mBluetoothGatt.disconnect();
        new Handler(Looper.getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                mBluetoothGatt.disconnect();
            }
        });

    }
}

To close bluetooth gatt.

public void close() {
    if (mBluetoothGatt != null) {
        if (mBluetoothDeviceAddress != null) {
            Log.i(TAG, "close");

            mBluetoothGatt.disconnect();
            mBluetoothGatt.close();

            // mBluetoothGatt.close();

        }
        mBluetoothGatt = null;
    }
}

I'm performing all this BluetoothGatt related operation on service.

Device: Samsung S4
Android version: 5.0.1
Build number: LRX22C.19500XXUHOK2

Vijay Vankhede
  • 3,018
  • 1
  • 26
  • 46
  • Please clarify your question. It is unclear to me what the problem is. Do you want to keep the gatt open after closing you app? Also, provide your code. – SoroushA Mar 01 '16 at 17:15
  • @SoroushA Sorry for late reply. I have added sample code. When I close app from recent running app list. I am calling `close()` method in `onPause()` with `isFinninshing()` check. On my Samsung device some time it close `BluetoothGatt` and some time not. Also Some time I'm having incorrect Bluetooth pin error and not able to connect Bluetooth device with Samsung 4. – Vijay Vankhede Mar 10 '16 at 06:48
  • @SoroushA And when I try to diconnect bluetooth I am calling `disconnect()` But instead of only disconnecting it's closing `BluetoothGatt` – Vijay Vankhede Mar 10 '16 at 06:50
  • In my Ble connection I run the communication part in a seperate Service. When I stop the Service in my onDestroy Mehod I call mConnectedGatt.disconnect(); ble_device=null; This works pretty stable in my case. Don't forget to set your Bluetoothdevice Object = null. – FishingIsLife May 24 '18 at 11:59

0 Answers0