Darren
Darren

Reputation: 10398

Android Bluetooth Low Energy sometimes locking up

I have an app complete and ready to ship that connects to a custom bluetooth peripheral we have had made. However I have just found an issue with the app that I can't pin down.

I am running all my Bluetooth operations in a Service and sometimes when I want the bluetooth operations to end, I end up with 1 peripheral still connected but i've lost all pointers to it. And every now and then the whole bluetooth stack seems to lock up and requires the phone to be rebooted.

I think the issues are arising when trying to clean out any connected devices after I stop scanning. I have this cleanup method

private void clearAllDevices() {
        Log.e(TAG, "Clear all devices");
        for (int i = 0; i < _connectedPeripherals.size(); i++) {
            Log.e(TAG, "int i:" + i + " _connectedPeripherals size:" + _connectedPeripherals.size());
            BluetoothGatt gatt = (BluetoothGatt) _connectedPeripherals.get(i);
            gatt.disconnect();
        }
}

However I think sometimes a peripheral is half way through connecting at the same time as disconnecting from everything that has a connection.

Is there a better way to clean out all connected devices or devices that are in the process of being connected?

Upvotes: 7

Views: 1469

Answers (1)

Gyebro
Gyebro

Reputation: 1521

bluetoothGatt.disconnect() is not enough alone. You should also call bluetoothGatt.close().

Once your app has finished using a BLE device, it should call close() so the system can release resources appropriately.

See: API Guides > Bluetooth Low Energy

You may check the result of bluetoothGatt.disconnect() via BluetoothGattCallback.onConnectionStateChange callback.

Upvotes: 7

Related Questions