scanPSDevices method

void scanPSDevices({
  1. String? prefix,
  2. int duration = 5000,
  3. bool exactMatch = false,
  4. ScanStateCallback? scanState,
  5. ScanCallback? onResult,
  6. FailureCallback? onFailure,
})

Starts scanning for Bluetooth devices.

This method initiates a scan for nearby Bluetooth devices with an optional prefix to filter results. The scan will continue for a specified duration and can trigger callbacks to indicate scan state changes, results, or failures.

Parameters:

  • prefix: An optional string prefix to filter scanned device names.
  • duration: The duration for the scan in milliseconds (default is 5000).
  • exactMatch: A boolean indicating whether to match devices exactly with the specified prefix (default is false).
  • scanState: An optional callback that is called to indicate the scan state.
  • onResult: A callback function that is called with each scan result.
  • onFailure: A callback function that is called if the scan fails.

Note: This method will check if Bluetooth is supported on the device. If not, it will call the onFailure callback with an appropriate exception. Ensure to handle the callbacks properly for effective scanning.

Implementation

void scanPSDevices(
    {String? prefix,
    int duration = 5000,
    bool exactMatch = false,
    ScanStateCallback? scanState,
    ScanCallback? onResult,
    FailureCallback? onFailure}) async {
  scanState?.call(true);
  _adapterStateSubscription?.cancel();
  if (await FlutterBluePlus.isSupported == false) {
    onFailure?.call(Exception(notSupportBle));
    return;
  }
  _adapterStateSubscription = FlutterBluePlus.adapterState.listen((BluetoothAdapterState state) async {
    if (state != BluetoothAdapterState.on) {
      if (Platform.isAndroid) {
        await FlutterBluePlus.turnOn();
      } else {
        onFailure?.call(Exception(bleDisable));
      }
    }
    if (state == BluetoothAdapterState.on) {
      _scan(prefix, duration, exactMatch, scanState, onResult);
    }
  });
}