Skip to content

Commit e175fdd

Browse files
committed
BluetoothGattCallback - add status checks
1 parent 9926501 commit e175fdd

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/main/java/org/microbit/android/partialflashing/PartialFlashingBaseService.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ protected boolean isDebug() {
183183
private BluetoothAdapter mBluetoothAdapter;
184184
private BluetoothGatt mBluetoothGatt = null;
185185
private int mConnectionState = STATE_DISCONNECTED;
186+
private boolean mWaitingForServices = false;
186187
private boolean descriptorWriteSuccess = false;
187188
BluetoothGattDescriptor descriptorRead = null;
188189
boolean descriptorReadSuccess = false;
@@ -365,7 +366,11 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
365366
public void onConnectionStateChange(BluetoothGatt gatt, int status,
366367
int newState) {
367368
logi( "onConnectionStateChange " + newState + " status " + status);
368-
//TODO this ignores status
369+
if ( status != 0) {
370+
logi("ERROR - status");
371+
mConnectionState = STATE_ERROR;;
372+
return;
373+
}
369374

370375
if (newState == BluetoothProfile.STATE_CONNECTED) {
371376
logi( "STATE_CONNECTED");
@@ -381,6 +386,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status,
381386
// about 600ms after establishing connection. Values 600 - 1600ms should be OK.
382387
}
383388
logi( "Calling gatt.discoverServices()");
389+
mWaitingForServices = true;
384390
final boolean success = gatt.discoverServices();
385391
if (!success) {
386392
Log.e(TAG,"ERROR_SERVICE_DISCOVERY_NOT_STARTED");
@@ -400,20 +406,22 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status,
400406
@Override
401407
// New services discovered
402408
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
403-
if (status == BluetoothGatt.GATT_SUCCESS) {
404-
Log.w(TAG, "onServicesDiscovered SUCCESS");
405-
logi( String.valueOf(gatt.getServices()));
406-
mConnectionState = STATE_CONNECTED_AND_READY;
407-
} else {
408-
Log.w(TAG, "onServicesDiscovered received: " + status);
409-
mConnectionState = STATE_ERROR;
409+
logi("onServicesDiscovered status " + status);
410+
if ( status != 0) {
411+
logi("ERROR - status");
412+
mConnectionState = STATE_ERROR;;
413+
return;
410414
}
411415

412-
// Clear locks
413-
synchronized (lock) {
414-
lock.notifyAll();
416+
if ( mWaitingForServices) {
417+
mWaitingForServices = false;
418+
mConnectionState = STATE_CONNECTED_AND_READY;
419+
// Clear locks
420+
synchronized (lock) {
421+
lock.notifyAll();
422+
}
423+
logi("onServicesDiscovered :: Cleared locks");
415424
}
416-
logi( "onServicesDiscovered :: Cleared locks");
417425
}
418426
@Override
419427
// API 31 Android 12
@@ -840,6 +848,7 @@ protected BluetoothGatt connect(@NonNull final String address) {
840848
long start = SystemClock.elapsedRealtime();
841849

842850
mConnectionState = STATE_CONNECTING;
851+
mWaitingForServices = false;
843852
int stateWas = mConnectionState;
844853

845854
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
@@ -964,6 +973,7 @@ private void refreshV1(boolean wantMicroBitDfu) {
964973
refresh.invoke(mBluetoothGatt);
965974
} catch (final Exception e) {
966975
}
976+
mWaitingForServices = true;
967977
mBluetoothGatt.discoverServices();
968978
lockWait(2000);
969979
}

0 commit comments

Comments
 (0)