public abstract class DfuBaseService
extends IntentService
To run the service to your application extend it in your project and overwrite the missing method. Remember to add your class to the AndroidManifest.xml file.
Start the service with the following parameters:
final Intent service = new Intent(this, YourDfuService.class); service.putExtra(DfuService.EXTRA_DEVICE_ADDRESS, mSelectedDevice.getAddress()); // Target device address service.putExtra(DfuService.EXTRA_DEVICE_NAME, mSelectedDevice.getName()); // This name will be shown on the notification service.putExtra(DfuService.EXTRA_FILE_MIME_TYPE, mFileType == DfuService.TYPE_AUTO ? YourDfuService.MIME_TYPE_ZIP : YourDfuService.MIME_TYPE_OCTET_STREAM); service.putExtra(DfuService.EXTRA_FILE_TYPE, mFileType); service.putExtra(DfuService.EXTRA_FILE_PATH, mFilePath); service.putExtra(DfuService.EXTRA_FILE_URI, mFileStreamUri); // optionally service.putExtra(DfuService.EXTRA_INIT_FILE_PATH, mInitFilePath); service.putExtra(DfuService.EXTRA_INIT_FILE_URI, mInitFileStreamUri); service.putExtra(DfuService.EXTRA_RESTORE_BOND, mRestoreBond); startService(service);The
EXTRA_FILE_MIME_TYPE
and EXTRA_FILE_TYPE
parameters are optional. If not provided the application upload from HEX/BIN file is assumed.
The service API is compatible with previous versions.
The service will show its progress on the notification bar and will send local broadcasts to the application.
Modifier and Type | Field and Description |
---|---|
static int |
ACTION_ABORT
Aborts the upload.
|
static int |
ACTION_PAUSE
Pauses the upload.
|
static int |
ACTION_RESUME
Resumes the upload that has been paused before using
ACTION_PAUSE . |
static java.lang.String |
BROADCAST_ACTION
Activity may broadcast this broadcast in order to pause, resume or abort DFU process.
|
static java.lang.String |
BROADCAST_ERROR
The broadcast error message contains the following extras:
EXTRA_DATA - the error number. |
static java.lang.String |
BROADCAST_LOG
The log events are only broadcast when there is no nRF Logger installed.
|
static java.lang.String |
BROADCAST_PROGRESS
The broadcast message contains the following extras:
EXTRA_DATA - the progress value (percentage 0-100) or:
PROGRESS_CONNECTING
PROGRESS_STARTING
PROGRESS_ENABLING_DFU_MODE
PROGRESS_VALIDATING
PROGRESS_DISCONNECTING
PROGRESS_COMPLETED
PROGRESS_ABORTED
EXTRA_DEVICE_ADDRESS - the target device address
EXTRA_PART_CURRENT - the number of currently transmitted part
EXTRA_PARTS_TOTAL - total number of parts that are being sent, f.e. |
static int |
DFU_STATUS_CRC_ERROR |
static int |
DFU_STATUS_DATA_SIZE_EXCEEDS_LIMIT |
static int |
DFU_STATUS_INVALID_STATE |
static int |
DFU_STATUS_NOT_SUPPORTED |
static int |
DFU_STATUS_OPERATION_FAILED |
static int |
DFU_STATUS_SUCCESS |
static int |
ERROR_BLUETOOTH_DISABLED
Thrown when the the Bluetooth adapter is disabled.
|
static int |
ERROR_CHARACTERISTICS_NOT_FOUND
Thrown when the required DFU service has been found but at least one of the DFU characteristics is absent.
|
static int |
ERROR_CONNECTION_MASK
The flag set when one of
android.bluetooth.BluetoothGattCallback methods was called with status other than android.bluetooth.BluetoothGatt#GATT_SUCCESS . |
static int |
ERROR_DEVICE_DISCONNECTED |
static int |
ERROR_FILE_ERROR
Thrown if service was unable to open the file (
IOException has been thrown). |
static int |
ERROR_FILE_INVALID
Thrown then input file is not a valid HEX or ZIP file.
|
static int |
ERROR_FILE_IO_EXCEPTION
Thrown when
IOException occurred when reading from file. |
static int |
ERROR_FILE_NOT_FOUND |
static int |
ERROR_FILE_TYPE_UNSUPPORTED
Thrown when the the service does not support given type or mime-type.
|
static int |
ERROR_INVALID_RESPONSE
Thrown when unknown response has been obtained from the target.
|
static int |
ERROR_MASK
If this bit is set than the progress value indicates an error.
|
static int |
ERROR_REMOTE_MASK
Flag set then the DFU target returned a DFU error.
|
static int |
ERROR_SERVICE_DISCOVERY_NOT_STARTED
Error thrown then
gatt.discoverServices(); returns false. |
static int |
ERROR_SERVICE_NOT_FOUND
Thrown when the service discovery has finished but the DFU service has not been found.
|
static java.lang.String |
EXTRA_ACTION
The action extra.
|
static java.lang.String |
EXTRA_AVG_SPEED_B_PER_MS
The average upload speed in bytes/millisecond for the current part.
|
static java.lang.String |
EXTRA_DATA
An extra field with progress and error information used in broadcast events.
|
static java.lang.String |
EXTRA_DEVICE_ADDRESS
The address of the device to update.
|
static java.lang.String |
EXTRA_DEVICE_NAME
The optional device name.
|
static java.lang.String |
EXTRA_FILE_MIME_TYPE
The input file mime-type.
|
static java.lang.String |
EXTRA_FILE_PATH
A path to the file with the new firmware.
|
static java.lang.String |
EXTRA_FILE_TYPE
This optional extra parameter may contain a file type.
|
static java.lang.String |
EXTRA_FILE_URI
See
EXTRA_FILE_PATH for details. |
static java.lang.String |
EXTRA_INIT_FILE_PATH
The Init packet URI.
|
static java.lang.String |
EXTRA_INIT_FILE_URI
The Init packet URI.
|
static java.lang.String |
EXTRA_LOG_LEVEL |
static java.lang.String |
EXTRA_LOG_MESSAGE |
static java.lang.String |
EXTRA_PART_CURRENT
The number of currently transferred part.
|
static java.lang.String |
EXTRA_PARTS_TOTAL
Number of parts in total.
|
static java.lang.String |
EXTRA_PROGRESS
An extra field to send the progress or error information in the DFU notification.
|
static java.lang.String |
EXTRA_RESTORE_BOND
If the new firmware (application) does not share the bond information with the old one, the bond information is lost.
|
static java.lang.String |
EXTRA_SPEED_B_PER_MS
The current upload speed in bytes/millisecond.
|
static int |
LOG_LEVEL_APPLICATION
Log entries level for applications
|
static int |
LOG_LEVEL_DEBUG
Level used just for debugging purposes.
|
static int |
LOG_LEVEL_ERROR
Log entries with very high importance, like errors
|
static int |
LOG_LEVEL_INFO
Default logging level for important entries
|
static int |
LOG_LEVEL_VERBOSE
Log entries with minor importance
|
static int |
LOG_LEVEL_WARNING
Log entries with high importance
|
static java.lang.String |
MIME_TYPE_OCTET_STREAM |
static java.lang.String |
MIME_TYPE_ZIP |
static int |
NOTIFICATION_ID |
static int |
PROGRESS_ABORTED
The upload has been aborted.
|
static int |
PROGRESS_COMPLETED
The connection is successful.
|
static int |
PROGRESS_CONNECTING
Service is connecting to the remote DFU target.
|
static int |
PROGRESS_DISCONNECTING
Service is disconnecting from the DFU target.
|
static int |
PROGRESS_ENABLING_DFU_MODE
Service has triggered a switch to bootloader mode.
|
static int |
PROGRESS_STARTING
Service is enabling notifications and starting transmission.
|
static int |
PROGRESS_VALIDATING
Service is sending validation request to the remote DFU target.
|
static int |
TYPE_APPLICATION
The file contains a new version of Application.
|
static int |
TYPE_AUTO
A ZIP file that combines more than 1 HEX file.
|
static int |
TYPE_BOOTLOADER
The file contains a new version of Bootloader.
|
static int |
TYPE_SOFT_DEVICE
The file contains a new version of Soft Device.
|
Constructor and Description |
---|
DfuBaseService() |
Modifier and Type | Method and Description |
---|---|
protected abstract java.lang.Class<? extends Activity> |
getNotificationTarget()
This method must return the activity class that will be used to create the pending intent used as a content intent in the notification showing the upload progress.
|
void |
onCreate() |
void |
onDestroy() |
protected void |
onHandleIntent(Intent intent) |
public static final java.lang.String EXTRA_DEVICE_ADDRESS
public static final java.lang.String EXTRA_DEVICE_NAME
public static final java.lang.String EXTRA_RESTORE_BOND
If the new firmware (application) does not share the bond information with the old one, the bond information is lost. Set this flag to true
to make the service create new bond with the new application when the upload is done (and remove the old one). When set to false
(default),
the DFU service assumes that the LTK is shared between them. Note: currently it is not possible to remove the old bond without creating a new one so if
your old application supported bonding while the new one does not you have to modify the source code yourself.
In case of updating the soft device the application is always removed so the bond information with it.
Search for occurrences of EXTRA_RESTORE_BOND in this file to check the implementation and get more details.
public static final java.lang.String EXTRA_FILE_PATH
EXTRA_FILE_URI
in the later case.public static final java.lang.String EXTRA_FILE_URI
EXTRA_FILE_PATH
for details.public static final java.lang.String EXTRA_INIT_FILE_PATH
public static final java.lang.String EXTRA_INIT_FILE_URI
public static final java.lang.String EXTRA_FILE_MIME_TYPE
public static final java.lang.String MIME_TYPE_OCTET_STREAM
public static final java.lang.String MIME_TYPE_ZIP
public static final java.lang.String EXTRA_FILE_TYPE
TYPE_SOFT_DEVICE
- only Soft Device updateTYPE_BOOTLOADER
- only Bootloader updateTYPE_APPLICATION
- only application updateTYPE_AUTO
- the file is a ZIP file that may contain more than one HEX/BIN + DAT files. The ZIP file MAY contain only the following files:
softdevice.hex/bin, bootloader.hex/bin, application.hex/bin to determine the type based on its name. At lease one of them MUST be present.
EXTRA_FILE_MIME_TYPE
field is null
or is equal to "application/octet-stream" - the TYPE_APPLICATION
is assumed.EXTRA_FILE_MIME_TYPE
field is equal to "application/zip" - the TYPE_AUTO
is assumed.public static final int TYPE_SOFT_DEVICE
The file contains a new version of Soft Device.
Since DFU Library 7.0 all firmware may contain an Init packet. The Init packet is required if Extended Init Packet is used by the DFU bootloader. The Init packet for the Soft Device must be placed in the [firmware name].dat file as binary file (in the same location).
EXTRA_FILE_TYPE
,
Constant Field Valuespublic static final int TYPE_BOOTLOADER
The file contains a new version of Bootloader.
Since DFU Library 7.0 all firmware may contain an Init packet. The Init packet is required if Extended Init Packet is used by the DFU bootloader. The Init packet for the bootloader must be placed in the [firmware name].dat file as binary file (in the same location).
EXTRA_FILE_TYPE
,
Constant Field Valuespublic static final int TYPE_APPLICATION
The file contains a new version of Application.
Since DFU Library 7.0 all firmware may contain an Init packet. The Init packet is required if Extended Init Packet is used by the DFU bootloader. The Init packet for the application must be placed in the [firmware name].dat file as binary file (in the same location).
EXTRA_FILE_TYPE
,
Constant Field Valuespublic static final int TYPE_AUTO
A ZIP file that combines more than 1 HEX file. The names of files in the ZIP must be: softdevice.hex (or .bin), bootloader.hex (or .bin), application.hex (or .bin) in order to be read correctly. In the DFU version 2 the Soft Device and Bootloader may be sent together. In case of additional application file included, the service will try to send Soft Device, Bootloader and Application together (not supported by DFU v.2) and if it fails, send first SD+BL, reconnect and send application.
Since the DFU Library 7.0 all firmware may contain an Init packet. The Init packet is required if Extended Init Packet is used by the DFU bootloader. The Init packet for the Soft Device and Bootloader must be in 'system.dat' file while for the application in the 'application.dat' file (included in the ZIP). The CRC in the 'system.dat' must be a CRC of both BIN contents if both a Soft Device and a Bootloader is present.
EXTRA_FILE_TYPE
,
Constant Field Valuespublic static final java.lang.String EXTRA_DATA
public static final java.lang.String EXTRA_PROGRESS
ERROR_MASK
if initialization error occurredERROR_REMOTE_MASK
if remote DFU target returned an errorERROR_CONNECTION_MASK
if connection error occurred (f.e. GATT error (133) or Internal GATT Error (129))boolean error = progressValue >= DfuBaseService.ERROR_MASK;
public static final java.lang.String EXTRA_PART_CURRENT
EXTRA_PARTS_TOTAL
,
Constant Field Valuespublic static final java.lang.String EXTRA_PARTS_TOTAL
EXTRA_PART_CURRENT
,
Constant Field Valuespublic static final java.lang.String EXTRA_SPEED_B_PER_MS
public static final java.lang.String EXTRA_AVG_SPEED_B_PER_MS
public static final java.lang.String BROADCAST_PROGRESS
EXTRA_DATA
- the progress value (percentage 0-100) or:
EXTRA_DEVICE_ADDRESS
- the target device addressEXTRA_PART_CURRENT
- the number of currently transmitted partEXTRA_PARTS_TOTAL
- total number of parts that are being sent, f.e. if a ZIP file contains a Soft Device, a Bootloader and an Application,
the SoftDevice and Bootloader will be send together as one part. Then the service will disconnect and reconnect to the new Bootloader and send the
application as part number two.EXTRA_SPEED_B_PER_MS
- current speed in bytes/millisecond as floatEXTRA_AVG_SPEED_B_PER_MS
- the average transmission speed in bytes/millisecond as floatpublic static final int PROGRESS_CONNECTING
public static final int PROGRESS_STARTING
public static final int PROGRESS_ENABLING_DFU_MODE
public static final int PROGRESS_VALIDATING
public static final int PROGRESS_DISCONNECTING
public static final int PROGRESS_COMPLETED
public static final int PROGRESS_ABORTED
public static final java.lang.String BROADCAST_ERROR
EXTRA_DATA
- the error number. Use GattError.parse(int)
to get String representationEXTRA_DEVICE_ADDRESS
- the target device addresspublic static final int ERROR_MASK
GattError.parse(int)
to obtain error name.public static final int ERROR_DEVICE_DISCONNECTED
public static final int ERROR_FILE_NOT_FOUND
public static final int ERROR_FILE_ERROR
IOException
has been thrown).public static final int ERROR_FILE_INVALID
public static final int ERROR_FILE_IO_EXCEPTION
IOException
occurred when reading from file.public static final int ERROR_SERVICE_DISCOVERY_NOT_STARTED
gatt.discoverServices();
returns false.public static final int ERROR_SERVICE_NOT_FOUND
public static final int ERROR_CHARACTERISTICS_NOT_FOUND
public static final int ERROR_INVALID_RESPONSE
public static final int ERROR_FILE_TYPE_UNSUPPORTED
public static final int ERROR_BLUETOOTH_DISABLED
public static final int ERROR_REMOTE_MASK
public static final int ERROR_CONNECTION_MASK
android.bluetooth.BluetoothGattCallback
methods was called with status other than android.bluetooth.BluetoothGatt#GATT_SUCCESS
.public static final java.lang.String BROADCAST_LOG
EXTRA_LOG_LEVEL
- The log level, one of following: LOG_LEVEL_DEBUG
, LOG_LEVEL_VERBOSE
, LOG_LEVEL_INFO
,
LOG_LEVEL_APPLICATION
, LOG_LEVEL_WARNING
, LOG_LEVEL_ERROR
EXTRA_LOG_MESSAGE
public static final java.lang.String EXTRA_LOG_MESSAGE
public static final java.lang.String EXTRA_LOG_LEVEL
public static final int LOG_LEVEL_DEBUG
public static final int LOG_LEVEL_VERBOSE
public static final int LOG_LEVEL_INFO
public static final int LOG_LEVEL_APPLICATION
public static final int LOG_LEVEL_WARNING
public static final int LOG_LEVEL_ERROR
public static final java.lang.String BROADCAST_ACTION
EXTRA_ACTION
extra to pass the action.public static final java.lang.String EXTRA_ACTION
ACTION_PAUSE
, ACTION_RESUME
, ACTION_ABORT
.public static final int ACTION_PAUSE
ACTION_RESUME
or ACTION_ABORT
.public static final int ACTION_RESUME
ACTION_PAUSE
.public static final int ACTION_ABORT
BROADCAST_ACTION
with extra EXTRA_ACTION
set to this value the DFU bootloader will restore the old application
(if there was already an application). Be aware that uploading the Soft Device will erase the application in order to make space in the memory.
In case there is no application, or the application has been removed, the DFU bootloader will be started and user may try to send the application again.
The bootloader may advertise with the address incremented by 1 to prevent caching services.public static final int DFU_STATUS_SUCCESS
public static final int DFU_STATUS_INVALID_STATE
public static final int DFU_STATUS_NOT_SUPPORTED
public static final int DFU_STATUS_DATA_SIZE_EXCEEDS_LIMIT
public static final int DFU_STATUS_CRC_ERROR
public static final int DFU_STATUS_OPERATION_FAILED
public static final int NOTIFICATION_ID
public void onCreate()
public void onDestroy()
protected void onHandleIntent(Intent intent)
protected abstract java.lang.Class<? extends Activity> getNotificationTarget()
android.content.Intent#FLAG_ACTIVITY_NEW_TASK
flag and the following extras:
EXTRA_DEVICE_ADDRESS
- target device addressEXTRA_DEVICE_NAME
- target device nameEXTRA_PROGRESS
- the connection state (values < 0)*, current progress (0-100) or error number if ERROR_MASK
bit set.
__________
* - connection state constants: