WiFiFlutter| wifi_scan

Flutter Network pub.dev pub points popularity likes


This plugin allows Flutter apps to scan for nearby visible WiFi access points.

This plugin is part of WiFiFlutter suite, enabling various WiFi services for Flutter.

Platform Support

PlatformStatusMin. VersionAPINotes
Android✔️16 (J)Scan related APIs in WifiManager [Guide]For SDK >= 26(O) scans are throttled.
iOS✔️9.0No public API, requires special entitlements from AppleStub implementation with sane returns.

Usage

The entry point for the plugin is the singleton instance WiFiScan.instance.

Check support

You can check if the platform has Wi-Fi scan capabilities with WiFiScan.hasCapability API, as shown below:

void _scan() async {
  if(await WiFiScan.instance.hasCapability()){
    // can safely call scan related functionalities
  } else {
    // fallback mechanism, like - show user that "scan" is not possible 
  }
}

For more details, you can read documentation of WiFiScan.hasCapability.

Start scan

You can trigger full WiFi scan with WiFiScan.startScan API, as shown below:

void _startScan() async {
  // start full scan async-ly
  final error = await WiFiScan.instance.startScan(askPermissions: true);
  if (error != null) {
    switch(error) {
      // handle error for values of StartScanErrors
    }
  }
}

For more details, you can read documentation of WiFiScan.startScan, StartScanErrors and Result<ValueType, ErrorType>.

Get scanned results

You can get scanned results with WiFiScan.getScannedResults API, as shown below:

void _getScannedResults() async {
  // get scanned results
  final result = await WiFiScan.instance.getScannedResults(askPermissions: true);
  if (result.hasError){
    switch (error){
      // handle error for values of GetScannedResultErrors
    }
  } else {
    final accessPoints = result.value;
    // ...
  }
}

NOTE: getScannedResults API can be used independently of startScan API. This returns the latest available scanned results.

For more details, you can read documentation of WiFiScan.getScannedResults, WiFiAccessPoint, GetScannedResultsErrors and Result<ValueType, ErrorType>.

Get notified when scanned results available

You can get notified when new scanned results are available with WiFiScan.onScannedResultsAvailable API, as shown below:

// initialize accessPoints and subscription
List<WiFiAccessPoint> accessPoints = [];
StreamSubscription<Result<List<WiFiAccessPoint>, GetScannedResultErrors>>? subscription;

void _startListeningToScannedResults() async {
  // listen to onScannedResultsAvailable stream
  subscription = WiFiScan.instance.onScannedResultsAvailable.listen((result) {
    if (result.hasError){
      switch (error){
      // handle error for values of GetScannedResultErrors
      }  
    } else {
      // update accessPoints
      setState(() => accessPoints = result.value);
    }
  });
}

// make sure to cancel subscription after you are done
@override
dispose() {
  super.dispose();
  subscription?.cancel();
}

Additionally, WiFiScan.onScannedResultsAvailable API can also be used with Flutter's StreamBuilder widget.

NOTE: onScannedResultsAvailable API can be used independently of startScan API. The notification can also be result of a full scan performed by platform or other app.

For more details, you can read documentation of WiFiScan.onScannedResultsAvailable, WiFiAccessPoint, GetScannedResultsErrors and Result<ValueType, ErrorType>.

Resources

Issues and feedback

Please file WiFiFlutter specific issues, bugs, or feature requests in our issue tracker.

To contribute a change to this plugin, please review plugin checklist for 1.0, our contribution guide and open a pull request.

Contributors ✨

Thanks goes to these 💖 people for their contributions.

This project follows the all-contributors specification. Contributions of any kind welcome!

Libraries

wifi_scan