BmScanAdvertisement.fromMap constructor

BmScanAdvertisement.fromMap(
  1. Map json
)

Implementation

factory BmScanAdvertisement.fromMap(Map<dynamic, dynamic> json) {
  // Get raw data
  var rawManufacturerData = json['manufacturer_data'] ?? {};
  var rawServiceData = json['service_data'] ?? {};
  var rawServiceUuids = json['service_uuids'] ?? [];

  // Cast the data to the right type
  Map<int, List<int>> manufacturerData = {};
  for (var key in rawManufacturerData.keys) {
    manufacturerData[key] = _hexDecode(rawManufacturerData[key]);
  }

  // Cast the data to the right type
  Map<Guid, List<int>> serviceData = {};
  for (var key in rawServiceData.keys) {
    serviceData[Guid(key)] = _hexDecode(rawServiceData[key]);
  }

  // Cast the data to the right type
  List<Guid> serviceUuids = [];
  for (var val in rawServiceUuids) {
    serviceUuids.add(Guid(val));
  }

  return BmScanAdvertisement(
    remoteId: DeviceIdentifier(json['remote_id']),
    platformName: json['platform_name'],
    advName: json['adv_name'],
    connectable: json['connectable'] != null ? json['connectable'] != 0 : false,
    txPowerLevel: json['tx_power_level'],
    appearance: json['appearance'],
    manufacturerData: manufacturerData,
    serviceData: serviceData,
    serviceUuids: serviceUuids,
    rssi: json['rssi'] ?? 0,
  );
}