2017-10-05 129 views
0

我正在使用arduino羽毛BLE板并尝试创建可通过BLE将数据发送到主板的iOS应用。我可以连接,但我不能给txCharacteristiciOS蓝牙低功耗(BLE)未发现TX特性

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

你确定这是正确的服务在正确的设备上?你用正确的参数调用了“discoverServices”和“discoverCharacteristics”吗?尝试下载BLE浏览器并检查它看到的内容。 – Kevin

+0

嗨,谢谢你的帮助。我打电话discoverServices(无)应该发现所有的服务。我已经添加了调用discoverCharacteristics()到上面的问题的代码。 – Megan

回答

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