I have noticed that in Mi Band 3 even without pairing(Authentication) you can access Heart Rate data and Details for heart rate measurement:
let BLE_Heart_Rate_Service_CBUUID = CBUUID(string: "0x180D")
let Heart_rate_UUID = CBUUID(string: "2A37")
Now put below code in didUpdateValueFor
if characteristic.uuid == Heart_rate_UUID {
print("HeartRate_UUID reading: ", characteristic.value)
peripheral.readValue(for: characteristic)
print("HEART RATE: ", getHeartRate(heartRateData: characteristic.value!))
}
Use getHeartRate() for reading Heart Rate.
func getHeartRate(heartRateData:Data) -> Int{
print("--- UPDATING Heart Rate..")
var buffer = [UInt8](repeating: 0x00, count: heartRateData.count)
heartRateData.copyBytes(to: &buffer, count: buffer.count)
var bpm:UInt16?
if (buffer.count >= 2){
if (buffer[0] & 0x01 == 0){
bpm = UInt16(buffer[1]);
}else {
bpm = UInt16(buffer[1]) << 8
bpm = bpm! | UInt16(buffer[2])
}
}
if let actualBpm = bpm{
return Int(actualBpm)
}else {
return Int(bpm!)
}
}