WebAdvertisementReceivedEvent.fromJSObject constructor

WebAdvertisementReceivedEvent.fromJSObject(
  1. Object jsObject,
  2. WebBluetoothDevice device
)

Convert an event received from a single js object to something that can actually be used.

jsObject is the original object. device is the device that the event was fired for. Normally the device would be inside of the jsObject but then that device would need to be wrapped in another WebBluetoothDevice while the original can just be re-used.

Implementation

factory WebAdvertisementReceivedEvent.fromJSObject(
    final Object jsObject, final WebBluetoothDevice device) {
  if (!_JSUtil.hasProperty(jsObject, "device")) {
    throw UnsupportedError("JSObject does not have a device.");
  }
  if (!_JSUtil.hasProperty(jsObject, "uuids")) {
    throw UnsupportedError("JSObject does not have a uuids.");
  }
  if (!_JSUtil.hasProperty(jsObject, "manufacturerData")) {
    throw UnsupportedError("JSObject does not have a manufacturerData.");
  }
  if (!_JSUtil.hasProperty(jsObject, "serviceData")) {
    throw UnsupportedError("JSObject does not have a serviceData.");
  }

  final List<String> uuids = _parseUUIDS(jsObject);
  final UnmodifiableMapView<int, ByteData> manufacturerData =
      _getByteDataMap(jsObject, "manufacturerData");
  final UnmodifiableMapView<String, ByteData> serviceData =
      _getByteDataMap(jsObject, "serviceData");

  final String? name = _JSUtil.getProperty(jsObject, "name") as String?;
  final int? rssi = _JSUtil.getProperty(jsObject, "rssi") as int?;
  final int? txPower = _JSUtil.getProperty(jsObject, "txPower") as int?;
  final int? appearance = _JSUtil.getProperty(jsObject, "appearance") as int?;

  return WebAdvertisementReceivedEvent._(
      uuids: uuids,
      manufacturerData: manufacturerData,
      serviceData: serviceData,
      name: name,
      rssi: rssi,
      txPower: txPower,
      appearance: appearance,
      device: device);
}