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?