ManufacturerData.fromString constructor

ManufacturerData.fromString(
  1. String manufacturerData
)

Creates an instance of ManufacturerData from the provided string. In the provided string, the first two bytes represent the manufacturer identifier, and the remaining bytes represent the manufacturer-specific payload.

This factory constructor is used with data returned from the native side in response to Method Channel calls.

Implementation

factory ManufacturerData.fromString(String manufacturerData) {
  // Convert the manufacturer data string to a list of integers.
  final Uint8List manufacturerDataInts = Uint8List.fromList(
    List<int>.generate(
      manufacturerData.length ~/ 2,
      (i) => int.parse(manufacturerData.substring(i * 2, i * 2 + 2), radix: 16),
    ),
  );

  // Extract the manufacturer identifier and the payload.
  final Uint8List manufacturerId = Uint8List.sublistView(manufacturerDataInts, 0, 2);
  final Uint8List payload = Uint8List.sublistView(manufacturerDataInts, 2);

  return ManufacturerData(
    manufacturerId: manufacturerId,
    payload: payload,
  );
}