Search code examples
androidbluetoothbluetooth-lowenergygatt

Status is GATT_FAILURE in onConnectionStateChange after many reconnections


I'm connecting to ble device by:

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

and than

 mBluetoothGatt.disconnect();

but if I'm doing it quickly then I receive status=BluetoothGatt.GATT_FAILURE in onConnectionStateChange of mGattCallback

and then I can't connect to GATT again, even if turn off/turn on Bluetooth.

Only Force Stop of the app can solve the problem


Solution

  • Fixed by adding mBluetoothGatt.close(); when state is STATE_DISCONNECTED

    private final BluetoothGattCallback mGattCallback =
                new BluetoothGattCallback() {
                    @Override
                    public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {
                        String intentAction;
    
                        if (newState == BluetoothProfile.STATE_CONNECTED) {
    
                        } else if (status==133&&newState == BluetoothProfile.STATE_DISCONNECTED) {
                            mBluetoothGatt.close();
                        }else if (status==BluetoothGatt.GATT_FAILURE&&newState == BluetoothProfile.STATE_DISCONNECTED){
    
                        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                            mBluetoothGatt.close();
                        }
                    }