requestLEScan method

  1. @override
Future<void> requestLEScan(
  1. RequestBleDeviceParams options,
  2. void callback(
    1. ScanResult result
    )
)
override

Implementation

@override
Future<void> requestLEScan(RequestBleDeviceParams options,
    void Function(ScanResult result) callback) async {
  var optionsMap = options.getMap();

  optionsMap['callbackName'] = 'requestLEScan';
  var cancelStream = false;

  var brodcastStream =
      _eventChannel.receiveBroadcastStream(optionsMap).listen((event) {
    if (event == null) {
      return;
    }
    if (event["status"] != null) {
      cancelStream = true;
      return;
    }

    var bleDeviceMap = event["device"];
    var scanResults = ScanResult(
      device: BleDevice(
        deviceId: bleDeviceMap["deviceId"]!!,
        name: bleDeviceMap["name"],
        uuids: bleDeviceMap["uuids"]?.split(':'),
      ),
      localName: event["localName"],
      rssi: event["rssi"],
      txPower: event["txPower"],
      manufacturerData: event["manufacturerData"]?.cast<String, String>(),
      uuids: event["uuids"]?.split(':'),
      rawAdvertisement: event["rawAdvertisement"],
    );
    callback(scanResults);
  });

  var timeout = Duration(milliseconds: options.timeout ?? 10000);
  Timer(timeout, () => brodcastStream.cancel());

  Timer.periodic(const Duration(milliseconds: 500), (timer) {
    if (cancelStream) {
      brodcastStream.cancel();
      timer.cancel();
    }
  });
}