Swift BLE“didDiscoverServices”没有执行。我错过了什么吗?
问题描述:
我一直在阅读教程,使用Adafruit的nRF8001并通过Arduino将其连接到iOS设备。到目前为止,我已经正确设置了一切,它在他们的应用程序中正常工作。
我试图写我自己的应用程序,(现在)完全一样的东西,我阅读他们的教程,并从我的理解我复制代码尽可能接近,到目前为止我可以得到连接和大多数事情似乎是工作(UI明智),但我似乎无法做任何事情过去连接到设备:
这里是我的代码之后连接:Swift BLE“didDiscoverServices”没有执行。我错过了什么吗?
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
//What to do when it discovers a peripheral, add it to the array list
print("Peripheral found: " + (peripheral.name ?? "Unknown Name"))
peripheralsFoundNames.append((peripheral.name ?? "Unknown Name"))
peripheralsFoundData.append((advertisementData.description))
peripheralsFoundCB.append(peripheral)
peripheralsFoundRSSIs.append(RSSI)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected to device!")
displayStatusAlert(localmsg: "Connection Succesful!")
NotificationCenter.default.post(name: Notification.Name(rawValue: DEVICE_READY_KEY), object: self)
data?.length = 0 //clear any data that might be stored
peripheral.discoverServices([BLETemperatureService])
print("Here at didConnect, connected to:" + peripheral.name!)
// Here needs to add code to check if it's a single or multi-channel device via the advertisement data or some other constant, maybe the name?
}
正如你所看到的,我打电话显式peripheral.discoverServices,然后我有一个执行的打印语句。然后,我有以下线以下(注:没有人可以执行AT ANY TIME(至少不是打印语句)。
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
print("here at diddisoverservices")
if ((error) != nil){
displayStatusAlert(localmsg: "Error: \n" + (error?.localizedDescription ?? "Error Unknown"))
}
guard let services = peripheral.services
else{
return
}
for service in services {
peripheral.discoverCharacteristics(nil, for: service)
}
print ("Discovered!")
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if ((error) != nil){
displayStatusAlert(localmsg: "Error: \n" + (error?.localizedDescription ?? "Error Unknown"))
}
guard let characteristics = service.characteristics
else{
return
}
for characteristic in characteristics {
//looks for the right characteristic
print("looking for characteristic")
if characteristic.uuid.isEqual(BLERXCharacteristic) {
deviceConnectedRXChar = characteristic
//Once found, subscribe to the this particular characteristic
peripheral.setNotifyValue(true, for: deviceConnectedRXChar!)
peripheral.readValue(for: characteristic)
print("Rx Characteristic: \(characteristic.uuid)")
}
if characteristic.uuid.isEqual(BLETXCharacteristic){
deviceConnectedTXChar = characteristic
print("Tx Characteristic: \(characteristic.uuid)")
}
peripheral.discoverDescriptors(for: characteristic)
}
print ("Characteristic discovered")
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if characteristic == deviceConnectedRXChar {
if let ASCIIstring = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue) {
receivedDataString = ASCIIstring
print("Value Recieved: \((receivedDataString as String))")
NotificationCenter.default.post(name: Notification.Name(rawValue: DEVICE_SENT_DATA), object: nil)
}
}
答
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
是一个CBPeripheralDelegate
方法
所以你缺少什么。只是做peripheral.discoverServices([BLETemperatureService])
之前设置CBPeripheral
对象delegate
所以,你需要做的peripheral.delegate = self
第一:你能展现'didDiscoverPeripheral'和你有很强的外设参考?第二:如果你做'peripheral.discoverServices(nil)',它是否工作? – Larme
添加了didDiscoverPeripheral代码,另外,尝试(零),仍然得到相同的结果(打印“这里在diddiscoverservices”不运行) –
'peripheralsFoundCB.append(peripheral)'后,你永远不会调用central.connect(外设,选项:[])'它发现了它,但它没有启动连接。一台设备的同时连接受到限制,如果您处于数百台设备的环境中,希望是这样。 – Larme