gpt4 book ai didi

ios - Arduino/Swift - BLE 通信 POST 和 GET

转载 作者:行者123 更新时间:2023-11-28 08:43:36 25 4
gpt4 key购买 nike

如何与 swift 和 arduino 设备进行 BLE 通信?现在,使用我的代码,我可以将值发送到我的 BLE 盾牌,但我不知道如何将报告发送回我的 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 应用程序?

最佳答案

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

从特征中获取值(value)当值更改时,此委托(delegate)调用。外设发现这个更新的值。

关于ios - Arduino/Swift - BLE 通信 POST 和 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35852536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com