我是swift的初学者,并尝试开发简单的程序,显示BLE的特征。我通过印刷确认我捕获了目标特征的UUID
myCharacteristicUUID_1 : (
"Manufacturer Name String",
"Model Number String",
"Serial Number String",
"Hardware Revision String",
"Firmware Revision String",
"Software Revision String"
)
但是,在override func prepare中(对于segue:UIStoryboardSegue,sender:Any?), 我通过打印再次检查它,我看到数据已经消失如下;
myCharacteristicUUID_2 : (
)
我可以移交其他类型的值(例如字符串,文本等)但我无法处理从BLE捕获的数据。有人可以指出当我在segue中使用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 :(得分:0)
以下是myCharacterisitcUUID
未正确读取的原因。请查看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)
}