From 9ff29ed04029f14275cb661fb197aa71754e2d4d Mon Sep 17 00:00:00 2001 From: joonhaengHeo <85541460+joonhaengHeo@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:33:43 +0900 Subject: [PATCH] Fix Android Bluetooth connect issue (#33087) --- .../chip/chiptool/bluetooth/BluetoothManager.kt | 16 ++++++++++++++++ .../java/chip/platform/AndroidBleManager.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt index 16d0f745b5f70f..61e95838b1cc61 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt @@ -138,6 +138,12 @@ class BluetoothManager : BleCallback { private val coroutineContinuation = continuation + private val STATE_INIT = 1 + private val STATE_DISCOVER_SERVICE = 2 + private val STATE_REQUEST_MTU = 3 + + private var mState = STATE_INIT + override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) { super.onConnectionStateChange(gatt, status, newState) Log.i( @@ -148,21 +154,31 @@ class BluetoothManager : BleCallback { if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { Log.i("$TAG|onConnectionStateChange", "Discovering Services...") + mState = STATE_DISCOVER_SERVICE gatt?.discoverServices() } } override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { Log.d(TAG, "${gatt?.device?.name}.onServicesDiscovered status = $status") + if (mState != STATE_DISCOVER_SERVICE) { + Log.d(TAG, "Invalid state : $mState") + return + } wrappedCallback.onServicesDiscovered(gatt, status) Log.i("$TAG|onServicesDiscovered", "Services Discovered") + mState = STATE_REQUEST_MTU gatt?.requestMtu(247) } override fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int) { Log.d(TAG, "${gatt?.device?.name}.onMtuChanged: connecting to CHIP device") super.onMtuChanged(gatt, mtu, status) + if (mState != STATE_REQUEST_MTU) { + Log.d(TAG, "Invalid state : $mState") + return + } wrappedCallback.onMtuChanged(gatt, mtu, status) if (coroutineContinuation.isActive) { coroutineContinuation.resume(gatt) diff --git a/src/platform/android/java/chip/platform/AndroidBleManager.java b/src/platform/android/java/chip/platform/AndroidBleManager.java index 1d440b67b18da3..52b0052d409ef8 100644 --- a/src/platform/android/java/chip/platform/AndroidBleManager.java +++ b/src/platform/android/java/chip/platform/AndroidBleManager.java @@ -584,12 +584,19 @@ private void connectBLE(Object bluetoothDeviceObj) { } class ConnectionGattCallback extends AndroidBluetoothGattCallback { + private static final int STATE_INIT = 1; + private static final int STATE_DISCOVER_SERVICE = 2; + private static final int STATE_REQUEST_MTU = 3; + + private int mState = STATE_INIT; + @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { Log.i(TAG, "onConnectionStateChange status = " + status + ", newState= + " + newState); super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { Log.i(TAG, "Discovering Services..."); + mState = STATE_DISCOVER_SERVICE; gatt.discoverServices(); return; } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { @@ -602,14 +609,23 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState public void onServicesDiscovered(BluetoothGatt gatt, int status) { Log.d(TAG, "onServicesDiscovered status = " + status); super.onServicesDiscovered(gatt, status); + if (mState != STATE_DISCOVER_SERVICE) { + Log.d(TAG, "Invalid state : " + mState); + return; + } Log.i(TAG, "Services Discovered"); + mState = STATE_REQUEST_MTU; gatt.requestMtu(247); } @Override public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { super.onMtuChanged(gatt, mtu, status); + if (mState != STATE_REQUEST_MTU) { + Log.d(TAG, "Invalid state : " + mState); + return; + } String deviceName = ""; if (gatt != null && gatt.getDevice() != null) { deviceName = gatt.getDevice().getName();