从iPhone Swift Xcode与HM10通信

时间:2018-08-01 13:03:23

标签: ios swift xcode core-bluetooth hm-10

我想通过iPhone应用程序与HM10通信,我已经创建了一个应用程序来建立与HM10的连接,我能够向蓝牙发送数据,但我没有接收数据,以下是代码:-

它不打印任何内容。

 func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    // notify the delegate in different ways
    // if you don't use one of these, just comment it (for optimum efficiency :])
    let data = characteristic.value
    guard data != nil else { return }

    // first the data
    delegate.serialDidReceiveData(data!)
  //  print(data)

    // then the string
    if let str = String(data: data!, encoding: String.Encoding.utf8) {
        delegate.serialDidReceiveString(str)
        print("str")
    } else {
        //print("Received an invalid string!") uncomment for debugging
    }

    // now the bytes array
    var bytes = [UInt8](repeating: 0, count: data!.count / MemoryLayout<UInt8>.size)
    (data! as NSData).getBytes(&bytes, length: data!.count)
    delegate.serialDidReceiveBytes(bytes)
    print(bytes)
//    print(delegate.serialDidReceiveData(bytes))
}
func serialDidReceiveString(message: String) {
   // Label.text! = message
    print(message)

0 个答案:

没有答案