![](/img/trans.png)
[英]How to write data to multiple Bluetooth LE slaves simultaneously on CoreBluetooth?
[英]How to extract SFLOAT value from Bluetooth LE data for 1822 PulseOximeter using CoreBluetooth
我正在开发一个 iOS 应用程序,该应用程序从支持蓝牙 LE 的设备读取脉搏血氧仪数据,在Swift 4.1 中使用iOS 11.4上的CoreBluetooth 。
我有CBCentralManager搜索外围设备,我找到了我感兴趣的CBPeripheral ,我验证它具有0x1822脉搏血氧仪服务,如蓝牙 SIG 此处所述。 (您可能需要向蓝牙 SIG 注册才能访问该链接。它是免费的,但需要一两天的时间。)
之后,我连接到它,然后我发现服务:
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.discoverServices(nil)
}
然后在我的外设中:didDiscoverServices我发现 GATT 特征:
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?){
for service in peripheral.services ?? [] {
if service.uuid.uuidString == "1822" {
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
从中我看到以下特征 ( CBCharacteristic.uuid ) 可用:0x2A5F、0x2A5E、0x2a60 和 0x2A52。 然后,我订阅了0x2A5F,这是PLX连续测量,被描述的更新在这里:
if service.uuid.uuidString == "1822" && characteristic.uuid.uuidString == "2A5F" {
// pulseox continuous
print("[SUBSCRIBING TO UPDATES FOR SERVICE 1822 'PulseOx' for Characteristic 2A5F 'PLX Continuous']")
peripheral.setNotifyValue(true, for: characteristic)
}
然后我开始在我的外设中接收 20 字节的数据包:didUpdateValueFor方法:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if characteristic.service.uuid.uuidString == "1822" && characteristic.uuid.uuidString == "2A5F" {
if let data = characteristic.value {
var values = [UInt8](repeating:0, count:data.count)
data.copyBytes(to: &values, count: data.count)
}
}
}
从参考文档中,您可以看到第一个字节是一堆位域,描述了数据包中包含哪些可选值。 接下来的2个字节用于该SpO2PR -正常的SFLOAT值-读取血氧(氧合),和下面的2个字节是用于SpO2PR -正常另一个SFLOAT值- PR(脉搏率)值。
蓝牙SIG列出了SFLOAT作为IEEE-11073 16位SFLOAT 这里。 IEEE 关于 IEEE-11073的文档未公开列出, 可以购买,但我宁愿避免这种情况。
知道如何解码吗? 我在 Stack Overflow 上发现了另一个问题,引用了普通的32 位 Float ,但那个问题是针对不同类型的 Float 的,它的答案不适用。
这里是:
Xcode 9.4.1或Xcode 10.0 测试版 3
iOS 11.4.1或iOS 12.0 测试版 3
Swift 4.1.2或Swift 4.2
func floatFromTwosComplementUInt16(_ value: UInt16, havingBitsInValueIncludingSign bitsInValueIncludingSign: Int) -> Float {
// calculate a signed float from a two's complement signed value
// represented in the lowest n ("bitsInValueIncludingSign") bits
// of the UInt16 value
let signMask: UInt16 = UInt16(0x1) << (bitsInValueIncludingSign - 1)
let signMultiplier: Float = (value & signMask == 0) ? 1.0 : -1.0
var valuePart = value
if signMultiplier < 0 {
// Undo two's complement if it's negative
var valueMask = UInt16(1)
for _ in 0 ..< bitsInValueIncludingSign - 2 {
valueMask = valueMask << 1
valueMask += 1
}
valuePart = ((~value) & valueMask) &+ 1
}
let floatValue = Float(valuePart) * signMultiplier
return floatValue
}
func extractSFloat(values: [UInt8], startingIndex index: Int) -> Float {
// IEEE-11073 16-bit SFLOAT -> Float
let full = UInt16(values[index+1]) * 256 + UInt16(values[index])
// Check special values defined by SFLOAT first
if full == 0x07FF {
return Float.nan
} else if full == 0x800 {
return Float.nan // This is really NRes, "Not at this Resolution"
} else if full == 0x7FE {
return Float.infinity
} else if full == 0x0802 {
return -Float.infinity // This is really negative infinity
} else if full == 0x801 {
return Float.nan // This is really RESERVED FOR FUTURE USE
}
// Get exponent (high 4 bits)
let expo = (full & 0xF000) >> 12
let expoFloat = floatFromTwosComplementUInt16(expo, havingBitsInValueIncludingSign: 4)
// Get mantissa (low 12 bits)
let mantissa = full & 0x0FFF
let mantissaFloat = floatFromTwosComplementUInt16(mantissa, havingBitsInValueIncludingSign: 12)
// Put it together
let finalValue = mantissaFloat * pow(10.0, expoFloat)
return finalValue
}
extraSFloat方法采用 Uint8 数组和该数组的索引来指示 SFLOAT 的位置。 例如,如果数组是两个字节(只是 SFLOAT 的两个字节),那么您会说:
let floatValue = extractSFloat(values: array, startingIndex: 0)
我这样做是因为在处理蓝牙数据时,我总是得到一个UInt8值数组,其中包含我需要解码的数据。
这就是我在Swift 5 (XCode 12.4) 中的做法
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
guard characteristic.service.uuid == CBUUID(string: "1822"),
characteristic.uuid == CBUUID(string: "2A5F"),
let data = characteristic.value else {
return
}
let numberOfBytes = data.count
var byteArray = [UInt8](repeating: 0, count: numberOfBytes)
(data as NSData).getBytes(&byteArray, length: numberOfBytes)
logger.debug("Data: \(byteArray)")
let oxygenation = byteArray[1]
let heartRate = byteArray[3]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.