Android Bluetooth Low Energy is sometimes blocked

I have a ready-to-send application that connects to the custom Bluetooth peripheral we made. However, I just found a problem with an application that I cannot install.

I do all my Bluetooth operations in Service , and sometimes when I need the bluetooth operations to end, I end up with 1 peripheral device still connected, but I lost all the pointers to it. From time to time, the entire bluetooth stack is blocked and requires a phone reboot.

I think that problems arise when I try to clean up any connected devices after I stopped scanning. I have this cleaning 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, sometimes I think that the peripheral device is halfway through the connection at the same time as disconnecting from everything that has the connection.

Is there a better way to clear all connected devices or devices that are in the process of connecting?

+5
source share
1 answer

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

Once your application has finished using the BLE device, it must call close() so that the system can free resources accordingly.

See: API Guides> Low Energy Bluetooth

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

+5
source

Source: https://habr.com/ru/post/1204818/


All Articles