This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Bug in Heart Rate example

Hi,

there seems to be a bug in the Heart Rate Monitor (HRM) example or in the iOS app (nRF connect) -- when the simulated heart rate exceeds 8 bits (256) in the example, the corresponding iOS app starts showing incorrect values (259 instead of 260, 269 instead of 270, and so on). So, either the encoding part in the nordic SDK is broken, or the decoding part in the app. Any ideas?

Parents
  • Alright, I found the bug. It's in the iOS app which incorrectly converts the 2 bytes from the heart rate. My code in Swift 4.2 is now -- I'll leave it to the maintainer of nRF connect to fix it in their example which uses Objective-C:

    private func heartRate(from characteristic: CBCharacteristic) -> Int {
    guard let characteristicData = characteristic.value else { return -1 }
    let array = characteristicData.toArray(type: UInt8.self)
    var bpmValue : Int = 0;
    if ((array[0] & 0x01) == 0) {
    // Stored in one byte
    bpmValue = Int(array[1])
    } else {
    //Stored in two bytes
    bpmValue = (Int(array[2]) << 8) + Int(array[1])
    }
    return bpmValue
    }
Reply
  • Alright, I found the bug. It's in the iOS app which incorrectly converts the 2 bytes from the heart rate. My code in Swift 4.2 is now -- I'll leave it to the maintainer of nRF connect to fix it in their example which uses Objective-C:

    private func heartRate(from characteristic: CBCharacteristic) -> Int {
    guard let characteristicData = characteristic.value else { return -1 }
    let array = characteristicData.toArray(type: UInt8.self)
    var bpmValue : Int = 0;
    if ((array[0] & 0x01) == 0) {
    // Stored in one byte
    bpmValue = Int(array[1])
    } else {
    //Stored in two bytes
    bpmValue = (Int(array[2]) << 8) + Int(array[1])
    }
    return bpmValue
    }
Children
No Data
Related