【问题标题】:How to send data to bluetooth LE in iOS如何在 iOS 中向蓝牙 LE 发送数据
【发布时间】:2018-07-01 04:39:09
【问题描述】:

我是蓝牙通信的新手。我的任务是用 commonand 将数据写入蓝牙。 共同点是0x61,我需要将01-02-03-04 4 字节值传递给它。 我已经认出了特征。

我想在 swift 4 中得到答案。

  let string = "0xB101020304"
                    let _data = string.data(using: String.Encoding.utf8)


                    self.peripheral?.writeValue(ofCharac: ser, value: _data!, completion: { (reult) in
                        switch result{
                        case .success(let value):
                            print(value)
                            print("wow")
                        case .failure(let error):
                            print("error: \(error.localizedDescription)")
                        }
                    })

【问题讨论】:

  • 能否请您显示您尝试过的相关代码并指出您的具体问题。你的问题太宽泛了
  • @Paulw11 我已经分享了一些代码
  • 您那里的代码正在发送字符串。如果你想发送字节值,那么你需要声明一个字节数组并发送它
  • 可以分享一下脚本吗? @Paulw11

标签: ios iphone swift bluetooth-lowenergy swift4


【解决方案1】:

您拥有的代码将发送代表字符串 "0xB101020304" 的字节,但可能您想发送字节 B1 01 02 03 04。

let dataBytes:[UInt8] = [0xB1,0x01,0x02,0x03,0x04]

let data = Data(bytes: dataBytes)

self.peripheral?.writeValue(ofCharac: ser, value: data, completion: { (result) in
    switch result {
        case .success(let value):
            print(value)
            print("wow")
        case .failure(let error):
            print("error: \(error.localizedDescription)")
    }
})

【讨论】:

    猜你喜欢
    • 2015-03-23
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多