import 'dart:async'; import 'package:flutter/services.dart'; import 'dart:convert'; import 'dart:io' show Platform; // Note support only Android // for iOS https://github.com/lou-lan/SmartConfig class FlutterSmartconfig { static const platform = const MethodChannel("plugins.flutter.io/flutter_smartconfig"); static Future stopConfigure() async { if (Platform.isAndroid) { try { await platform.invokeMethod('stopSmartConfig'); } on PlatformException catch (e) { return Future>(() { throw new PlatformException( code: "Failed to get connected WiFi name: '${e.message}'."); }); } } else { return Future>(() { throw new PlatformException(code: "only Android is supported"); }); } } static Future configureEsp( {String ssid, String bssid, String password, bool multicast, Duration timeout}) async { if (Platform.isAndroid) { try { // Change if required. const String deviceCount = "1"; // the expect result count String broadcast = (multicast != null && multicast) ? "0" : "1"; // broadcast (1) or multicast (0) Duration _kLongTimeout = timeout ?? const Duration(seconds: 20); final String result = await platform.invokeMethod('startSmartConfig', { 'ssid': ssid, 'bssid': bssid, 'pass': password, 'deviceCount': deviceCount, 'broadcast': broadcast, }).timeout(_kLongTimeout); final parsed = json.decode(result); final devices = parsed["devices"]; return Future(() { return devices; }); } on PlatformException catch (e) { return Future>(() { throw new PlatformException( code: "Failed to configure: '${e.message}'."); }); } } else { return Future>(() { throw new PlatformException(code: "only Android is supported"); }); } } // Build.VERSION.SDK_INT >= 28 needs Manifest.permission.ACCESS_COARSE_LOCATION static Future> getConnectedWiFiInfo() async { if (Platform.isAndroid) { try { String wiFiInfo = await platform.invokeMethod('getConnectedWiFiInfo'); final parsed = json.decode(wiFiInfo); return Future>(() { return { "ssid": parsed["ssid"], "bssid": parsed["bssid"], "is5G": parsed["is5G"] }; }); } on PlatformException catch (e) { return Future>(() { throw new PlatformException( code: "Failed to get connected WiFi name: '${e.message}'."); }); } } else { return Future>(() { throw new PlatformException(code: "only Android is supported"); }); } } }