您好,我是swift的初学者,我正在尝试创建一个以极地心率传递一个BLE的应用程序,我有一个问题是从characteristic.value生成的数据结构中获取正确的信息,我真的有一个将uint8转换为更简单的类似int或字符串的问题。
她是我的代码:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
if characteristic.uuid == POLARH7_HRM_MEASUREMENT_CHARACTERISTIC_UUID {
print("Get the new heart BPM : ", terminator:"")
var wavelength: UInt16?
if let data = characteristic.value {
var bytes = Array(repeating: 0 as UInt8, count:(characteristic.value?.count)!/MemoryLayout<UInt8>.size)
data.copyBytes(to: &bytes, count:data.count)
let data16 = bytes.map { UInt16($0) }
wavelength = 256 * data16[1] + data16[0]
}
print("W : ")
print(wavelength)
}
if characteristic.uuid == POLARH7_HRM_BODY_LOCATION_CHARACTERISTIC_UUID {
print("Get the new sensor location", terminator:"")
print(characteristic.value)
//let data = NSData(data: characteristic.value as! Data)
}
if characteristic.uuid == POLARH7_HRM_MANUFACTURER_NAME_CHARACTERISTIC_UUID {
print("Get the new name", terminator:"")
print(characteristic.value)
}
}
我有很多方法,但我是新手快速而快速3并不容易,因为每年都有这种语言的更新,因此最常见的提示是objective-c或swift 2。 就像在这个例子中一样:
// Instance method to get the manufacturer name of the device
- (void) getManufacturerName:(CBCharacteristic *)characteristic
{
NSString *manufacturerName = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]; // 1
self.manufacturer = [NSString stringWithFormat:@"Manufacturer: %@", manufacturerName]; // 2
return;
}
或
- (void) getHeartBPMData:(CBCharacteristic *)characteristic error:(NSError *)error
{
// Get the Heart Rate Monitor BPM
NSData *data = [characteristic value]; // 1
const uint8_t *reportData = [data bytes];
uint16_t bpm = 0;
if ((reportData[0] & 0x01) == 0) { // 2
// Retrieve the BPM value for the Heart Rate Monitor
bpm = reportData[1];
}
else {
bpm = CFSwapInt16LittleToHost(*(uint16_t *)(&reportData[1])); // 3
}
// Display the heart rate value to the UI if no error occurred
if( (characteristic.value) || !error ) { // 4
self.heartRate = bpm;
self.heartRateBPM.text = [NSString stringWithFormat:@"%i bpm", bpm];
self.heartRateBPM.font = [UIFont fontWithName:@"Futura-CondensedMedium" size:28];
[self doHeartBeat];
self.pulseTimer = [NSTimer scheduledTimerWithTimeInterval:(60. / self.heartRate) target:self selector:@selector(doHeartBeat) userInfo:nil repeats:NO];
}
return;
}
提前致谢
答案 0 :(得分:1)
从BLE设备发送的数据通常是unicode,因此您需要将这些unicode值转换为您需要的值。
以下是将其转换为字符串的示例:
// Convert byte to unicode string
let unicodeString = String(data: characteristic.value!, encoding: String.Encoding.utf8)
// Initialize value
var value: String? = nil
// Convert unicode to actual value
for element in unicodeString!.unicodeScalars {
value = "\(element.value)"
}
// If value exists
if let value = value {
// Do something with the value
}
答案 1 :(得分:0)
let data = characteristic.value
let count = (data?.count)! / MemoryLayout<UInt8>.size
var array = [UInt8](repeating: 0, count: count)
data?.copyBytes(to: &array, count:count * MemoryLayout<UInt8>.size)
print("Get the new heart BPM : ", terminator : "")
if ((array[0] & 0x01) == 0) {
let bpm = array[1]
let bpmInt = Int(bpm)
print(bpmInt)
}