scanForNearbyDevices method

Future<List<NearbyDevice>> scanForNearbyDevices()

Starts scanning for nearby Bluetooth devices.

Initiates Bluetooth LE scanning to discover nearby devices. Results are delivered through the bluetoothScanEventStream.

Usage:

try {
  // Set up event listener first
  final stream = communicator.bluetoothScanEventStream();
  stream.receiveBroadcastStream().listen((event) {
    // Handle scan events
  });

  // Start scanning
  await communicator.scanForNearbyDevices();
  print('Bluetooth scan started');
} catch (e) {
  print('Failed to start scan: $e');
}

Throws:

  • Future.error("Unable to scan for device"): When scan start fails

Prerequisites:

  • Bluetooth must be enabled
  • Location permissions must be granted (Android requirement)
  • Bluetooth permissions must be granted

Important Notes:

Implementation

Future<List<NearbyDevice>> scanForNearbyDevices() async {
  try {
    return await kruzr_comm.scanForNearbyDevices();
  } on Exception catch (e, stackTrace) {
    if (kDebugMode) {
      print("Error in scanForNearbyDevices");
      print(stackTrace);
      print(e);
    }
    return Future.error("Unable to scan for device");
  }
}