startBluetoothScan method

Future<List<Vehicle>> startBluetoothScan()

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.startBluetoothScan();
  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:

  • Scanning consumes battery power
  • Remember to call stopBluetoothScan when done
  • Some devices may not be discoverable

Implementation

Future<List<Vehicle>> startBluetoothScan() async {
  try {
    return await kruzr_comm.startBluetoothScan();
  } on PlatformException catch (e, stackTrace) {
    if (kDebugMode) {
      print("Error in startBluetoothScan");
      print(stackTrace);
      print(e);
    }
    return Future.error({"code": e.code, "message": e.message, "details": e.details});
  } on Exception catch (e, stackTrace) {
    if (kDebugMode) {
      print("Error in startBluetoothScan");
      print(stackTrace);
      print(e);
    }
    return Future.error(PlatformException(code: "PLUGIN_ERROR", message: e.toString()));
  }
}