|
| 1 | +# micro:bit Partial Flashing Library for Android |
| 2 | + |
| 3 | +This library provides partial flashing capabilities to an Android application. It will process a hex file created in MakeCode and flash only the MakeCode script. |
| 4 | + |
| 5 | +To modularize the code, information is passed to and from the library using Intents. |
| 6 | + |
| 7 | +## Including the lib in an application |
| 8 | + |
| 9 | +Create a class that extends the base service: |
| 10 | + |
| 11 | +``` |
| 12 | +package com.microbitreactnative.pf; |
| 13 | +
|
| 14 | +import android.app.Activity; |
| 15 | +
|
| 16 | +import org.microbit.android.partialflashing.PartialFlashingBaseService; |
| 17 | +
|
| 18 | +import com.microbitreactnative.NotificationActivity; |
| 19 | +
|
| 20 | +public class PartialFlashingService extends PartialFlashingBaseService { |
| 21 | +
|
| 22 | + @Override |
| 23 | + protected Class<? extends Activity> getNotificationTarget() { |
| 24 | + return NotificationActivity.class; |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## To start a partial flash |
| 30 | + |
| 31 | +Send an Intent with the micro:bit's device address (scanning and bonding is not handled by this library) and the file path of the hex file to flash. |
| 32 | + |
| 33 | +``` |
| 34 | + MainApplication application = MainApplication.getApp(); |
| 35 | +
|
| 36 | + Log.v("MicrobitDFU", "Start Partial Flash"); |
| 37 | +
|
| 38 | + // final Intent service = new Intent(application, PartialFlashingService.class); |
| 39 | + final Intent service = new Intent(application, PartialFlashingService.class); |
| 40 | + service.putExtra("deviceAddress", deviceAddress); |
| 41 | + service.putExtra("filepath", filePath); // a path or URI must be provided. |
| 42 | +
|
| 43 | + application.startService(service); |
| 44 | +``` |
| 45 | + |
| 46 | +## Receiving progress updates |
| 47 | + |
| 48 | +Currently the library only sends progress as percentage updates (0-100%). These are broadcast during the flashing process and can be obtained using a LocalBroadcastManager. |
| 49 | + |
| 50 | +An example that forwards the information to a React Native app: |
| 51 | + |
| 52 | +``` |
| 53 | +... |
| 54 | +
|
| 55 | + public static final String BROADCAST_PROGRESS_PF = "org.microbit.android.partialflashing.broadcast.BROADCAST_PROGRESS"; |
| 56 | + public static final String EXTRA_PROGRESS_PF = "org.microbit.android.partialflashing.extra.EXTRA_PROGRESS"; |
| 57 | +
|
| 58 | + private ReactContext mReactContext; |
| 59 | + private LocalBroadcastReceiver mLocalBroadcastReceiver; |
| 60 | +
|
| 61 | + public MicrobitDFUModule(ReactApplicationContext reactContext) { |
| 62 | + super(reactContext); |
| 63 | + this.mReactContext = reactContext; |
| 64 | + this.mLocalBroadcastReceiver = new LocalBroadcastReceiver(); |
| 65 | + LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(reactContext); |
| 66 | + localBroadcastManager.registerReceiver(mLocalBroadcastReceiver, new IntentFilter(BROADCAST_PROGRESS_PF)); |
| 67 | + } |
| 68 | +
|
| 69 | + public class LocalBroadcastReceiver extends BroadcastReceiver { |
| 70 | + @Override |
| 71 | + public void onReceive(Context context, Intent intent) { |
| 72 | + // Doesn't need to be precise so using int |
| 73 | + int percentage = intent.getIntExtra(EXTRA_PROGRESS_PF, 0); |
| 74 | +
|
| 75 | + mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) |
| 76 | + .emit("flashingProgress", percentage); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | +... |
| 81 | +``` |
0 commit comments