I am retrieving a BLE characteristic value and I want to understand how am I supposed to read the advertised value on an iOS App using Swift?
I am using a c++ Library from nkolban to advertise a value over Bluetooth from an ESP32. I have a u32int value voltage1 and convert it into a string:
std::string Voltage1AsString((char*)&voltage1, 4);
Then set the value for the characteristic:
Signal1_Characteristic->setValue(Voltage1AsString);
What I found till now is this function for reading in the value which, nearly works perfectly, but I want to understand the complete implementation to gain the knowledge for another use case.
private func getByteArray(from characteristic: CBCharacteristic ) -> Int{
guard let characteristicData = characteristic.value else {return -1}
let byteArray = [UInt8](characteristicData)
return (Int(byteArray[1]) << 8) + Int(byteArray[2] << 8) + Int(byteArray[3] << 8) + Int(byteArray[2] << 8)
}