@@ -54,14 +54,23 @@ public void logi(String message) {
54
54
// ================================================================
55
55
// INTENT SERVICE
56
56
57
- public static final String DFU_BROADCAST_ERROR = "no.nordicsemi .android.dfu .broadcast.BROADCAST_ERROR" ;
57
+ public static final String BROADCAST_ERROR = "org.microbit .android.partialflashing .broadcast.BROADCAST_ERROR" ;
58
58
public static final String BROADCAST_ACTION = "org.microbit.android.partialflashing.broadcast.BROADCAST_ACTION" ;
59
59
public static final String BROADCAST_PROGRESS = "org.microbit.android.partialflashing.broadcast.BROADCAST_PROGRESS" ;
60
60
public static final String BROADCAST_START = "org.microbit.android.partialflashing.broadcast.BROADCAST_START" ;
61
61
public static final String BROADCAST_COMPLETE = "org.microbit.android.partialflashing.broadcast.BROADCAST_COMPLETE" ;
62
62
public static final String EXTRA_PROGRESS = "org.microbit.android.partialflashing.extra.EXTRA_PROGRESS" ;
63
63
public static final String BROADCAST_PF_FAILED = "org.microbit.android.partialflashing.broadcast.BROADCAST_PF_FAILED" ;
64
64
public static final String BROADCAST_PF_ATTEMPT_DFU = "org.microbit.android.partialflashing.broadcast.BROADCAST_PF_ATTEMPT_DFU" ;
65
+ public static final String BROADCAST_PF_ABORTED = "org.microbit.android.partialflashing.broadcast.BROADCAST_PF_ABORTED" ;
66
+ public static final String EXTRA_ACTION = "org.microbit.android.partialflashing.extra.EXTRA_ACTION" ;
67
+ public static final int ACTION_ABORT = 0 ;
68
+ public static final String EXTRA_DATA = "org.microbit.android.partialflashing.extra.EXTRA_DATA" ;
69
+ public static final int ERROR_CONNECT = 1 ;
70
+ public static final int ERROR_RECONNECT = 2 ;
71
+ public static final int ERROR_DFU_MODE = 3 ;
72
+
73
+ private boolean abortReceived = false ;
65
74
66
75
protected abstract Class <? extends Activity > getNotificationTarget ();
67
76
@@ -73,7 +82,19 @@ public PartialFlashingBaseService() {
73
82
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver () {
74
83
@ Override
75
84
public void onReceive (Context context , final Intent intent ) {
76
- logi ( "Received Broadcast: " + intent .toString ());
85
+ logi ("Received Broadcast: " + intent .toString ());
86
+ int extra = intent .getIntExtra (EXTRA_ACTION , -1 );
87
+ switch (extra ) {
88
+ case ACTION_ABORT :
89
+ abortReceived = true ;
90
+ // Clear locks
91
+ synchronized (lock ) {
92
+ lock .notifyAll ();
93
+ }
94
+ break ;
95
+ default :
96
+ break ;
97
+ }
77
98
}
78
99
};
79
100
@@ -85,6 +106,8 @@ public void onCreate() {
85
106
86
107
logi ( "onCreate" );
87
108
109
+ abortReceived = false ;
110
+
88
111
// Create intent filter and add to Local Broadcast Manager so that we can use an Intent to
89
112
// start the Partial Flashing Service
90
113
@@ -134,6 +157,8 @@ protected void onHandleIntent(@Nullable Intent intent) {
134
157
final boolean pf = intent .getBooleanExtra ("pf" , true );
135
158
136
159
partialFlash ( filePath , deviceAddress , pf );
160
+
161
+ checkAbort ();
137
162
logi ("onHandleIntent END" );
138
163
}
139
164
@@ -251,14 +276,16 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
251
276
252
277
for ( int i = 0 ; i < 3 ; i ++) {
253
278
mBluetoothGatt = connect (deviceAddress );
279
+ if ( abortReceived ) return ;
254
280
if (mBluetoothGatt != null )
255
281
break ;
256
282
}
257
283
258
284
if (mBluetoothGatt == null ) {
259
285
logi ( "Failed to connect" );
260
- logi ( "Send Intent: DFU_BROADCAST_ERROR" );
261
- final Intent broadcast = new Intent (DFU_BROADCAST_ERROR );
286
+ logi ( "Send Intent: BROADCAST_ERROR" );
287
+ final Intent broadcast = new Intent (BROADCAST_ERROR );
288
+ broadcast .putExtra (EXTRA_DATA , ERROR_CONNECT );
262
289
LocalBroadcastManager .getInstance (this ).sendBroadcast (broadcast );
263
290
return ;
264
291
}
@@ -272,16 +299,16 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
272
299
// But it doesn't seem to work in Android 8
273
300
cccEnable (serviceChangedCharacteristic (), false );
274
301
logi ( "Reconnect" );
275
- mBluetoothGatt .disconnect ();
276
- lockWait (2000 );
277
- mBluetoothGatt .close ();
278
- mBluetoothGatt = null ;
302
+ disconnectAndClose ();
303
+ if ( abortReceived ) return ;
279
304
280
305
mBluetoothGatt = connect (deviceAddress );
306
+ if ( abortReceived ) return ;
281
307
if (mBluetoothGatt == null ) {
282
308
logi ( "Failed to connect" );
283
- logi ( "Send Intent: DFU_BROADCAST_ERROR" );
284
- final Intent broadcast = new Intent (DFU_BROADCAST_ERROR );
309
+ logi ( "Send Intent: BROADCAST_ERROR" );
310
+ final Intent broadcast = new Intent (BROADCAST_ERROR );
311
+ broadcast .putExtra (EXTRA_DATA , ERROR_RECONNECT );
285
312
LocalBroadcastManager .getInstance (this ).sendBroadcast (broadcast );
286
313
return ;
287
314
}
@@ -297,6 +324,8 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
297
324
refreshV1ForMicroBitDfu ();
298
325
}
299
326
327
+ if ( abortReceived ) return ;
328
+
300
329
int pfResult = PF_ATTEMPT_DFU ;
301
330
if ( pf ) {
302
331
logi ( "Trying to partial flash" );
@@ -306,6 +335,7 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
306
335
}
307
336
308
337
String action = "" ;
338
+ int extra = 0 ;
309
339
310
340
switch ( pfResult ) {
311
341
case PF_FAILED : {
@@ -322,7 +352,8 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
322
352
if ( isV1 ) {
323
353
if ( !enterDFUModeV1 ()) {
324
354
logi ( "Failed to enter DFU mode" );
325
- action = DFU_BROADCAST_ERROR ;
355
+ action = BROADCAST_ERROR ;
356
+ extra = ERROR_DFU_MODE ;
326
357
}
327
358
}
328
359
break ;
@@ -333,13 +364,9 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
333
364
}
334
365
}
335
366
336
- logi ( "disconnect" );
337
- mBluetoothGatt .disconnect ();
338
- lockWait ( 2000 );
339
- mBluetoothGatt .close ();
340
- mBluetoothGatt = null ;
367
+ disconnectAndClose ();
341
368
342
- if ( isV1 && action == BROADCAST_PF_ATTEMPT_DFU ) {
369
+ if (isV1 && action . equals ( BROADCAST_PF_ATTEMPT_DFU ) ) {
343
370
// Try to ensure the NordicDfu profile
344
371
for ( int i = 0 ; i < 5 ; i ++) {
345
372
mBluetoothGatt = connect (deviceAddress );
@@ -350,21 +377,43 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
350
377
351
378
if ( mBluetoothGatt != null ) {
352
379
refreshV1ForNordicDfu ();
353
- mBluetoothGatt .disconnect ();
354
- lockWait (2000 );
355
- mBluetoothGatt .close ();
356
- mBluetoothGatt = null ;
380
+ disconnectAndClose ();
357
381
}
358
382
}
359
383
360
384
if ( !action .isEmpty ()) {
361
385
logi ( "Send Intent: " + action );
362
386
final Intent broadcast = new Intent (action );
387
+ if ( action .equals (BROADCAST_ERROR )) {
388
+ broadcast .putExtra (EXTRA_DATA , extra );
389
+ }
363
390
LocalBroadcastManager .getInstance (this ).sendBroadcast (broadcast );
364
391
}
365
392
logi ( "partialFlash End" );
366
393
}
367
394
395
+ @ SuppressLint ("MissingPermission" )
396
+ private void disconnectAndClose () {
397
+ if ( mBluetoothGatt != null ) {
398
+ logi ("disconnect" );
399
+ mBluetoothGatt .disconnect ();
400
+ lockWait (2000 );
401
+ mBluetoothGatt .close ();
402
+ mBluetoothGatt = null ;
403
+ }
404
+ }
405
+
406
+ private boolean checkAbort () {
407
+ if ( !abortReceived ) {
408
+ return false ;
409
+ }
410
+ disconnectAndClose ();
411
+ logi ("Send Intent: " + BROADCAST_PF_ABORTED );
412
+ final Intent broadcast = new Intent (BROADCAST_PF_ABORTED );
413
+ LocalBroadcastManager .getInstance (this ).sendBroadcast (broadcast );
414
+ return true ;
415
+ }
416
+
368
417
// Various callback methods defined by the BLE API.
369
418
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback () {
370
419
@ SuppressLint ("MissingPermission" )
@@ -1172,6 +1221,10 @@ protected BluetoothGatt connect(@NonNull final String address) {
1172
1221
break ;
1173
1222
}
1174
1223
1224
+ if ( abortReceived ) {
1225
+ mConnectionState = STATE_ERROR ;
1226
+ }
1227
+
1175
1228
waiting = false ;
1176
1229
switch ( mConnectionState ) {
1177
1230
case STATE_CONNECTED_AND_READY :
0 commit comments