我想通过 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)