bluetoothScanEventStream method

EventChannel bluetoothScanEventStream()

Provides an event stream for Bluetooth device scanning updates.

Returns an EventChannel that broadcasts Bluetooth scanning events including device discoveries, scan status changes, and error conditions.

Returns:

  • EventChannel: Stream of Bluetooth scanning events

Usage:

final bluetoothStream = communicator.bluetoothScanEventStream();
bluetoothStream.receiveBroadcastStream().listen((event) {
  if (event == "SCANNING_STARTED") {
    print('Bluetooth scan started');
    // Update UI to show scanning indicator
  } else if (event == "SCANNING_STOPPED") {
    print('Bluetooth scan stopped');
  } else if (event == "DEVICE_NOT_SUPPORTED") {
    print('Device does not support Bluetooth LE');
  } else if (event == "PERMISSION_NOT_GRANTED") {
    print('Bluetooth permissions not granted');
  } else {
    // Parse device discovery event
    try {
      final device = CustomBluetoothDevice.fromJson(jsonDecode(event));
      print('Found device: ${device.name}');
    } catch (e) {
      print('Failed to parse device data: $e');
    }
  }
});

Event Types:

  • "SCANNING_STARTED": Bluetooth scan has begun
  • "SCANNING_STOPPED": Bluetooth scan has ended
  • "REQUEST_USER_TO_SWITCH_ON_BLUETOOTH": Bluetooth is disabled
  • "PERMISSION_NOT_GRANTED": Missing Bluetooth permissions
  • "DEVICE_NOT_SUPPORTED": Device doesn't support Bluetooth LE
  • JSON device data: When a device is discovered

Important Notes:

  • Requires Bluetooth permissions
  • Device must support Bluetooth LE
  • Events continue until scanning is stopped

Implementation

EventChannel bluetoothScanEventStream() {
  return kruzr_comm.bluetoothScanEventStream;
}