【发布时间】:2016-10-21 10:39:52
【问题描述】:
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
dispatch_async(dispatch_get_main_queue(), ^{
NSData *data = characteristic.value;
uint8_t *array = (uint8_t*) data.bytes;
cadenceValue = [CharacteristicReader readUInt8Value:&array];
self.cadence.text = [NSString stringWithFormat:@"%d", cadenceValue];
});
}
如何在 swift 2 中从 bLE(蓝牙低功耗)设备获取节奏。我无法为此找到确切的代码。为此,didUpdateValueForCharacteristic 委托方法被调用。
我有一个 nRF Toolbox 的代码,但它在 objective c 或 swift 3 中,但我的项目在 swift 2 中。我尝试使用桥接头调用目标 c 方法,但它总是返回 0 cadence。
【问题讨论】:
-
Swift 3 示例代码是什么样的?
-
Getting Error on bindMemory and pointee in swift 2 // on didUpdateValueForCharacteristic let data = characteristic.value var array = UnsafeMutablePointer
(mutating: (data! as NSData).bytes.bindMemory(to: UInt8.self, capacity: data!.count)) self.cadenceValue = NORCharacteristicReader.readUInt8Value(ptr: &array) // 方法。 static func readUInt8Value(ptr aPointer : inout UnsafeMutablePointer ) -> UInt8 { let val = aPointer.pointee aPointer = aPointer.successor() return val } @davidgyoung -
@davidgyoung 面临在 swift 2 中转换上述代码的问题,问题在于 aPointer.pointee 和 bindMemory
标签: ios swift bluetooth-lowenergy ibeacon cadence