我想宣傳靜態數據。我在iOS上使用Swift 2.2.1和CoreBluetooth。我的應用程序建立藍牙服務及其相應的特點,然後調用startAdvertising()和peripheralManagerDidStartAdvertising90回調返回此錯誤:iOS CoreBluetooth:startAdvertising()廣告靜態數據的錯誤
peripheralManagerDidStartAdvertising encountered an error. // Mine
One or more parameters were invalid. // ...from Apple CoreBluetooth
nil // ... return value from Apple CoreBluetooth callback
我是比較新的雨燕和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 - - - - - - -
我真的很感謝您提供的任何幫助。
最好的問候,
邁克爾
你在哪裏調用'configureUtilityForIdentity'? – Paulw11
我創建了一個簡單的單視圖應用程序,並在viewDidLoad()中調用了configureUtilityForIdentity() –
但是你在'init'中創建了外設管理器,所以在你調用'configureUtility'之前, ? – Paulw11