public final class DfuServiceInitiator extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_MBR_SIZE |
static int |
DEFAULT_PRN_VALUE |
static int |
SCOPE_APPLICATION
Constant used to narrow the scope of the update to application only.
|
static int |
SCOPE_SYSTEM_COMPONENTS
Constant used to narrow the scope of the update to system components (SD+BL) only.
|
| Constructor and Description |
|---|
DfuServiceInitiator(String deviceAddress)
Creates the builder.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
createDfuNotificationChannel(Context context) |
DfuServiceInitiator |
disableMtuRequest()
Disables MTU request.
|
DfuServiceInitiator |
disableResume()
This options allows to disable the resume feature in Secure DFU.
|
DfuServiceInitiator |
setBinOrHex(int fileType,
int rawResId)
Deprecated.
|
DfuServiceInitiator |
setBinOrHex(int fileType,
String path)
Deprecated.
|
DfuServiceInitiator |
setBinOrHex(int fileType,
Uri uri)
Deprecated.
|
DfuServiceInitiator |
setBinOrHex(int fileType,
Uri uri,
String path)
Deprecated.
The Distribution packet (ZIP) should be used for DFU Bootloader version 0.5 or newer
|
DfuServiceInitiator |
setCurrentMtu(int mtu)
Sets the current MTU value.
|
DfuServiceInitiator |
setCustomUuidsForButtonlessDfuWithBondSharing(UUID buttonlessDfuServiceUuid,
UUID buttonlessDfuControlPointUuid)
Sets custom UUIDs for the Buttonless DFU Service from SDK 14 (or later).
|
DfuServiceInitiator |
setCustomUuidsForButtonlessDfuWithoutBondSharing(UUID buttonlessDfuServiceUuid,
UUID buttonlessDfuControlPointUuid)
Sets custom UUIDs for the Buttonless DFU Service from SDK 13.
|
DfuServiceInitiator |
setCustomUuidsForExperimentalButtonlessDfu(UUID buttonlessDfuServiceUuid,
UUID buttonlessDfuControlPointUuid)
Sets custom UUIDs for the experimental Buttonless DFU Service from SDK 12.x.
|
DfuServiceInitiator |
setCustomUuidsForLegacyDfu(UUID dfuServiceUuid,
UUID dfuControlPointUuid,
UUID dfuPacketUuid,
UUID dfuVersionUuid)
Sets custom UUIDs for Legacy DFU and Legacy Buttonless DFU.
|
DfuServiceInitiator |
setCustomUuidsForSecureDfu(UUID dfuServiceUuid,
UUID dfuControlPointUuid,
UUID dfuPacketUuid)
Sets custom UUIDs for Secure DFU.
|
DfuServiceInitiator |
setDeviceName(String name)
Sets the device name.
|
DfuServiceInitiator |
setDisableNotification(boolean disableNotification)
Sets whether the progress notification in the status bar should be disabled.
|
DfuServiceInitiator |
setForceDfu(boolean force)
Setting force DFU to true will prevent from jumping to the DFU Bootloader
mode in case there is no DFU Version characteristic (Legacy DFU only!).
|
DfuServiceInitiator |
setForeground(boolean foreground)
Sets whether the DFU service should be started as a foreground service.
|
DfuServiceInitiator |
setInitFile(int initFileResId)
Deprecated.
|
DfuServiceInitiator |
setInitFile(String initFilePath)
Deprecated.
|
DfuServiceInitiator |
setInitFile(Uri initFileUri)
Deprecated.
|
DfuServiceInitiator |
setInitFile(Uri initFileUri,
String initFilePath)
Deprecated.
|
DfuServiceInitiator |
setKeepBond(boolean keepBond)
Sets whether the bond information should be preserver after flashing new application.
|
DfuServiceInitiator |
setMbrSize(int mbrSize)
This method sets the size of an MBR (Master Boot Record).
|
DfuServiceInitiator |
setMtu(int mtu)
Sets the Maximum Transfer Unit (MTU) value that the Secure DFU service will try to request
before performing DFU.
|
DfuServiceInitiator |
setNumberOfRetries(int max)
Sets the number of retries that the DFU service will use to complete DFU.
|
DfuServiceInitiator |
setPacketsReceiptNotificationsEnabled(boolean enabled)
Enables or disables the Packet Receipt Notification (PRN) procedure.
|
DfuServiceInitiator |
setPacketsReceiptNotificationsValue(int number)
If Packet Receipt Notification procedure is enabled, this method sets number of packets to
be sent before receiving a PRN.
|
DfuServiceInitiator |
setRestoreBond(boolean restoreBond)
Sets whether the bond should be created after the DFU is complete.
|
DfuServiceInitiator |
setScope(int scope)
This method allows to narrow the update to selected parts from the ZIP, for example
to allow only application update from a ZIP file that has SD+BL+App.
|
DfuServiceInitiator |
setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(boolean enable)
Set this flag to true to enable experimental buttonless feature in Secure DFU.
|
DfuServiceInitiator |
setZip(int rawResId)
Sets the resource ID of the Distribution packet (ZIP) or the a ZIP file matching the
deprecated naming convention.
|
DfuServiceInitiator |
setZip(String path)
Sets the path to the Distribution packet (ZIP) or the a ZIP file matching the deprecated naming
convention.
|
DfuServiceInitiator |
setZip(Uri uri)
Sets the URI to the Distribution packet (ZIP) or to a ZIP file matching the deprecated naming
convention.
|
DfuServiceInitiator |
setZip(Uri uri,
String path)
Sets the URI or path of the ZIP file.
|
DfuServiceController |
start(Context context,
Class<? extends DfuBaseService> service)
Starts the DFU service.
|
public static final int DEFAULT_PRN_VALUE
public static final int DEFAULT_MBR_SIZE
public static final int SCOPE_SYSTEM_COMPONENTS
public static final int SCOPE_APPLICATION
public DfuServiceInitiator(String deviceAddress)
deviceAddress - the target device device addresspublic DfuServiceInitiator setDeviceName(String name)
R.string.dfu_unknown_name
value will be used.name - the device name (optional)public DfuServiceInitiator setDisableNotification(boolean disableNotification)
disableNotification - whether to disable the notificationpublic DfuServiceInitiator setForeground(boolean foreground)
foreground - whether the service should be started in foreground state.public DfuServiceInitiator setKeepBond(boolean keepBond)
DfuBaseService.EXTRA_KEEP_BOND for more information regarding
requirements. Remember that currently updating the Soft Device will remove the bond
information.
This flag is ignored when Secure DFU Buttonless Service is used. It will keep or remove the bond depending on the Buttonless service type.
keepBond - whether the bond information should be preserved in the new application.public DfuServiceInitiator setRestoreBond(boolean restoreBond)
DfuBaseService.EXTRA_RESTORE_BOND for more information regarding
requirements.
This flag is ignored when Secure DFU Buttonless Service is used. It will keep or will not restore the bond depending on the Buttonless service type.
restoreBond - whether the bond should be created after the DFU is complete.public DfuServiceInitiator setPacketsReceiptNotificationsEnabled(boolean enabled)
By default the PRNs are disabled on devices with Android Marshmallow or newer and enabled on older ones.
enabled - true to enabled PRNs, false to disableDfuSettingsConstants.SETTINGS_PACKET_RECEIPT_NOTIFICATION_ENABLEDpublic DfuServiceInitiator setPacketsReceiptNotificationsValue(int number)
If the value given is equal to 0, the DEFAULT_PRN_VALUE will be used instead.
To disable PRNs use setPacketsReceiptNotificationsEnabled(boolean).
number - number of packets to be sent before receiving a PRN. Defaulted when set to 0.setPacketsReceiptNotificationsEnabled(boolean),
DfuSettingsConstants.SETTINGS_NUMBER_OF_PACKETSpublic DfuServiceInitiator setForceDfu(boolean force)
If the DFU Version characteristic exists, the information whether to begin DFU operation, or jump to bootloader, is taken from that characteristic's value. The value returned equal to 0x0100 (read as: minor=1, major=0, or version 0.1) means that the device is in the application mode and buttonless jump to DFU Bootloader is supported.
However, if there is no DFU Version characteristic, a device may support only Application update (version from SDK 4.3.0), may support Soft Device, Bootloader and Application update but without buttonless jump to bootloader (SDK 6.0.0) or with buttonless jump (SDK 6.1.0).
In the last case, the DFU Library determines whether the device is in application mode or in
DFU Bootloader mode by counting number of services: if no DFU Service found - device is in
app mode and does not support buttonless jump, if the DFU Service is the only service found
(except General Access and General Attribute services) - it assumes it is in DFU Bootloader
mode and may start DFU immediately, if there is at least one service except DFU Service -
the device is in application mode and supports buttonless jump. In the last case, if you
want to perform DFU operation without jumping - call the setForceDfu(boolean)
method with parameter equal to true.
This method is ignored in Secure DFU.
force - true to ensure the DFU will start if there is no DFU Version characteristic
(Legacy DFU only)DfuSettingsConstants.SETTINGS_ASSUME_DFU_NODEpublic DfuServiceInitiator disableResume()
It is ignored when Legacy DFU is used.
This feature seems to help in some cases: #71.
public DfuServiceInitiator setNumberOfRetries(int max)
If the given value is greater than 0, the service will restart itself at most max
times in case of an undesired disconnection during DFU operation. This attempt counter
is independent from another counter, for reconnection attempts, which is equal to 3.
The latter one will be used when connection will fail with an error (possible packet
collision or any other reason). After successful connection, the reconnection counter is
reset, while the retry counter is cleared after a DFU finishes with success.
The service will not try to retry DFU in case of any other error, for instance an error sent from the target device.
max - Maximum number of retires to complete DFU. Usually around 2.public DfuServiceInitiator setMtu(int mtu)
The higher the MTU, the faster the data may be sent.
If you encounter problems with high MTU, you may lower the required value using this method. See: https://github.com/NordicSemiconductor/Android-DFU-Library/issues/111
To disable requesting MTU, use value 0, or disableMtuRequest().
Note: Higher (that is greater then 23) MTUs are supported on Lollipop or newer Android devices, and on DFU bootloader from SDK 15 or newer (Secure DFU only).
mtu - the MTU that wil be requested, 0 to disable MTU request.public DfuServiceInitiator setCurrentMtu(int mtu)
By default value 23 is used for compatibility reasons.
Higher MTU values were supported since SDK 15.0.
mtu - the MTU value received in
BluetoothGattCallback.onMtuChanged(BluetoothGatt, int, int) or
BluetoothGattServerCallback.onMtuChanged(BluetoothDevice, int).public DfuServiceInitiator disableMtuRequest()
setMtu(int)public DfuServiceInitiator setScope(int scope)
scope - the update scope, one of SCOPE_SYSTEM_COMPONENTS or
SCOPE_APPLICATION.public DfuServiceInitiator setMbrSize(int mbrSize)
The MBR size is important for the HEX parser, which has to cut it from the Soft Device's HEX before sending it to the DFU target. The MBR can't be updated using DFU, and the bootloader expects only the Soft Device bytes. Usually, the Soft Device HEX provided by Nordic contains an MBR at addresses 0x0000 to 0x1000. 0x1000 is the default size of MBR which will be used.
If you have a HEX file which address start from 0 and want to send the whole BIN content from it, you have to set the MBR size to 0, otherwise first 4096 bytes will be cut off.
The value set here will not be used if the DfuSettingsConstants.SETTINGS_MBR_SIZE
is set in Shared Preferences.
mbrSize - the MBR size in bytes. Defaults to 4096 (0x1000) bytes.DfuSettingsConstants.SETTINGS_MBR_SIZEpublic DfuServiceInitiator setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(boolean enable)
Please, read the information below before setting it to true.
In the SDK 12.x the Buttonless DFU feature for Secure DFU was experimental. It is NOT recommended to use it: it was not properly tested, had implementation bugs (e.g. link) and does not required encryption and therefore may lead to DOS attack (anyone can use it to switch the device to bootloader mode). However, as there is no other way to trigger bootloader mode on devices without a button, this DFU Library supports this service, but the feature must be explicitly enabled here. Be aware, that setting this flag to false will no protect your devices from this kind of attacks, as an attacker may use another app for that purpose. To be sure your device is secure remove this experimental service from your device.
Spec:
Buttonless DFU Service UUID: 8E400001-F315-4F60-9FB8-838830DAEA50
Buttonless DFU characteristic UUID: 8E400001-F315-4F60-9FB8-838830DAEA50 (the same)
Enter Bootloader Op Code: 0x01
Correct return value: 0x20-01-01 , where:
0x20 - Response Op Code
0x01 - Request Code
0x01 - Success
The device should disconnect and restart in DFU mode after sending the notification.
The Buttonless service has changed in SDK 13 and later. Indications are used instead of notifications. Also, Buttonless service for bonded devices has been added. It is recommended to use any of the new services instead.
public DfuServiceInitiator setCustomUuidsForLegacyDfu(UUID dfuServiceUuid, UUID dfuControlPointUuid, UUID dfuPacketUuid, UUID dfuVersionUuid)
null will reset the UUID to the default value.dfuServiceUuid - custom Legacy DFU service UUID or null, if default is to be useddfuControlPointUuid - custom Legacy DFU Control Point characteristic UUID or null,
if default is to be useddfuPacketUuid - custom Legacy DFU Packet characteristic UUID or null, if default is
to be useddfuVersionUuid - custom Legacy DFU Version characteristic UUID or null,
if default is to be used (SDK 7.0 - 11.0 only, set null for earlier SDKs)public DfuServiceInitiator setCustomUuidsForSecureDfu(UUID dfuServiceUuid, UUID dfuControlPointUuid, UUID dfuPacketUuid)
null will reset
the UUID to the default value.dfuServiceUuid - custom Secure DFU service UUID or null, if default is to be useddfuControlPointUuid - custom Secure DFU Control Point characteristic UUID or null,
if default is to be useddfuPacketUuid - custom Secure DFU Packet characteristic UUID or null, if default
is to be usedpublic DfuServiceInitiator setCustomUuidsForExperimentalButtonlessDfu(UUID buttonlessDfuServiceUuid, UUID buttonlessDfuControlPointUuid)
null will reset the UUID to the default value.
Remember to call setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(boolean)
with parameter true if you intent to use this service.
buttonlessDfuServiceUuid - custom Buttonless DFU service UUID or null, if default
is to be usedbuttonlessDfuControlPointUuid - custom Buttonless DFU characteristic UUID or null,
if default is to be usedpublic DfuServiceInitiator setCustomUuidsForButtonlessDfuWithBondSharing(UUID buttonlessDfuServiceUuid, UUID buttonlessDfuControlPointUuid)
null will reset the UUID to the default value.buttonlessDfuServiceUuid - custom Buttonless DFU service UUID or null, if default
is to be usedbuttonlessDfuControlPointUuid - custom Buttonless DFU characteristic UUID or null,
if default is to be usedpublic DfuServiceInitiator setCustomUuidsForButtonlessDfuWithoutBondSharing(UUID buttonlessDfuServiceUuid, UUID buttonlessDfuControlPointUuid)
null will reset the UUID to the default value.buttonlessDfuServiceUuid - custom Buttonless DFU service UUID or null, if default
is to be usedbuttonlessDfuControlPointUuid - custom Buttonless DFU characteristic UUID or null,
if default is to be usedpublic DfuServiceInitiator setZip(Uri uri)
uri - the URI of the filesetZip(String),
setZip(int)public DfuServiceInitiator setZip(String path)
path - path to the filesetZip(Uri),
setZip(int)public DfuServiceInitiator setZip(int rawResId)
rawResId - file's resource IDsetZip(Uri),
setZip(String)public DfuServiceInitiator setZip(Uri uri, String path)
uri - the URI of the filepath - the path of the file@Deprecated public DfuServiceInitiator setBinOrHex(int fileType, Uri uri)
setInitFile(Uri) methods.fileType - the file type, a bit field created from:
DfuBaseService.TYPE_APPLICATION - the Application will be sentDfuBaseService.TYPE_SOFT_DEVICE - he Soft Device will be sentDfuBaseService.TYPE_BOOTLOADER - the Bootloader will be senturi - the URI of the file@Deprecated public DfuServiceInitiator setBinOrHex(int fileType, String path)
setInitFile(String) methods.fileType - see setBinOrHex(int, Uri) for detailspath - path to the file@Deprecated public DfuServiceInitiator setBinOrHex(int fileType, Uri uri, String path)
setInitFile(String) methods.fileType - see setBinOrHex(int, Uri) for detailsuri - the URI of the filepath - path to the file@Deprecated public DfuServiceInitiator setBinOrHex(int fileType, int rawResId)
setInitFile(int) methods.fileType - see setBinOrHex(int, Uri) for detailsrawResId - resource ID@Deprecated public DfuServiceInitiator setInitFile(Uri initFileUri)
initFileUri - the URI of the init file@Deprecated public DfuServiceInitiator setInitFile(String initFilePath)
initFilePath - the path to the init file@Deprecated public DfuServiceInitiator setInitFile(int initFileResId)
initFileResId - the resource ID of the init file@Deprecated public DfuServiceInitiator setInitFile(Uri initFileUri, String initFilePath)
initFileUri - the URI of the init fileinitFilePath - the path of the init filepublic DfuServiceController start(Context context, Class<? extends DfuBaseService> service)
context - the application contextservice - the class derived from the BaseDfuServicepublic static void createDfuNotificationChannel(Context context)