scan static method

Implementation

static Stream<BrilliantScannedDevice> scan() async* {
  try {
    _log.info("Starting to scan for devices");

    await FlutterBluePlus.startScan(
      withServices: [
        Guid('7a230001-5475-a6a4-654c-8431f6ad49c4'),
        Guid('fe59'),
      ],
      // note: adding a shorter scan period to reflect
      // that it might be used for a short period at the
      // beginning of an app but not running in the background
      timeout: const Duration(seconds: 10),
      continuousUpdates: false,
      removeIfGone: null,
    );
  } catch (error) {
    _log.warning("Scanning failed. $error");
    throw BrilliantBluetoothException(error.toString());
  }

  yield* FlutterBluePlus.scanResults
      .where((results) => results.isNotEmpty)
      // TODO filter by name: "Frame"
      .map((results) {
    ScanResult nearestDevice = results[0];
    for (int i = 0; i < results.length; i++) {
      if (results[i].rssi > nearestDevice.rssi) {
        nearestDevice = results[i];
      }
    }

    _log.fine(() =>
        "Found ${nearestDevice.device.advName} rssi: ${nearestDevice.rssi}");

    return BrilliantScannedDevice(
      device: nearestDevice.device,
      rssi: nearestDevice.rssi,
    );
  });
}