【问题标题】:Arduino/Swift - BLE communications POST & GETArduino/Swift - BLE 通信 POST & GET
【发布时间】: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 应用程序?

【问题讨论】:

标签: ios swift arduino core-bluetooth bluetooth-lowenergy


【解决方案1】:
 func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
   print(characteristic)
}

从特征中获取价值 当值改变时这个委托调用。 外围设备发现这个更新的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多