diff --git a/packages/wifi_scan/lib/src/accesspoint.dart b/packages/wifi_scan/lib/src/accesspoint.dart index 801c7db2..eaa0544f 100644 --- a/packages/wifi_scan/lib/src/accesspoint.dart +++ b/packages/wifi_scan/lib/src/accesspoint.dart @@ -1,5 +1,3 @@ -part of '../wifi_scan.dart'; - /// WiFi standards. enum WiFiStandards { /// Unknown. @@ -148,7 +146,7 @@ class WiFiAccessPoint { /// ranging requests. final bool? is80211mcResponder; - WiFiAccessPoint._fromMap(Map map) + WiFiAccessPoint.fromMap(Map map) : ssid = map["ssid"], bssid = map["bssid"], capabilities = map["capabilities"], diff --git a/packages/wifi_scan/lib/src/can.dart b/packages/wifi_scan/lib/src/can.dart index 8adef95a..86b47b64 100644 --- a/packages/wifi_scan/lib/src/can.dart +++ b/packages/wifi_scan/lib/src/can.dart @@ -1,5 +1,3 @@ -part of '../wifi_scan.dart'; - /// Result for [WiFiScan.canStartScan] method. enum CanStartScan { /// Functionality is not supported. @@ -30,7 +28,7 @@ enum CanStartScan { failed, } -CanStartScan _deserializeCanStartScan(int? canCode) { +CanStartScan deserializeCanStartScan(int? canCode) { switch (canCode) { case 0: return CanStartScan.notSupported; @@ -75,7 +73,7 @@ enum CanGetScannedResults { noLocationServiceDisabled, } -CanGetScannedResults _deserializeCanGetScannedResults(int? canCode) { +CanGetScannedResults deserializeCanGetScannedResults(int? canCode) { switch (canCode) { case 0: return CanGetScannedResults.notSupported; diff --git a/packages/wifi_scan/lib/wifi_scan.dart b/packages/wifi_scan/lib/wifi_scan.dart index f52c8f5d..49c8b822 100644 --- a/packages/wifi_scan/lib/wifi_scan.dart +++ b/packages/wifi_scan/lib/wifi_scan.dart @@ -1,89 +1,3 @@ -import 'dart:async'; - -import 'package:flutter/services.dart'; - -part 'src/accesspoint.dart'; -part 'src/can.dart'; - -/// The `wifi_scan` plugin entry point. -/// -/// To get a new instance, call [WiFiScan.instance]. -class WiFiScan { - WiFiScan._(); - - /// Singleton instance of [WiFiScan]. - static final instance = WiFiScan._(); - - final _channel = const MethodChannel('wifi_scan'); - final _scannedResultsAvailableChannel = - const EventChannel('wifi_scan/onScannedResultsAvailable'); - Stream>? _onScannedResultsAvailable; - - /// Checks if it is ok to invoke [startScan]. - /// - /// Necesearry platform requirements, like permissions dependent services, - /// configuration, etc are checked. - /// - /// Set [askPermissions] flag to ask user for necessary permissions. - Future canStartScan({bool askPermissions = true}) async { - final canCode = await _channel.invokeMethod("canStartScan", { - "askPermissions": askPermissions, - }); - return _deserializeCanStartScan(canCode); - } - - /// Request a Wi-Fi scan. - /// - /// Return value indicates if the "scan" trigger successed. - /// - /// Should call [canStartScan] as a check before calling this method. - Future startScan() async { - final isSucess = await _channel.invokeMethod("startScan"); - return isSucess!; - } - - /// Checks if it is ok to invoke [getScannedResults] or [onScannedResultsAvailable]. - /// - /// Necesearry platform requirements, like permissions dependent services, - /// configuration, etc are checked. - /// - /// Set [askPermissions] flag to ask user for necessary permissions. - Future canGetScannedResults( - {bool askPermissions = true}) async { - final canCode = await _channel.invokeMethod("canGetScannedResults", { - "askPermissions": askPermissions, - }); - return _deserializeCanGetScannedResults(canCode); - } - - /// Get scanned access point. - /// - /// This are cached accesss points from most recently performed scan. - /// - /// Should call [canGetScannedResults] as a check before calling this method. - Future> getScannedResults() async { - final scannedResults = - await _channel.invokeListMethod("getScannedResults"); - return scannedResults! - .map((map) => WiFiAccessPoint._fromMap(map)) - .toList(growable: false); - } - - /// Fires whenever new scanned results are available. - /// - /// New results are added to stream when platform performs the scan, either by - /// itself or trigger with [startScan]. - /// - /// Should call [canGetScannedResults] as a check before calling this method. - Stream> get onScannedResultsAvailable => - _onScannedResultsAvailable ??= - _scannedResultsAvailableChannel.receiveBroadcastStream().map((event) { - if (event is Error) throw event; - if (event is List) { - return event - .map((map) => WiFiAccessPoint._fromMap(map)) - .toList(growable: false); - } - return const []; - }); -} +export 'src/accesspoint.dart'; +export 'src/can.dart'; +export 'wifi_scan_platform_interface.dart'; \ No newline at end of file diff --git a/packages/wifi_scan/lib/wifi_scan_method_channel.dart b/packages/wifi_scan/lib/wifi_scan_method_channel.dart new file mode 100644 index 00000000..a3bc68a9 --- /dev/null +++ b/packages/wifi_scan/lib/wifi_scan_method_channel.dart @@ -0,0 +1,54 @@ +import 'package:flutter/services.dart'; +import 'package:wifi_scan/wifi_scan.dart'; + +class MethodChannelWifiScan extends WiFiScan { + final _channel = const MethodChannel('wifi_scan'); + final _scannedResultsAvailableChannel = + const EventChannel('wifi_scan/onScannedResultsAvailable'); + Stream>? _onScannedResultsAvailable; + + @override + Future canStartScan({bool askPermissions = true}) async { + final canCode = await _channel.invokeMethod("canStartScan", { + "askPermissions": askPermissions, + }); + return deserializeCanStartScan(canCode); + } + + @override + Future startScan() async { + final isSucess = await _channel.invokeMethod("startScan"); + return isSucess!; + } + + @override + Future canGetScannedResults( + {bool askPermissions = true}) async { + final canCode = await _channel.invokeMethod("canGetScannedResults", { + "askPermissions": askPermissions, + }); + return deserializeCanGetScannedResults(canCode); + } + + @override + Future> getScannedResults() async { + final scannedResults = + await _channel.invokeListMethod("getScannedResults"); + return scannedResults! + .map((map) => WiFiAccessPoint.fromMap(map)) + .toList(growable: false); + } + + @override + Stream> get onScannedResultsAvailable => + _onScannedResultsAvailable ??= + _scannedResultsAvailableChannel.receiveBroadcastStream().map((event) { + if (event is Error) throw event; + if (event is List) { + return event + .map((map) => WiFiAccessPoint.fromMap(map)) + .toList(growable: false); + } + return const []; + }); +} \ No newline at end of file diff --git a/packages/wifi_scan/lib/wifi_scan_platform_interface.dart b/packages/wifi_scan/lib/wifi_scan_platform_interface.dart new file mode 100644 index 00000000..7e3c8b53 --- /dev/null +++ b/packages/wifi_scan/lib/wifi_scan_platform_interface.dart @@ -0,0 +1,76 @@ +import 'dart:async'; + +import 'package:plugin_platform_interface/plugin_platform_interface.dart'; +import 'package:wifi_scan/src/accesspoint.dart'; +import 'package:wifi_scan/src/can.dart'; +import 'package:wifi_scan/wifi_scan_method_channel.dart'; + +abstract class WiFiScan extends PlatformInterface { + WiFiScan() : super(token: _token); + static final Object _token = Object(); + + static WiFiScan _instance = MethodChannelWifiScan(); + + /// The default instance of [WiFiScan] to use. + /// + /// Defaults to [MethodChannelWifiScan]. + static WiFiScan get instance => _instance; + + /// Platform-specific plugins should set this with their own platform-specific + /// class that extends [WiFiScan] when they register themselves. + // TODO(amirh): Extract common platform interface logic. + // https://github.com/flutter/flutter/issues/43368 + static set instance(WiFiScan instance) { + PlatformInterface.verifyToken(instance, _token); + _instance = instance; + } + + /// Checks if it is ok to invoke [startScan]. + /// + /// Necessary platform requirements, like permissions dependent services, + /// configuration, etc are checked. + /// + /// Set [askPermissions] flag to ask user for necessary permissions. + Future canStartScan({bool askPermissions = true}) async { + throw UnimplementedError('canStartScan() has not been implemented.'); + } + + /// Request a Wi-Fi scan. + /// + /// Return value indicates if the "scan" trigger succeeded. + /// + /// Should call [canStartScan] as a check before calling this method. + Future startScan() async { + throw UnimplementedError('canStartScan() has not been implemented.'); + } + + /// Checks if it is ok to invoke [getScannedResults] or [onScannedResultsAvailable]. + /// + /// Necessary platform requirements, like permissions dependent services, + /// configuration, etc are checked. + /// + /// Set [askPermissions] flag to ask user for necessary permissions. + Future canGetScannedResults( + {bool askPermissions = true}) async { + throw UnimplementedError('canGetScannedResults() has not been implemented.'); + } + + /// Get scanned access point. + /// + /// This are cached access points from most recently performed scan. + /// + /// Should call [canGetScannedResults] as a check before calling this method. + Future> getScannedResults() async { + throw UnimplementedError('getScannedResults() has not been implemented.'); + } + + /// Fires whenever new scanned results are available. + /// + /// New results are added to stream when platform performs the scan, either by + /// itself or trigger with [startScan]. + /// + /// Should call [canGetScannedResults] as a check before calling this method. + Stream> get onScannedResultsAvailable { + throw UnimplementedError('onScannedResultsAvailable has not been implemented.'); + } +} \ No newline at end of file diff --git a/packages/wifi_scan/pubspec.yaml b/packages/wifi_scan/pubspec.yaml index b30bcd89..cde6e6c5 100644 --- a/packages/wifi_scan/pubspec.yaml +++ b/packages/wifi_scan/pubspec.yaml @@ -10,6 +10,7 @@ environment: dependencies: flutter: sdk: flutter + plugin_platform_interface: ^2.1.8 dev_dependencies: flutter_test: