iOS CoreBluetooth:startAdvertising() 广播静态数据时出错
iOS CoreBluetooth: startAdvertising() Error advertising static data
我想宣传静态数据。我在 iOS 上使用 Swift 2.2.1 和 CoreBluetooth。我的应用构建蓝牙 Services 及其相应的 Characteristics,然后调用 startAdvertising(),然后 peripheralManagerDidStartAdvertising90回调returns这个错误:
peripheralManagerDidStartAdvertising encountered an error. // Mine
One or more parameters were invalid. // ...from Apple CoreBluetooth
nil // ... return value from Apple CoreBluetooth callback
我对 Swift 和 iOS 开发比较陌生,所以我猜我在做一些愚蠢的事情,但到目前为止我还不知道是什么。
我会尝试将事情提炼出来以供更有经验的人使用。
- - - - - - pointutility.swift - - - - - - -
// This is a code excerpt and probably won't compile.
// UUID for the one peripheral service, declared outside the class:
var peripheralServiceUUID = CBUUID(string: "9BC1F0DC-F4CB-4159-BD38-7375CD0DD545")
// UUID for one characteristic of the service above, declared outside the class:
var nameCharacteristicUUID = CBUUID(string: "9BC1F0DC-F4CB-4159-BD38-7B74CD0CD546")
class PointUtility: NSObject, CBPeripheralManagerDelegate {
var peripheralManager:CBPeripheralManager?
var bluetoothServices:CBMutableService?
var nameCharacteristic:CBMutableCharacteristic?
override init() {
super.init()
peripheralManager = CBPeripheralManager(delegate:self, queue:nil)
bluetoothServices = CBMutableService(type: peripheralServiceUUID, primary: true)
}
func configureUtilityForIdentity(identity:Point!) {
var characteristicsArray:[CBCharacteristic] = []
myIdentity = identity
if (identity.name != nil) {
nameCharacteristic =
CBMutableCharacteristic(type: nameCharacteristicUUID,
properties: (CBCharacteristicProperties.Read),
value: myIdentity?.name?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false),
permissions: CBAttributePermissions.Readable)
characteristicsArray.append(nameCharacteristic!)
}
// more characteristics built here and added to the characteristicsArray[]...
// ... then all are added to the CBMutableService at the bottom...
bluetoothServices?.characteristics = characteristicsArray as [CBCharacteristic]
}
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
switch (peripheral.state) {
case .PoweredOn:
print("Current Bluetooth State: PoweredOn")
publishServices(bluetoothServices)
break;
case .PoweredOff:
print("Current Bluetooth State: PoweredOff")
break;
case .Resetting:
print("Current Bluetooth State: Resetting")
break;
case .Unauthorized:
print("Current Bluetooth State: Unauthorized")
case .Unknown:
print("Current Bluetooth State: Unknown")
break;
case .Unsupported:
/
print("Current Bluetooth State: Unsupported")
break;
}
}
func publishServices(newService:CBMutableService!) {
peripheralManager?.addService(newService)
}
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])
}
}
func peripheralManagerDidStartAdvertising(peripheral: CBPeripheralManager,
error: NSError?) {
if (error != nil) {
print("peripheralManagerDidStartAdvertising encountered an error.")
print(error!.localizedDescription) // Error: One or more parameters were invalid
print(error!.localizedFailureReason) // Error: nil
}
print ("Debug: peripheralManagerDidStartAdvertising()")
}
}
- - - - - - pointutility.swift - - - - - - -
非常感谢提供的任何帮助。
此致,
迈克尔
传递给 startAdvertising
的字典中 CBAdvertisementDataServiceUUIDsKey
的值是一个 CBUUID
对象数组,但您只传递了一个 CBUUID
。一旦我将它更改为数组,您的代码就可以工作了。
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]])
}
}
一些小问题;我有一个额外的参数。
peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
我在 peripheralManagerDidUpdateState 中有这段代码,就在最上面。
if (peripheral.state != .PoweredOn) {
return;
}
我在同一个方法中调用它,当然假设它是打开的。
transferCharacteristic = CBMutableCharacteristic(type: cbtransfer, properties: .Notify, value: nil, permissions: .Readable)
transferService = CBMutableService(type: cbservice, primary: true)
transferService.characteristics = [transferCharacteristic]
最后我这样做了:
peripheralManager!.addService(transferService)
peripheralManager!.startAdvertising([CBAdvertisementDataServiceUUIDsKey :[cbservice]])
但是请注意;我没有附加特征;我使用您在此处看到的语法,附加不起作用。由于我不清楚的原因 Swift 说它没问题,但它在语法上很好,但 BLE 不喜欢它。
func publishServices(newService:CBMutableService!) {
peripheralManager?.addService(newService)
}
检查您的代码并进行更改;它应该工作。
方法中的错误参数:
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?)
报错:
Error Domain=CBErrorDomain Code=1 "One or more parameters were invalid." UserInfo={NSLocalizedDescription=One or more parameters were invalid.}
问题是需要传递 UUID 的 数组 并且还使用 CBUUID(string:)
类型而不是 UUID(uuidString:)
。每个问题都会独立重现错误。
struct BluetoothPeripheral {
let localName: String
let uuid: String
func peripheralData() -> [String : Any] {
return [
CBAdvertisementDataLocalNameKey : localName,
CBAdvertisementDataServiceUUIDsKey : [CBUUID(string: uuid)],
]
}
}
我想宣传静态数据。我在 iOS 上使用 Swift 2.2.1 和 CoreBluetooth。我的应用构建蓝牙 Services 及其相应的 Characteristics,然后调用 startAdvertising(),然后 peripheralManagerDidStartAdvertising90回调returns这个错误:
peripheralManagerDidStartAdvertising encountered an error. // Mine
One or more parameters were invalid. // ...from Apple CoreBluetooth
nil // ... return value from Apple CoreBluetooth callback
我对 Swift 和 iOS 开发比较陌生,所以我猜我在做一些愚蠢的事情,但到目前为止我还不知道是什么。
我会尝试将事情提炼出来以供更有经验的人使用。
- - - - - - pointutility.swift - - - - - - -
// This is a code excerpt and probably won't compile.
// UUID for the one peripheral service, declared outside the class:
var peripheralServiceUUID = CBUUID(string: "9BC1F0DC-F4CB-4159-BD38-7375CD0DD545")
// UUID for one characteristic of the service above, declared outside the class:
var nameCharacteristicUUID = CBUUID(string: "9BC1F0DC-F4CB-4159-BD38-7B74CD0CD546")
class PointUtility: NSObject, CBPeripheralManagerDelegate {
var peripheralManager:CBPeripheralManager?
var bluetoothServices:CBMutableService?
var nameCharacteristic:CBMutableCharacteristic?
override init() {
super.init()
peripheralManager = CBPeripheralManager(delegate:self, queue:nil)
bluetoothServices = CBMutableService(type: peripheralServiceUUID, primary: true)
}
func configureUtilityForIdentity(identity:Point!) {
var characteristicsArray:[CBCharacteristic] = []
myIdentity = identity
if (identity.name != nil) {
nameCharacteristic =
CBMutableCharacteristic(type: nameCharacteristicUUID,
properties: (CBCharacteristicProperties.Read),
value: myIdentity?.name?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false),
permissions: CBAttributePermissions.Readable)
characteristicsArray.append(nameCharacteristic!)
}
// more characteristics built here and added to the characteristicsArray[]...
// ... then all are added to the CBMutableService at the bottom...
bluetoothServices?.characteristics = characteristicsArray as [CBCharacteristic]
}
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
switch (peripheral.state) {
case .PoweredOn:
print("Current Bluetooth State: PoweredOn")
publishServices(bluetoothServices)
break;
case .PoweredOff:
print("Current Bluetooth State: PoweredOff")
break;
case .Resetting:
print("Current Bluetooth State: Resetting")
break;
case .Unauthorized:
print("Current Bluetooth State: Unauthorized")
case .Unknown:
print("Current Bluetooth State: Unknown")
break;
case .Unsupported:
/
print("Current Bluetooth State: Unsupported")
break;
}
}
func publishServices(newService:CBMutableService!) {
peripheralManager?.addService(newService)
}
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])
}
}
func peripheralManagerDidStartAdvertising(peripheral: CBPeripheralManager,
error: NSError?) {
if (error != nil) {
print("peripheralManagerDidStartAdvertising encountered an error.")
print(error!.localizedDescription) // Error: One or more parameters were invalid
print(error!.localizedFailureReason) // Error: nil
}
print ("Debug: peripheralManagerDidStartAdvertising()")
}
}
- - - - - - pointutility.swift - - - - - - -
非常感谢提供的任何帮助。
此致,
迈克尔
传递给 startAdvertising
的字典中 CBAdvertisementDataServiceUUIDsKey
的值是一个 CBUUID
对象数组,但您只传递了一个 CBUUID
。一旦我将它更改为数组,您的代码就可以工作了。
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]])
}
}
一些小问题;我有一个额外的参数。
peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
我在 peripheralManagerDidUpdateState 中有这段代码,就在最上面。
if (peripheral.state != .PoweredOn) {
return;
}
我在同一个方法中调用它,当然假设它是打开的。
transferCharacteristic = CBMutableCharacteristic(type: cbtransfer, properties: .Notify, value: nil, permissions: .Readable)
transferService = CBMutableService(type: cbservice, primary: true)
transferService.characteristics = [transferCharacteristic]
最后我这样做了:
peripheralManager!.addService(transferService)
peripheralManager!.startAdvertising([CBAdvertisementDataServiceUUIDsKey :[cbservice]])
但是请注意;我没有附加特征;我使用您在此处看到的语法,附加不起作用。由于我不清楚的原因 Swift 说它没问题,但它在语法上很好,但 BLE 不喜欢它。
func publishServices(newService:CBMutableService!) {
peripheralManager?.addService(newService)
}
检查您的代码并进行更改;它应该工作。
方法中的错误参数:
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?)
报错:
Error Domain=CBErrorDomain Code=1 "One or more parameters were invalid." UserInfo={NSLocalizedDescription=One or more parameters were invalid.}
问题是需要传递 UUID 的 数组 并且还使用 CBUUID(string:)
类型而不是 UUID(uuidString:)
。每个问题都会独立重现错误。
struct BluetoothPeripheral {
let localName: String
let uuid: String
func peripheralData() -> [String : Any] {
return [
CBAdvertisementDataLocalNameKey : localName,
CBAdvertisementDataServiceUUIDsKey : [CBUUID(string: uuid)],
]
}
}