Android-BLE-Library icon indicating copy to clipboard operation
Android-BLE-Library copied to clipboard

The error "TOO MANY OPEN CONNECTIONS" is returned in onDeviceFailedToConnect

Open LuoPeiQin opened this issue 10 months ago • 3 comments

Reproduction environment: Phone: Google Pixel 6 (other Google phones should be the same) Android version: Android 14

Reproduction steps: Method One:

  1. Connect to a Bluetooth device and disconnect after ten seconds;
  2. Wait ten seconds and then reconnect to this Bluetooth device, then loop steps 1 and 2;
  3. 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:
  4. Simultaneously connect multiple devices and disconnect after ten seconds;
  5. Reconnect multiple devices and repeat steps 1 and 2;
  6. 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.

LuoPeiQin avatar Mar 28 '25 02:03 LuoPeiQin

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. Image

LuoPeiQin avatar Mar 28 '25 03:03 LuoPeiQin

Hi, Do you know what type is BleManagerHandler$4 exactly? This seems to be a list / map holding instances of BluetoothGatt objects.

philips77 avatar Apr 01 '25 09:04 philips77

	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.

LuoPeiQin avatar Apr 09 '25 01:04 LuoPeiQin