Skip to content

Commit c522daf

Browse files
committed
Flash first flow
1 parent 3b167d4 commit c522daf

File tree

1 file changed

+74
-21
lines changed

1 file changed

+74
-21
lines changed

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

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,23 @@ public void logi(String message) {
5454
// ================================================================
5555
// INTENT SERVICE
5656

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";
5858
public static final String BROADCAST_ACTION = "org.microbit.android.partialflashing.broadcast.BROADCAST_ACTION";
5959
public static final String BROADCAST_PROGRESS = "org.microbit.android.partialflashing.broadcast.BROADCAST_PROGRESS";
6060
public static final String BROADCAST_START = "org.microbit.android.partialflashing.broadcast.BROADCAST_START";
6161
public static final String BROADCAST_COMPLETE = "org.microbit.android.partialflashing.broadcast.BROADCAST_COMPLETE";
6262
public static final String EXTRA_PROGRESS = "org.microbit.android.partialflashing.extra.EXTRA_PROGRESS";
6363
public static final String BROADCAST_PF_FAILED = "org.microbit.android.partialflashing.broadcast.BROADCAST_PF_FAILED";
6464
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;
6574

6675
protected abstract Class<? extends Activity> getNotificationTarget();
6776

@@ -73,7 +82,19 @@ public PartialFlashingBaseService() {
7382
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
7483
@Override
7584
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+
}
7798
}
7899
};
79100

@@ -85,6 +106,8 @@ public void onCreate() {
85106

86107
logi( "onCreate");
87108

109+
abortReceived = false;
110+
88111
// Create intent filter and add to Local Broadcast Manager so that we can use an Intent to
89112
// start the Partial Flashing Service
90113

@@ -134,6 +157,8 @@ protected void onHandleIntent(@Nullable Intent intent) {
134157
final boolean pf = intent.getBooleanExtra("pf", true);
135158

136159
partialFlash( filePath, deviceAddress, pf);
160+
161+
checkAbort();
137162
logi("onHandleIntent END");
138163
}
139164

@@ -251,14 +276,16 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
251276

252277
for ( int i = 0; i < 3; i++) {
253278
mBluetoothGatt = connect(deviceAddress);
279+
if ( abortReceived) return;
254280
if (mBluetoothGatt != null)
255281
break;
256282
}
257283

258284
if (mBluetoothGatt == null) {
259285
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);
262289
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
263290
return;
264291
}
@@ -272,16 +299,16 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
272299
// But it doesn't seem to work in Android 8
273300
cccEnable(serviceChangedCharacteristic(), false);
274301
logi( "Reconnect");
275-
mBluetoothGatt.disconnect();
276-
lockWait(2000);
277-
mBluetoothGatt.close();
278-
mBluetoothGatt = null;
302+
disconnectAndClose();
303+
if ( abortReceived) return;
279304

280305
mBluetoothGatt = connect(deviceAddress);
306+
if ( abortReceived) return;
281307
if (mBluetoothGatt == null) {
282308
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);
285312
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
286313
return;
287314
}
@@ -297,6 +324,8 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
297324
refreshV1ForMicroBitDfu();
298325
}
299326

327+
if ( abortReceived) return;
328+
300329
int pfResult = PF_ATTEMPT_DFU;
301330
if ( pf) {
302331
logi( "Trying to partial flash");
@@ -306,6 +335,7 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
306335
}
307336

308337
String action = "";
338+
int extra = 0;
309339

310340
switch ( pfResult) {
311341
case PF_FAILED: {
@@ -322,7 +352,8 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
322352
if( isV1) {
323353
if ( !enterDFUModeV1()) {
324354
logi( "Failed to enter DFU mode");
325-
action = DFU_BROADCAST_ERROR;
355+
action = BROADCAST_ERROR;
356+
extra = ERROR_DFU_MODE;
326357
}
327358
}
328359
break;
@@ -333,13 +364,9 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
333364
}
334365
}
335366

336-
logi( "disconnect");
337-
mBluetoothGatt.disconnect();
338-
lockWait( 2000);
339-
mBluetoothGatt.close();
340-
mBluetoothGatt = null;
367+
disconnectAndClose();
341368

342-
if ( isV1 && action == BROADCAST_PF_ATTEMPT_DFU) {
369+
if (isV1 && action.equals(BROADCAST_PF_ATTEMPT_DFU)) {
343370
// Try to ensure the NordicDfu profile
344371
for ( int i = 0; i < 5; i++) {
345372
mBluetoothGatt = connect(deviceAddress);
@@ -350,21 +377,43 @@ private void partialFlash( final String filePath, final String deviceAddress, fi
350377

351378
if ( mBluetoothGatt != null) {
352379
refreshV1ForNordicDfu();
353-
mBluetoothGatt.disconnect();
354-
lockWait(2000);
355-
mBluetoothGatt.close();
356-
mBluetoothGatt = null;
380+
disconnectAndClose();
357381
}
358382
}
359383

360384
if ( !action.isEmpty()) {
361385
logi( "Send Intent: " + action);
362386
final Intent broadcast = new Intent(action);
387+
if ( action.equals(BROADCAST_ERROR)) {
388+
broadcast.putExtra(EXTRA_DATA, extra);
389+
}
363390
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
364391
}
365392
logi( "partialFlash End");
366393
}
367394

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+
368417
// Various callback methods defined by the BLE API.
369418
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
370419
@SuppressLint("MissingPermission")
@@ -1172,6 +1221,10 @@ protected BluetoothGatt connect(@NonNull final String address) {
11721221
break;
11731222
}
11741223

1224+
if ( abortReceived) {
1225+
mConnectionState = STATE_ERROR;
1226+
}
1227+
11751228
waiting = false;
11761229
switch ( mConnectionState) {
11771230
case STATE_CONNECTED_AND_READY:

0 commit comments

Comments
 (0)