Skip to content

Commit

Permalink
Allow user to specify an alternative advertising name (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlubawy authored and philips77 committed Jun 14, 2019
1 parent 1b7e8d3 commit ec410f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ import CoreBluetooth
For more information read: https://github.com/NordicSemiconductor/IOS-nRF-Connect/issues/16
*/
@objc public var alternativeAdvertisingNameEnabled = true

/**
If `alternativeAdvertisingNameEnabled` is `true` then this specifies the alternative name to use. If nil (default)
then a random name is generated.
*/
@objc public var alternativeAdvertisingName: String? = nil

/**
Set this flag to true to enable experimental buttonless feature in Secure DFU. When the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ internal class SecureDFUPeripheral : BaseCommonDFUPeripheral<SecureDFUExecutor,

/// A flag indicating whether setting alternative advertising name is enabled (SDK 14+) (true by default)
let alternativeAdvertisingNameEnabled: Bool

/// The alternative advertising name to use specified by the user, if nil then use a randomly generated name.
var alternativeAdvertisingName: String? = nil

// MARK: - Peripheral API

Expand All @@ -42,6 +45,7 @@ internal class SecureDFUPeripheral : BaseCommonDFUPeripheral<SecureDFUExecutor,

override init(_ initiator: DFUServiceInitiator, _ logger: LoggerHelper) {
self.alternativeAdvertisingNameEnabled = initiator.alternativeAdvertisingNameEnabled
self.alternativeAdvertisingName = initiator.alternativeAdvertisingName
super.init(initiator, logger)
}

Expand Down Expand Up @@ -73,7 +77,19 @@ internal class SecureDFUPeripheral : BaseCommonDFUPeripheral<SecureDFUExecutor,
func jumpToBootloader() {
jumpingToBootloader = true
newAddressExpected = dfuService!.newAddressExpected
dfuService!.jumpToBootloaderMode(withAlternativeAdvertisingName: alternativeAdvertisingNameEnabled,

var name: String?
if alternativeAdvertisingNameEnabled {
if let userSuppliedName = alternativeAdvertisingName {
// Use the user supplied name
name = userSuppliedName
} else {
// Generate a random 8-character long name
name = String(format: "Dfu%05d", arc4random_uniform(100000))
}
}

dfuService!.jumpToBootloaderMode(withAlternativeAdvertisingName: name,
// onSuccess the device gets disconnected and centralManager(_:didDisconnectPeripheral:error) will be called
onError: { (error, message) in
self.jumpingToBootloader = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,14 @@ import CoreBluetooth
- parameter report: Method called when an error occurred.
*/
func jumpToBootloaderMode(withAlternativeAdvertisingName rename: Bool, onError report: @escaping ErrorCallback) {
func jumpToBootloaderMode(withAlternativeAdvertisingName name: String?, onError report: @escaping ErrorCallback) {
if !aborted {
func enterBootloader() {
self.buttonlessDfuCharacteristic!.send(ButtonlessDFURequest.enterBootloader, onSuccess: nil, onError: report)
}

// If the device may support setting alternative advertising name in the bootloader mode, try it
if rename && buttonlessDfuCharacteristic!.maySupportSettingName {
// Generate a random 8-character long name
let name = String(format: "Dfu%05d", arc4random_uniform(100000))

if let name = name, buttonlessDfuCharacteristic!.maySupportSettingName {
logger.v("Trying setting bootloader name to \(name)")
buttonlessDfuCharacteristic!.send(ButtonlessDFURequest.set(name: name), onSuccess: {
// Success. The buttonless service is from SDK 14.0+. The bootloader, after jumping to it, will advertise with this name.
Expand Down

0 comments on commit ec410f3

Please sign in to comment.