Search code examples
androidbluetoothgatt

OnConnectionStateChange not called after disconnecting device


I'm facing a problem when I disable bluetooth from parameter. I use a Broadcast Receiver to listen this action and when its state is turned off I call a method to disconnect my peripheral. In my log I only see D/BluetoothGatt: cancelOpen() but according to BluetoothGatt.class the service calls our BluetoothGattCallback in disconnect method with onConnectionStateChanged turns to DEVICE_DISCONNECTED and I have nothing after 20 or 30 seconds too (supervisor timeout).

When I want to disconnect my device directly with my inner method it works correctly.

This is the disconnect method:

   /**
     * Disconnects an established connection, or cancels a connection attempt
     * currently in progress.
     *
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
     */
    public void disconnect() {
        if (DBG) Log.d(TAG, "cancelOpen() - device: " + mDevice.getAddress());
        if (mService == null || mClientIf == 0) return;

        try {
            mService.clientDisconnect(mClientIf, mDevice.getAddress());
        } catch (RemoteException e) {
            Log.e(TAG,"",e);
        }
    }

I checked with reflection if mClientIf equals 0 or if mService is Null but he goes to the next step and enter in the try/catch. So I don't understand the Android behavior here


Solution

  • I found a solution according to this library and this class: https://github.com/NordicSemiconductor/Android-BLE-Library/blob/master/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java

    private final BroadcastReceiver mBluetoothStateBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
            final int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);
    
            switch (state) {
                case BluetoothAdapter.STATE_TURNING_OFF:
                case BluetoothAdapter.STATE_OFF:
                    if (mConnected && previousState != BluetoothAdapter.STATE_TURNING_OFF && previousState != BluetoothAdapter.STATE_OFF) {
                        // The connection is killed by the system, no need to gently disconnect
                        mGattCallback.notifyDeviceDisconnected(mBluetoothDevice);
                    }
                    // Calling close() will prevent the STATE_OFF event from being logged (this receiver will be unregistered). But it doesn't matter.
                    close();
                    break;
            }
        }
    };
    

    I adapted it to my need but I also dispatch my event with my disconnected device info and call my own close method to release resources and prepare for another connection.