### Import the package Source: https://pub.dev/packages/wifi_scan/install Include the library in your Dart files to access the wifi_scan functionality. ```dart import 'package:wifi_scan/wifi_scan.dart'; ``` -------------------------------- ### Implement WiFi scanning in Flutter Source: https://pub.dev/packages/wifi_scan/example Demonstrates how to check for scan permissions, initiate a scan, and manage result streams using the wifi_scan plugin. ```dart import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:wifi_scan/wifi_scan.dart'; void main() { runApp(const MyApp()); } /// Example app for wifi_scan plugin. class MyApp extends StatefulWidget { /// Default constructor for [MyApp] widget. const MyApp({Key? key}) : super(key: key); @override State createState() => _MyAppState(); } class _MyAppState extends State { List accessPoints = []; StreamSubscription>? subscription; bool shouldCheckCan = true; bool get isStreaming => subscription != null; Future _startScan(BuildContext context) async { // check if "can" startScan if (shouldCheckCan) { // check if can-startScan final can = await WiFiScan.instance.canStartScan(); // if can-not, then show error if (can != CanStartScan.yes) { if (context.mounted) kShowSnackBar(context, "Cannot start scan: $can"); return; } } // call startScan API final result = await WiFiScan.instance.startScan(); if (context.mounted) kShowSnackBar(context, "startScan: $result"); // reset access points. setState(() => accessPoints = []); } Future _canGetScannedResults(BuildContext context) async { if (shouldCheckCan) { // check if can-getScannedResults final can = await WiFiScan.instance.canGetScannedResults(); // if can-not, then show error if (can != CanGetScannedResults.yes) { if (context.mounted) { kShowSnackBar(context, "Cannot get scanned results: $can"); } accessPoints = []; return false; } } return true; } Future _getScannedResults(BuildContext context) async { if (await _canGetScannedResults(context)) { // get scanned results final results = await WiFiScan.instance.getScannedResults(); setState(() => accessPoints = results); } } Future _startListeningToScanResults(BuildContext context) async { if (await _canGetScannedResults(context)) { subscription = WiFiScan.instance.onScannedResultsAvailable .listen((result) => setState(() => accessPoints = result)); } } void _stopListeningToScanResults() { subscription?.cancel(); setState(() => subscription = null); } @override void dispose() { super.dispose(); // stop subscription for scanned results _stopListeningToScanResults(); } // build toggle with label Widget _buildToggle({ String? label, bool value = false, ValueChanged? onChanged, Color? activeColor, }) => Row( children: [ if (label != null) Text(label), Switch(value: value, onChanged: onChanged, activeColor: activeColor), ], ); ``` -------------------------------- ### Add dependency via command line Source: https://pub.dev/packages/wifi_scan/install Use the Flutter CLI to automatically add the package to your project. ```bash $ flutter pub add wifi_scan ``` -------------------------------- ### Create Access Point Tile Widget Source: https://pub.dev/packages/wifi_scan/example Displays a list tile for an access point that opens a dialog with detailed network information when tapped. ```dart class _AccessPointTile extends StatelessWidget { final WiFiAccessPoint accessPoint; const _AccessPointTile({Key? key, required this.accessPoint}) : super(key: key); // build row that can display info, based on label: value pair. Widget _buildInfo(String label, dynamic value) => Container( decoration: const BoxDecoration( border: Border(bottom: BorderSide(color: Colors.grey)), ), child: Row( children: [ Text( "$label: ", style: const TextStyle(fontWeight: FontWeight.bold), ), Expanded(child: Text(value.toString())) ], ), ); @override Widget build(BuildContext context) { final title = accessPoint.ssid.isNotEmpty ? accessPoint.ssid : "**EMPTY**"; final signalIcon = accessPoint.level >= -80 ? Icons.signal_wifi_4_bar : Icons.signal_wifi_0_bar; return ListTile( visualDensity: VisualDensity.compact, leading: Icon(signalIcon), title: Text(title), subtitle: Text(accessPoint.capabilities), onTap: () => showDialog( context: context, builder: (context) => AlertDialog( title: Text(title), content: Column( mainAxisSize: MainAxisSize.min, children: [ _buildInfo("BSSDI", accessPoint.bssid), _buildInfo("Capability", accessPoint.capabilities), _buildInfo("frequency", "${accessPoint.frequency}MHz"), _buildInfo("level", accessPoint.level), _buildInfo("standard", accessPoint.standard), _buildInfo( "centerFrequency0", "${accessPoint.centerFrequency0}MHz"), _buildInfo( "centerFrequency1", "${accessPoint.centerFrequency1}MHz"), _buildInfo("channelWidth", accessPoint.channelWidth), _buildInfo("isPasspoint", accessPoint.isPasspoint), _buildInfo( "operatorFriendlyName", accessPoint.operatorFriendlyName), _buildInfo("venueName", accessPoint.venueName), _buildInfo("is80211mcResponder", accessPoint.is80211mcResponder), ], ), ), ), ); } } ``` -------------------------------- ### Build WiFi Scan Dashboard UI Source: https://pub.dev/packages/wifi_scan/example Constructs the main application screen with controls for scanning, retrieving results, and streaming WiFi data. ```dart @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), actions: [ _buildToggle( label: "Check can?", value: shouldCheckCan, onChanged: (v) => setState(() => shouldCheckCan = v), activeColor: Colors.purple) ], ), body: Builder( builder: (context) => Padding( padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 20), child: Column( mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ElevatedButton.icon( icon: const Icon(Icons.perm_scan_wifi), label: const Text('SCAN'), onPressed: () async => _startScan(context), ), ElevatedButton.icon( icon: const Icon(Icons.refresh), label: const Text('GET'), onPressed: () async => _getScannedResults(context), ), _buildToggle( label: "STREAM", value: isStreaming, onChanged: (shouldStream) async => shouldStream ? await _startListeningToScanResults(context) : _stopListeningToScanResults(), ), ], ), const Divider(), Flexible( child: Center( child: accessPoints.isEmpty ? const Text("NO SCANNED RESULTS") : ListView.builder( itemCount: accessPoints.length, itemBuilder: (context, i) => _AccessPointTile(accessPoint: accessPoints[i])), ), ), ], ), ), ), ), ); } } ``` -------------------------------- ### Trigger a WiFi scan Source: https://pub.dev/packages/wifi_scan Initiates a full WiFi scan after verifying platform support and permissions using canStartScan. ```dart void _startScan() async { // check platform support and necessary requirements final can = await WiFiScan.instance.canStartScan(askPermissions: true); switch(can) { case CanStartScan.yes: // start full scan async-ly final isScanning = await WiFiScan.instance.startScan(); //... break; // ... handle other cases of CanStartScan values } } ``` -------------------------------- ### Retrieve scanned WiFi results Source: https://pub.dev/packages/wifi_scan Fetches the latest available scanned access points independently of the startScan trigger. ```dart void _getScannedResults() async { // check platform support and necessary requirements final can = await WiFiScan.instance.canGetScannedResults(askPermissions: true); switch(can) { case CanGetScannedResults.yes: // get scanned results final accessPoints = await WiFiScan.instance.getScannedResults(); // ... break; // ... handle other cases of CanGetScannedResults values } } ``` -------------------------------- ### Display Debug Snackbar Source: https://pub.dev/packages/wifi_scan/example Utility function to show a snackbar message and print it to the console in debug mode. ```dart void kShowSnackBar(BuildContext context, String message) { if (kDebugMode) print(message); ScaffoldMessenger.of(context) ..hideCurrentSnackBar() ..showSnackBar(SnackBar(content: Text(message))); } ``` -------------------------------- ### Add dependency to pubspec.yaml Source: https://pub.dev/packages/wifi_scan/install Manually add the package version to the dependencies section of your pubspec.yaml file. ```yaml dependencies: wifi_scan: ^0.4.1+2 ``` -------------------------------- ### Listen for scanned results updates Source: https://pub.dev/packages/wifi_scan Subscribes to the onScannedResultsAvailable stream to receive updates when new results are available. Ensure the subscription is cancelled in the dispose method. ```dart // initialize accessPoints and subscription List accessPoints = []; StreamSubscription>? subscription; void _startListeningToScannedResults() async { // check platform support and necessary requirements final can = await WiFiScan.instance.canGetScannedResults(askPermissions: true); switch(can) { case CanGetScannedResults.yes: // listen to onScannedResultsAvailable stream subscription = WiFiScan.instance.onScannedResultsAvailable.listen((results) { // update accessPoints setState(() => accessPoints = results); }); // ... break; // ... handle other cases of CanGetScannedResults values } } // make sure to cancel subscription after you are done @override dispose() { super.dispose(); subscription?.cancel(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.