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

标签 ios swift arduino core-bluetooth bluetooth-lowenergy

如何与 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/

相关文章:

ios - 如何在 UIButton 上触发不同的操作

ios - 将值设置为 SWIFT 中的计算属性

swift - UIButton 边框功能仅返回白色边框

ios - 从 UISearchController 过滤的搜索结果中删除

c - 如何计算 ADC 读数的平均值?

ios - 快速获取实现协议(protocol)的类类型

ios - 如何用空格#define标识符

ios - 如何从 Firebase 获取单个数据库引用

arduino - 在 OLED 显示屏上滚动长文本

arduino - 如何从 arduino uno kit 中删除旧程序并在其中上传新程序?