Swift BLE 设备 - 写入值
Swift BLE Device - write value
我有 0x010303757372707764 可以写入 ble 设备。我知道使用 writeValue 方法
peripheral.writeValue(data, for: characteristics[i], type: .withResponse)
但是我想写0x010303757372707764作为数据。我怎么做?
如果您需要更多信息,请告诉我!谢谢!
为此你必须像这样使用 UInt8 数组值 "Array"
当您在 Peripheral.writeValue 中写入数据时,您必须将 UInt8 值作为 Data() 函数传递。
让我分享一个这样的例子
让 CmdRequestTempLogSize = "#requestTempLogSize$"
让 writeData = 数据(CmdRequestTempLogSize.bytes)
//* CmdRequestTempLogSize.bytes 输入你的 UInt8 值*
对于字节你可以使用字符串扩展(现在我正在使用字符串值转换成字节(UInt8)
public var bytes: Array<UInt8> {
return data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(utf8)
}
希望这个例子能让你明白如何将数据转换成字节数组
对数据使用 UInt8
序列初始值设定项:
init<S>(_ elements: S) where S : Sequence, Self.Element == S.Element
对于您的示例,这将是:
let data = Data([0x01, 0x03, 0x03, 0x75, 0x73, 0x72, 0x70, 0x77, 0x64])
我有 0x010303757372707764 可以写入 ble 设备。我知道使用 writeValue 方法
peripheral.writeValue(data, for: characteristics[i], type: .withResponse)
但是我想写0x010303757372707764作为数据。我怎么做?
如果您需要更多信息,请告诉我!谢谢!
为此你必须像这样使用 UInt8 数组值 "Array"
当您在 Peripheral.writeValue 中写入数据时,您必须将 UInt8 值作为 Data() 函数传递。
让我分享一个这样的例子
让 CmdRequestTempLogSize = "#requestTempLogSize$"
让 writeData = 数据(CmdRequestTempLogSize.bytes) //* CmdRequestTempLogSize.bytes 输入你的 UInt8 值*
对于字节你可以使用字符串扩展(现在我正在使用字符串值转换成字节(UInt8)
public var bytes: Array<UInt8> {
return data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(utf8)
}
希望这个例子能让你明白如何将数据转换成字节数组
对数据使用 UInt8
序列初始值设定项:
init<S>(_ elements: S) where S : Sequence, Self.Element == S.Element
对于您的示例,这将是:
let data = Data([0x01, 0x03, 0x03, 0x75, 0x73, 0x72, 0x70, 0x77, 0x64])