如何从使用 SWIFT 3 的 iOS 应用程序中的测量特征 UUID:AND BLE WS 的 2A9D 获取实际测量值?
How to get the actual measurement value from measurement chracteristic UUID: 2A9D of AND BLE WS in an iOS app using SWIFT 3?
我正在尝试使用 swift 在 iOS 应用中从 AND BLE 体重秤型号的测量特征 UUID 同步实际测量值:A&D_UC-352BLE_011E17。该应用程序正在从 BLE WS 读取测量特征 UUID 和 10 字节值,但是当它试图从 NSData 读取实际测量值时,它正在同步并在应用程序中显示错误值,例如实际重量 59.2 KG应用程序的值为 11860.0。
我已经实现了从这个 BLE WS 配对和同步数据所需的所有相关方法,很可能该应用程序还从 BLE WS 获取体重秤测量服务 UUID:181D 和测量特征 UUID:2A9D,因为我在 Xcode 调试控制台中看到了这些值。
在那种情况下,我认为主要问题在于reading/converting测量特征UUID值与double类型的实际测量值。下面给出了我的didUpdateCharacteristicValue方法,请教我如何从测量特征UUID中获取实际测量值?
func didUpdateCharacteristicValue(_ characteristic: CBCharacteristic){
communicating = true
let charUUID = ANDWeightScaleBTManager.measurementWeightCharacteristicUUID
print("did update value for characteristic")
print("charUUID:", charUUID)
print("charValue is", characteristic.value!)
print("value is:" + (characteristic.value?.base64EncodedString())!)
if (charUUID == ANDWeightScaleBTManager.measurementWeightCharacteristicUUID)
{
NSLog("Got Weight Scale Measurement char")
var weightData: NSData
weightData = characteristic.value! as NSData
//var data: NSData
//weightData = NSData.init(data: characteristic.value!)//if (bpmData.length <= 12) {
let flag = weightData.subdata(with: NSRange.init(location: 0, length: 1))
let unit : UInt16 = readInteger(data: weightData, start: 0)
let weight : UInt16 = readInteger(data: weightData, start: 1)
print("weight: \(weight)")//print the output
var message: String
var image: UIImage
var data = [MeasurementType: Double]()
message = "Completed measurement."
image = UIImage(named: "Success")!
data = [.weight: Double(weight)]
shouldEnd = true
update(data, message, image, shouldEnd)
}
}
如果您参考 documentation for the characteristic,您会看到公制 (SI) 值的单位是 0.005 千克。
如果将您拥有的值 11860 乘以 0.005,您将得到 59.3 公斤。
let kg = Double(weight) * 0.005
我正在尝试使用 swift 在 iOS 应用中从 AND BLE 体重秤型号的测量特征 UUID 同步实际测量值:A&D_UC-352BLE_011E17。该应用程序正在从 BLE WS 读取测量特征 UUID 和 10 字节值,但是当它试图从 NSData 读取实际测量值时,它正在同步并在应用程序中显示错误值,例如实际重量 59.2 KG应用程序的值为 11860.0。
我已经实现了从这个 BLE WS 配对和同步数据所需的所有相关方法,很可能该应用程序还从 BLE WS 获取体重秤测量服务 UUID:181D 和测量特征 UUID:2A9D,因为我在 Xcode 调试控制台中看到了这些值。
在那种情况下,我认为主要问题在于reading/converting测量特征UUID值与double类型的实际测量值。下面给出了我的didUpdateCharacteristicValue方法,请教我如何从测量特征UUID中获取实际测量值?
func didUpdateCharacteristicValue(_ characteristic: CBCharacteristic){
communicating = true
let charUUID = ANDWeightScaleBTManager.measurementWeightCharacteristicUUID
print("did update value for characteristic")
print("charUUID:", charUUID)
print("charValue is", characteristic.value!)
print("value is:" + (characteristic.value?.base64EncodedString())!)
if (charUUID == ANDWeightScaleBTManager.measurementWeightCharacteristicUUID)
{
NSLog("Got Weight Scale Measurement char")
var weightData: NSData
weightData = characteristic.value! as NSData
//var data: NSData
//weightData = NSData.init(data: characteristic.value!)//if (bpmData.length <= 12) {
let flag = weightData.subdata(with: NSRange.init(location: 0, length: 1))
let unit : UInt16 = readInteger(data: weightData, start: 0)
let weight : UInt16 = readInteger(data: weightData, start: 1)
print("weight: \(weight)")//print the output
var message: String
var image: UIImage
var data = [MeasurementType: Double]()
message = "Completed measurement."
image = UIImage(named: "Success")!
data = [.weight: Double(weight)]
shouldEnd = true
update(data, message, image, shouldEnd)
}
}
如果您参考 documentation for the characteristic,您会看到公制 (SI) 值的单位是 0.005 千克。
如果将您拥有的值 11860 乘以 0.005,您将得到 59.3 公斤。
let kg = Double(weight) * 0.005