toFormattedString method
Converts the manufacturer data to a hexadecimal string format for easy reading.
The manufacturer identifier is surrounded by angle brackets to make its separation from the rest of the data more clear. Each byte is represented as a two-digit hexadecimal value with spaces between bytes for readability.
Example output: <4C00> 02 15 A1 B2 C3 D4 ...
The format is similar to that used by the nRF Connect for Mobile app.
Implementation
String toFormattedString() {
// Create a string representing the manufacturer identifier, surrounded by angle brackets.
final StringBuffer formattedString = StringBuffer('<');
for (int i = 0; i < manufacturerId.length; i++) {
formattedString
.write(manufacturerId[i].toRadixString(16).padLeft(2, '0'));
}
formattedString.write('> ');
// Create a string representing the payload, with spaces between each byte (two hex characters).
for (int i = 0; i < payload.length; i++) {
if (i > 0) {
formattedString.write(' ');
}
formattedString.write(payload[i].toRadixString(16).padLeft(2, '0'));
}
return formattedString.toString().toUpperCase();
}