【问题标题】:Cadence from BLE Device来自 BLE 设备的 Cadence
【发布时间】: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 cswift 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


【解决方案1】:

我不确定CharacteristicReader的定义,但你可以试试:

[CharacteristicReader readUInt8Value:&array];
cadenceValue = Int(array[0])
self.cadence.text = [NSString stringWithFormat:@"%d", cadenceValue];

上面假设调用readUInt8Value的结果被放入UInt8对象的数组中,并且cadence值在数组的第一个字节中。您还可以通过尝试cadenceValue = Int(array[1])cadenceValue = Int(array[2]) 等来检查正确的值是否在其他字节中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多