2017-10-05 179 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) 
    } 
}