FirmwareUpdateActivity

This is the activity which will require DfuService class to upload the firmware. You will first need to implement a file chooser which will be responsible to pick the secure package for firmware. This can be achieved by including following package to your FirmwareUpdateActivity:

private static final int FIRMWARE_SELECT_CODE = 0;
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip");
intent.addCategory(Intent.CATEGORY_OPENABLE);

try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FIRMWARE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}

FIRMWARE_SELECT_CODE is a unique code that we are using while calling the intent. It will help us to filter a callback from this intent. When we will come back to our application from the file chooser, we will use FIRMWARE_SELECT_CODE for identification. Your intent from the file chooser will come back to onActivityResult, where you will write:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FIRMWARE_SELECT_CODE:
if (resultCode == RESULT_OK) {
firmwareUri = data.getData();
this.grantUriPermission(this.getPackageName(), firmwareUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

The next step is to write the method which will use firmwareUri to upload the firmware to the device. It will require two other parameters:

  • deviceAddress: MAC address of the device you want to upload the firmware
  • deviceName: Name of the device you want to upload the firmware

Identify these two things using basic Bluetooth LE scan (as described in the previous sections) and call:

private void startSecureDfuProcess(String deviceAddress, String deviceName) {
final DfuServiceInitiator starter = new DfuServiceInitiator(deviceAddress).setDeviceName(deviceName).setKeepBond(true);
starter.setZip(firmwareUri);
starter.start(this, DfuService.class);
}

DfuServiceInitiator will take the parameters such as deviceAddress, deviceName and firmwareUri and by calling DfuServiceInitiator.start(), you can start the secure device firmware update process. Notice that there is no message listener registered yet. So, in order to hear the messages from the device firmware update process you will need a DfuProgressListener. Defined a global variable mDfuProgressListener:

private DfuProgressListener mDfuProgressListener = new DfuProgressListener() {

//Override methods here...
}

DfuProgressListener provides many methods to override certain behaviors. You can find following methods in it:

  • onDeviceConnecting, is called when DfuService is making a connection to the device
  • onDeviceConnected, is called when a connection is established
  • onDfuProcessStarting, is called upon starting the Dfu process
  • onDfuProcessStarted, is called when the Dfu process has started
  • onFirmwareValidating, is called to indicate that service is validating the firmware package
  • onDeviceDisconnecting, is called when the device is disconnecting from the service
  • onDfuCompleted, is called when the DFU process if completed
  • onDfuAborted, is called when the DFU process is aborted to some reason
  • onError, is called if an error occurs in the DFU upload process

These hooks can be used to update the UI on the go. It will keep the user informed of the current processes. Once you implement these methods, you can register the mDfuProgressListener to the DfuService by calling in onResume method of your activity:

DfuServiceListenerHelper.registerProgressListener(this, mDfuProgressListener);

By implementing this project, you will be able to successfully execute device firmware service and hear the message callbacks. The code is generic and can be executed from any activity. Just make sure that you have given location, Bluetooth and Uri read permissions to the activity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.8