[英]Reading data from Core Bluetooth Peripheral that is sending the array data?
I am trying read the array data sent by Peripheral and parse it into Swift array form. 我正在尝试读取Peripheral发送的数组数据,并将其解析为Swift数组形式。
Peripheral Device is sending data for Example: Array[1 To 15] Numbers.(For full scale purpose this integer values range between 1000 to 2000). 外围设备正在发送数据,例如:Array [1至15]数字。(出于全面的目的,此整数范围在1000至2000之间)。 but for now i am trying to make it work with 1 to 15 Integer values. 但是现在我正在尝试使其使用1到15个Integer值。 I was able to get the result in string format in encoded format. 我能够以编码格式的字符串格式获取结果。 My Question is how can i get array 1 to 15 from Characteristic.value from didUpdateValueFor Method in swift. 我的问题是如何快速从didUpdateValueFor方法的Characteristic.value获取数组1到15。
Here is the code snippet for reference. 这是供参考的代码段。
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if characteristic == rxCharacteristic {
count += 1
let data = characteristic.value
print("Count: \(count), data: \(data! as NSData)")
}
}
In X-Code console Printed result is: 在X代码控制台中,打印结果为:
Count: 2, data: <00000100 02> 计数:2,数据:<00000100 02>
Count: 3, data: <00030004 00050006 00070008 0009000a 000b000c> 计数:3,数据:<00030004 00050006 00070008 0009000a 000b000c>
Count: 4, data: <000d00> 计数:4,数据:<000d00>
Count: 5, data: <0e000f00> 计数:5,数据:<0e000f00>
Use below code for retrieving the Int
values in the form of array
. 使用下面的代码以array
的形式检索Int
值。
Don't forget to reset count = 0
and values.removeAll()
for data request. 不要忘记为数据请求重置count = 0
和values.removeAll()
。
I am assuming your characteristics.value
is Int
value 我假设你的characteristics.value
是Int
值
var values:[Int] = []
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if characteristic == rxCharacteristic {
count += 1
if let data = characteristic.value {
var num = 0
//Convert Data to Int
data.getBytes(&num, length: MemoryLayout<Int>.size)
values.append(num)
}
print("Count: \(count), data: \(data! as NSData)")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.