【发布时间】:2016-03-07 19:42:04
【问题描述】:
如何与 swift 和 arduino 设备进行 BLE 通信? 现在使用我的代码,我可以将值发送到我的 BLE shield,但我不知道如何将报告发送回我的 swift 应用程序!
我使用 HM-10 BLE 模块和 Swift 2.0。
这是我的arduino代码:
#include<SoftwareSerial.h>
SoftwareSerial Try(1,0);
void setup() {
pinMode(3, OUTPUT);
Try.begin(9600);
}
void loop() {
if(Try.available()){
switch(Try.read()){
case('a') :{
digitalWrite(3, HIGH);
}
}
}
}
这是一个简单的代码,当从我的应用程序接收到字符“a”时,它会打开绿色 LED。 这样就可以了,但我想发回报告“绿色 LED 已打开!”到我的 Swift 应用程序。
这是将“a”值发送到我的 BLE 模块的 Swift 代码。
import UIKit
import CoreBluetooth
class Connection: UIViewController{
var connee: CBCharacteristic!
var per: CBPeripheral!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func SendingVal(sender: AnyObject) {
let Sen = "a"
let Value: NSData = Sen.dataUsingEncoding(NSUTF8StringEncoding)!
sendDataToCentral(per, characteristic: connee, data: Value)
}
private func sendDataToCentral(peripheral: CBPeripheral, characteristic: CBCharacteristic, data: NSData) {
peripheral.writeValue(data, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse) }
}
那么,我怎样才能将 BLE 模块的报告发送回我的 swift 应用程序?
【问题讨论】:
-
我们猜测您完成了所有的连接/读取服务/特性。您看到 Arduino 中发送的值了吗?就算错了?
-
我可以看到我的 arduino 中的所有值,但是...我不知道如何发回报告或值
标签: ios swift arduino core-bluetooth bluetooth-lowenergy