7

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?

1 Answer 1

7
+200

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.

3
  • Thanks. I shall try that tomorrow when I get our prototype back. I've never seen close() used in any of the tutorials I've read. Shall I call close() in the onConnectionStateChange callback once disconnected?
    – Darren
    Oct 20, 2014 at 6:46
  • 2
    I used to call close() after onConnectionStateChange reports successful disconnection.
    – Gyebro
    Oct 20, 2014 at 9:50
  • This was spot on, everything seems a lot more stable now. Thanks
    – Darren
    Oct 23, 2014 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.