2016-10-21 55 views
0
-(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]; 
     }); 
} 

如何获得节奏从BLE(蓝牙低功耗)器件迅速2。我无法找到确切的代码。对于此didUpdateValueForCharacteristic调用委托方法。Cadence的

我NRF工具箱的代码,但它是在目标CSWIFT 3,但我的项目是在迅速2。我试图使用桥接头调用客观的c方法,但它总是返回0节奏。

+0

Swift 3示例代码的外观如何? – davidgyoung

+0

获取有关bindMemory错误和指针对象中迅速2 //上didUpdateValueForCharacteristic 让数据= characteristic.value 变种阵列= UnsafeMutablePointer (不同诱变:(数据作为NSData的).bytes.bindMemory(到:UINT8。 self,capacity:data!.count)) self.cadenceValue = NORCharacteristicReader.readUInt8Value(ptr:&array) //方法。 静态FUNC readUInt8Value(PTR aPointer:INOUT UnsafeMutablePointer ) - > UINT8 { 让VAL = aPointer.pointee aPointer = aPointer.successor() 返回VAL } @davidgyoung – Ved

+0

@davidgyoung面临的问题,以上面的代码转换在swift 2中,问题是使用aPointer.pointee和bindMemory – Ved

回答

0

我不知道的CharacteristicReader定义,但你可以尝试:

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

上述假定调用readUInt8Value结果被投入UInt8对象的数组,和节奏值位于数组的第一个字节中。您还可以通过尝试cadenceValue = Int(array[1])cadenceValue = Int(array[2])等来检查其他字节中的值是否合适。