2016-03-08 12 views
3

我想宣傳靜態數據。我在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 - - - - - - - 

我真的很感謝您提供的任何幫助。

最好的問候,

邁克爾

+0

你在哪裏調用'configureUtilityForIdentity'? – Paulw11

+1

我創建了一個簡單的單視圖應用程序,並在viewDidLoad()中調用了configureUtilityForIdentity() –

+0

但是你在'init'中創建了外設管理器,所以在你調用'configureUtility'之前, ? – Paulw11

回答

3

在傳遞給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]]) 
    } 
} 
+0

有趣,所以append爲你工作,我再次檢查我的代碼,當我第一次使用它時,它似乎破裂了,也許他們已經修復它,我應該工作 – user3069232

+0

這是我爲了測試你的代碼所做的類。我做了一些其他更改,主要是使用!而不是?但它應該是相同的https://gist.github.com/paulw11/b2b33d7ff4c86818ac4d – Paulw11

+0

Paulo11可以給我你的電子郵件地址?我剛剛閱讀你的個人資料,並認爲這是很好的獲得你的細節;我在1989年畢業於CS上個世紀,我很遺憾我還沒有在商店有任何應用程序。 – user3069232

0

有一些小問題;我有一個額外的參數。

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) 
} 

查看您的代碼並進行更改;它應該工作。

+0

[給CBPeripheralManager增加了額外的參數] [改變'CBMutableCharacteristic'屬性來接受'.Notify' < - 因爲我不期望驗證,所以更好理解。] [更正'startAdvertising'來接受**數組** 'CBUUID'對象< - 這是我的錯誤,yippee!]問題:你怎麼知道BLE不喜歡追加特性?我看到你正在創建'CBMutableCharacteristic',其中_nil_作爲值參數,告訴BLE數據是_dynamic_,而不是_static_。也許這跟它有關係? –

+0

我知道,因爲我試過了,它不起作用;但也許是因爲我傳遞了錯誤的參數,我仔細檢查。 – user3069232