@@ -183,6 +183,7 @@ protected boolean isDebug() {
183
183
private BluetoothAdapter mBluetoothAdapter ;
184
184
private BluetoothGatt mBluetoothGatt = null ;
185
185
private int mConnectionState = STATE_DISCONNECTED ;
186
+ private boolean mWaitingForServices = false ;
186
187
private boolean descriptorWriteSuccess = false ;
187
188
BluetoothGattDescriptor descriptorRead = null ;
188
189
boolean descriptorReadSuccess = false ;
@@ -365,7 +366,11 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
365
366
public void onConnectionStateChange (BluetoothGatt gatt , int status ,
366
367
int newState ) {
367
368
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
+ }
369
374
370
375
if (newState == BluetoothProfile .STATE_CONNECTED ) {
371
376
logi ( "STATE_CONNECTED" );
@@ -381,6 +386,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status,
381
386
// about 600ms after establishing connection. Values 600 - 1600ms should be OK.
382
387
}
383
388
logi ( "Calling gatt.discoverServices()" );
389
+ mWaitingForServices = true ;
384
390
final boolean success = gatt .discoverServices ();
385
391
if (!success ) {
386
392
Log .e (TAG ,"ERROR_SERVICE_DISCOVERY_NOT_STARTED" );
@@ -400,20 +406,22 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status,
400
406
@ Override
401
407
// New services discovered
402
408
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 ;
410
414
}
411
415
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" );
415
424
}
416
- logi ( "onServicesDiscovered :: Cleared locks" );
417
425
}
418
426
@ Override
419
427
// API 31 Android 12
@@ -840,6 +848,7 @@ protected BluetoothGatt connect(@NonNull final String address) {
840
848
long start = SystemClock .elapsedRealtime ();
841
849
842
850
mConnectionState = STATE_CONNECTING ;
851
+ mWaitingForServices = false ;
843
852
int stateWas = mConnectionState ;
844
853
845
854
final BluetoothDevice device = mBluetoothAdapter .getRemoteDevice (address );
@@ -964,6 +973,7 @@ private void refreshV1(boolean wantMicroBitDfu) {
964
973
refresh .invoke (mBluetoothGatt );
965
974
} catch (final Exception e ) {
966
975
}
976
+ mWaitingForServices = true ;
967
977
mBluetoothGatt .discoverServices ();
968
978
lockWait (2000 );
969
979
}
0 commit comments