我正在使用arduino羽毛BLE板并试图创建一个可以通过BLE将数据发送到电路板的iOS应用程序。我可以连接,但我无法访问txCharacteristic
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else {
return
}
for characteristic in characteristics {
if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) {
writableCharacteristic = characteristic
}
// if(characteristic.uuid == CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e"))
// {
// txCharacteristic = characteristic
// }
var txUUID = CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e")
let temp = characteristic.uuid.uuidString;
switch characteristic.uuid {
case txUUID:
txCharacteristic = characteristic;
break;
default:
break;
}
peripheral.setNotifyValue(true, for: characteristic)
}
}
此代码有效,但只发现以下UUID:
temp String "00001532-1212-EFDE-1523-785FEABCD123"
temp String "00001531-1212-EFDE-1523-785FEABCD123"
temp String "00001534-1212-EFDE-1523-785FEABCD123"
我已经发现这些UUID是DFU UUID。我怎样才能发现txCharacteristic?
添加了有关我如何调用discoverCharacteristics()的更多信息:
extension SimpleBluetoothIO: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let services = peripheral.services else {
return
}
targetService = services.first
if let service = services.first {
targetService = service
peripheral.discoverCharacteristics(nil, for: service)
}
}
答案 0 :(得分:0)
通过阅读本网站的信息,我能够解决这个问题: https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/adding-app-support
特别是这部分(注意UART是服务,TX和RX是服务的特征):
UART Service UUID: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
TX Characteristic UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
RX Characteristic UUID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
所以我需要使用UART服务ID调用peripheral.discoverServices()。在我的例子中,self.serviceUUID =“6e400001-b5a3-f393-e0a9-e50e24dcca9e”
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
//peripheral.discoverServices(nil)
//peripheral.discoverServices([CBUUID(string: "6e400001-b5a3-f393-e0a9-e50e24dcca9e")])
// Disover peripheral with service UUID that was given to us during initialization (self.serviceUUID)
peripheral.discoverServices([CBUUID(string: self.serviceUUID)])
}
然后在didDiscoverCharacteristicsFor服务中,我需要查找TX特征ID:
// Did Discover Characteristics
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else {
return
}
for characteristic in characteristics {
if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) {
writableCharacteristic = characteristic
}
// TX Characteristic UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
// https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/adding-app-support
let txUUID = CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e")
switch characteristic.uuid {
case txUUID:
txCharacteristic = characteristic;
break;
default:
break;
}
peripheral.setNotifyValue(true, for: characteristic)
}
}