2016-11-22 48 views
0

從以下教程可以看到: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源代碼。

回答

1

我從來沒有使用過黑寡婦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。

相關問題