将数据从Arduino发送到应用程序

问题描述:

从以下教程可以看到:https://www.raywenderlich.com/85900/arduino-tutorial-integrating-bluetooth-le-ios-swift我可以通过iPhone应用程序控制伺服器。它将数据从iPhone发送到arduino板。我想要的是也能够将数据从Arduino发送到应用程序,基本上是相反的方式,但我很困惑我会如何使用Arduino发送数据。将数据从Arduino发送到应用程序

这是数据是如何发送到伺服上的Arduino:

if(BLE_Shield.available()) { 
    myservo.write(BLE_Shield.read()); // Write position to servo 
} 

所有有人告诉我的是,我需要写UART端口的RX与UUID A9CD2F86-8661-4EB1-B132-367A3434BC90,并得到特征发射应用程序将被通知RX特性的变化。我很难理解这一点,并会感谢任何帮助!该网站在页面底部包含完整的Swift源代码。

我从来没有使用过黑寡妇BLE盾牌,因为通常我用一个简单HM-10 BLE 4.0模块,但逻辑是一样的。要将数据从Arduino发送到iPhone,您应该写入....“写入”:

BLE_Shield.write("Test");

而要接收数据,您必须在BTService.swift类中写入函数didUpdateValueFor characteristic

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { 

    if (error != nil) { 
     print("Error rading characteristics: \(error)......)") 
     return; 
    } 

    if (characteristic.value != nil) { 
     //value here. 

     print("value is : \(characteristic.value)") 
     let dataBLE = String(data: characteristic.value!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) 

     print("Data from Arduino:\(dataBLE!)") 
    } 
} 

为了达到此目的,您必须确保iPhone始终连接到BLE。