Skip to content Skip to sidebar Skip to footer

Unhandled Exception In Callback While Trying To Connect With Bluetoothgatt

I am trying to connect to a peripheral. When I find the device I want in Scancallback I send a message with the device to my handler. 11-16 10:31:12.471 25907-25907/I/BleManager: S

Solution 1:

I just found the problem. It was a stupid thing, I was calling device.connect with a null bluetoothGattCallback. Writing the question really helped me, I was a hole day in this issue.

Solution 2:

Try to do the callback like this:

// Various callback methods defined by the BLE API.privatefinalBluetoothGattCallbackmGattCallback=newBluetoothGattCallback() {
    @OverridepublicvoidonConnectionStateChange(BluetoothGatt gatt, int status,
            int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());

        } elseif (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
        }
    }
}

source: https://developer.android.com/guide/topics/connectivity/bluetooth-le.html

And make sure you keep your BluetoothGatt bluetoothGatt. If you write null into it, callbacks are not called anymore. Maybe this solves your problem.

Post a Comment for "Unhandled Exception In Callback While Trying To Connect With Bluetoothgatt"