Skip to content

Commit 9ff29ed

Browse files
authored
Fix Android Bluetooth connect issue (#33087)
1 parent 8dd2de7 commit 9ff29ed

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ class BluetoothManager : BleCallback {
138138

139139
private val coroutineContinuation = continuation
140140

141+
private val STATE_INIT = 1
142+
private val STATE_DISCOVER_SERVICE = 2
143+
private val STATE_REQUEST_MTU = 3
144+
145+
private var mState = STATE_INIT
146+
141147
override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
142148
super.onConnectionStateChange(gatt, status, newState)
143149
Log.i(
@@ -148,21 +154,31 @@ class BluetoothManager : BleCallback {
148154

149155
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
150156
Log.i("$TAG|onConnectionStateChange", "Discovering Services...")
157+
mState = STATE_DISCOVER_SERVICE
151158
gatt?.discoverServices()
152159
}
153160
}
154161

155162
override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
156163
Log.d(TAG, "${gatt?.device?.name}.onServicesDiscovered status = $status")
164+
if (mState != STATE_DISCOVER_SERVICE) {
165+
Log.d(TAG, "Invalid state : $mState")
166+
return
167+
}
157168
wrappedCallback.onServicesDiscovered(gatt, status)
158169

159170
Log.i("$TAG|onServicesDiscovered", "Services Discovered")
171+
mState = STATE_REQUEST_MTU
160172
gatt?.requestMtu(247)
161173
}
162174

163175
override fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int) {
164176
Log.d(TAG, "${gatt?.device?.name}.onMtuChanged: connecting to CHIP device")
165177
super.onMtuChanged(gatt, mtu, status)
178+
if (mState != STATE_REQUEST_MTU) {
179+
Log.d(TAG, "Invalid state : $mState")
180+
return
181+
}
166182
wrappedCallback.onMtuChanged(gatt, mtu, status)
167183
if (coroutineContinuation.isActive) {
168184
coroutineContinuation.resume(gatt)

src/platform/android/java/chip/platform/AndroidBleManager.java

+16
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,19 @@ private void connectBLE(Object bluetoothDeviceObj) {
584584
}
585585

586586
class ConnectionGattCallback extends AndroidBluetoothGattCallback {
587+
private static final int STATE_INIT = 1;
588+
private static final int STATE_DISCOVER_SERVICE = 2;
589+
private static final int STATE_REQUEST_MTU = 3;
590+
591+
private int mState = STATE_INIT;
592+
587593
@Override
588594
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
589595
Log.i(TAG, "onConnectionStateChange status = " + status + ", newState= + " + newState);
590596
super.onConnectionStateChange(gatt, status, newState);
591597
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
592598
Log.i(TAG, "Discovering Services...");
599+
mState = STATE_DISCOVER_SERVICE;
593600
gatt.discoverServices();
594601
return;
595602
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
@@ -602,14 +609,23 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
602609
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
603610
Log.d(TAG, "onServicesDiscovered status = " + status);
604611
super.onServicesDiscovered(gatt, status);
612+
if (mState != STATE_DISCOVER_SERVICE) {
613+
Log.d(TAG, "Invalid state : " + mState);
614+
return;
615+
}
605616

606617
Log.i(TAG, "Services Discovered");
618+
mState = STATE_REQUEST_MTU;
607619
gatt.requestMtu(247);
608620
}
609621

610622
@Override
611623
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
612624
super.onMtuChanged(gatt, mtu, status);
625+
if (mState != STATE_REQUEST_MTU) {
626+
Log.d(TAG, "Invalid state : " + mState);
627+
return;
628+
}
613629
String deviceName = "";
614630
if (gatt != null && gatt.getDevice() != null) {
615631
deviceName = gatt.getDevice().getName();

0 commit comments

Comments
 (0)