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];
         });
}

How to get cadence from bLE (Bluetooth low energy) device in swift 2. I am unable to find exact code for this. For this didUpdateValueForCharacteristic delegate method is called.

I have a code of nRF Toolbox but it is in objective c or swift 3 but my project is in swift 2. I tried to call objective c method using bridging header but it was always returned 0 cadence.

Wos
  • 390
  • 1
  • 16
Ved Rauniyar
  • 1,539
  • 14
  • 21
  • What does the Swift 3 sample code look like? – davidgyoung Oct 21 '16 at 11:46
  • 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) // Method. static func readUInt8Value(ptr aPointer : inout UnsafeMutablePointer) -> UInt8 { let val = aPointer.pointee aPointer = aPointer.successor() return val } @davidgyoung – Ved Rauniyar Oct 21 '16 at 11:53
  • @davidgyoung facing problem to convert above code in swift 2, Problem is with aPointer.pointee and bindMemory – Ved Rauniyar Oct 21 '16 at 11:57

1 Answers1

0

I'm not sure the definition of CharacteristicReader, but you might try:

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

The above assumes that the result of the call to readUInt8Value gets put into the array of UInt8 objects, and the cadence value is in the first byte of the array. You could also check if the proper value is in other bytes by trying cadenceValue = Int(array[1]) or cadenceValue = Int(array[2]), etc.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204