iOS Swift应用中如何从AND BLE体重秤UUID:2A9D获取正确测量值
Hey there! Let's get that weight parsing sorted out—your issue looks like a common combination of byte order mismatch and missing unit conversion, which is super typical with BLE devices.
First, let's break down the numbers: you're seeing 11860.0 when the actual weight is 59.2kg. If you do the math, 11860 × 0.005 = 59.3kg, which is almost exactly your measured value (likely just a tiny measurement error). That tells me the raw integer from the BLE data uses a scaling factor of 0.005kg per unit, and you're probably just displaying the raw integer without applying this conversion.
On top of that, BLE devices almost always send multi-byte values in little-endian order, so we need to make sure we're parsing the bytes correctly.
Here's a step-by-step fix for your Swift code:
1. Extract the weight bytes from the 10-byte data
The A&D UC-352BLE stores weight in the first 2 bytes of the 2A9D characteristic data. We'll pull those out first.
2. Parse the bytes with the correct endianness
Convert the raw bytes to a 16-bit integer, making sure to use little-endian (the standard for BLE).
3. Apply the scaling factor
Multiply the raw integer by 0.005 to get the actual weight in kilograms.
Here's the concrete Swift code:
// Assume this is the 10-byte Data you received from the BLE characteristic let bleMeasurementData: Data = ... // Step 1: Extract the first 2 bytes (weight data) guard bleMeasurementData.count >= 2 else { print("Insufficient data received from scale") return } let weightBytes = bleMeasurementData.subdata(in: 0..<2) // Step 2: Parse to 16-bit integer with little-endian order var rawWeightValue: Int16 = 0 weightBytes.withUnsafeBytes { buffer in rawWeightValue = buffer.load(as: Int16.self).littleEndian } // Step 3: Convert to actual kg using the scaling factor let actualWeightKg = Double(rawWeightValue) * 0.005 print("Actual weight: \(actualWeightKg) kg") // Should show ~59.2kg
If you want to confirm the exact byte values, you can print the raw data in hex to cross-verify:
let hexString = bleMeasurementData.map { String(format: "%02X", $0) }.joined() print("Raw BLE data (hex): \(hexString)")
For a 59.2kg measurement, the first two hex bytes should be 30 2E (which is 11840 in decimal, 11840 × 0.005 = 59.2).
Quick check for common mistakes
- Did you forget to apply the scaling factor? That's the most likely reason you're seeing 11860 instead of 59.2.
- Are you using the correct endianness? If you skip
.littleEndian, you might get a completely wrong number (e.g., big-endian would interpret30 2Eas 12334 instead of 11840).
Give this a shot, and it should fix your parsing issue!
内容的提问来源于stack exchange,提问作者Uzzal Biswas

