The error "TOO MANY OPEN CONNECTIONS" is returned in onDeviceFailedToConnect
Reproduction environment: Phone: Google Pixel 6 (other Google phones should be the same) Android version: Android 14
Reproduction steps: Method One:
- Connect to a Bluetooth device and disconnect after ten seconds;
- Wait ten seconds and then reconnect to this Bluetooth device, then loop steps 1 and 2;
- After repeating steps 1 and 2 about twenty times, the "TOO MANY OPEN CONNECTIONS" message will appear, and at this point, no Bluetooth devices can be connected unless Bluetooth or the App is restarted; Method Two:
- Simultaneously connect multiple devices and disconnect after ten seconds;
- Reconnect multiple devices and repeat steps 1 and 2;
- The issue can be reproduced within about five minutes;
I did not encounter this problem on an OPPO phone with the latest Android 15 system. Could this issue be related to any recent code changes by Google? How should I resolve this issue?
I am eagerly awaiting replies from everyone and the author. Thank you very much.
After the issue occurred, I checked the Heap Dump of the Profiler and found that all BleManager instances were held by BluetoothGatt, but in fact, the disconnect calls were all BleManager.
Hi,
Do you know what type is BleManagerHandler$4 exactly?
This seems to be a list / map holding instances of BluetoothGatt objects.
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(@NonNull final BluetoothGatt gatt,
final int status, final int newState) {
log(Log.DEBUG, () ->
"[Callback] Connection state changed with status: " + status +
" and new state: " + newState + " (" + ParserUtils.stateToString(newState) + ")");
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
// Sometimes, when a notification/indication is received after the device got
// disconnected, the Android calls onConnectionStateChanged again, with state
// STATE_CONNECTED.
// See: https://github.com/NordicSemiconductor/Android-BLE-Library/issues/43
if (bluetoothDevice == null) {
Log.e(TAG, "Device received notification after disconnection.");
log(Log.DEBUG, () -> "gatt.close()");
try {
gatt.close();
} catch (final Throwable t) {
// ignore
}
return;
}
After comparative analysis, it was found that BleManagerHandler$4 corresponds to the code mentioned above, but the specific cause has yet to be determined.
Thank you very much for providing such an excellent open-source library.