2016-11-08 34 views
0

我是swift的初學者,嘗試開發顯示BLE特性的簡單程序。我證實,我捕捉到的目標特性UUID在segue中丟失的數據(xcode8.0)

myCharacteristicUUID_1 : (
    "Manufacturer Name String", 
    "Model Number String", 
    "Serial Number String", 
    "Hardware Revision String", 
    "Firmware Revision String", 
    "Software Revision String" 
) 

但是打印,在覆蓋FUNC準備(對於SEGUE:UIStoryboardSegue,發件人:否?), 我再次打印檢查的話,我看到的數據是走了如下;

myCharacteristicUUID_2 : (
) 

我可以交出其他類型的值(例如字符串,文本等),但我無法處理從BLE捕獲的數據。有人可以指出我在使用BLE中的數據時必須做些什麼嗎? 以下是我的代碼;

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor targetService: CBService, error: Error?) { 

    if error != nil { 
     print("Characteristic NOT found : \(error)") 

    } 

    let foundCharacteristics = targetService.characteristics 

    for c in foundCharacteristics! as [CBCharacteristic] { 

     myCharacteristicUUID.add(c.uuid) 

    } 

    print("myCharacteristicUUID_1 : \(myCharacteristicUUID)") 

} 

// prepare for segue 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if (segue.identifier == "mySegue") { 

     print("myCharacteristicUUID_2 : \(myCharacteristicUUID)") 

     let mySecondViewController : SecondViewController = segue.destination as! SecondViewController 

     mySecondViewController.relayedFoundCharacteristicUUID = myCharacteristicUUID 

    } 

} 
+0

什麼是myCharacteristicUUID類型?在我看來,功能完成時並沒有被持久化。 –

+0

我將myCharacteristicUUID設置爲NSMutableArray。我幾個星期前開始學習迅速,仍然掙扎... – sandalwalk

+0

你能告訴你如何在代碼中聲明'myCharacteristicUUID'嗎? –

回答

0

這是未被正確讀取的原因。請參閱performSegue的地方。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    selectedService = myService[indexPath.row] as! CBService 
    print("Selected_service : \(selectedService)") 

    self.peripheral.discoverCharacteristics(nil, for:selectedService as CBService!) 

    //performSegue(withIdentifier: "mySegue", sender: nil) 
    // I used to performed segue here. This was the reason myCharacterisitcUUID was empty. 


} 

// find the characteristics for the targetService 

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor targetService: CBService, error: Error?) { 

    if error != nil { 
     print("Characteristic NOT found : \(error)") 

    } 

    foundCharacteristics = targetService.characteristics! 

    for c in foundCharacteristics as [CBCharacteristic] { 

     self.myCharacteristicUUID.add(c.uuid) 

    } 
    // segue should be performed here ! This change worked! 
    performSegue(withIdentifier: "mySegue", sender: nil) 
}