iOS CoreBluetooth: startAdvertising() Error advertising static data
The error parameter in the method:
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?)
Reports the error:
Error Domain=CBErrorDomain Code=1 "One or more parameters were invalid." UserInfo={NSLocalizedDescription=One or more parameters were invalid.}
Issue was needing to pass an array of UUIDs and also use the CBUUID(string:)
type rather than UUID(uuidString:)
. Each issue reproduces the error independently.
struct BluetoothPeripheral {
let localName: String
let uuid: String
func peripheralData() -> [String : Any] {
return [
CBAdvertisementDataLocalNameKey : localName,
CBAdvertisementDataServiceUUIDsKey : [CBUUID(string: uuid)],
]
}
}
The value for the CBAdvertisementDataServiceUUIDsKey
in the dictionary passed to startAdvertising
is an array of CBUUID
objects, but you are only passing a single CBUUID
. Once I changed it to an array your code worked.
func peripheralManager(peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?) {
if (error != nil) {
print("PerformerUtility.publishServices() returned error: \(error!.localizedDescription)")
print("Providing the reason for failure: \(error!.localizedFailureReason)")
}
else {
peripheralManager?.startAdvertising([CBAdvertisementDataServiceUUIDsKey : [service.UUID]])
}
}