toFormattedString method

String toFormattedString()

Converts the manufacturer data to a string that is formatted for easy reading. The manufacturer identifier is surrounded by angle brackets to make its separation from the rest of the data more clear. A space is included after every fourth character to separate the manufacturer data into chunks of four characters.

The end result of this formatting is a value that looks similar to the format 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 after every fourth character.
  for (int i = 0; i < payload.length; i++) {
    formattedString.write(payload[i].toRadixString(16).padLeft(2, '0'));
    if ((i + 1).isEven) {
      formattedString.write(' ');
    }
  }

  return formattedString.toString().toUpperCase();
}