|
| 1 | +// Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// ignore_for_file: public_member_api_docs |
| 6 | + |
| 7 | +import 'dart:async'; |
| 8 | +import 'dart:io'; |
| 9 | + |
| 10 | +import 'package:connectivity/connectivity.dart' |
| 11 | + show Connectivity, ConnectivityResult; |
| 12 | +import 'package:flutter/foundation.dart'; |
| 13 | +import 'package:flutter/material.dart'; |
| 14 | +import 'package:flutter/services.dart'; |
| 15 | +import 'package:wifi_info_flutter/wifi_info_flutter.dart'; |
| 16 | + |
| 17 | +// Sets a platform override for desktop to avoid exceptions. See |
| 18 | +// https://flutter.dev/desktop#target-platform-override for more info. |
| 19 | +void _enablePlatformOverrideForDesktop() { |
| 20 | + if (!kIsWeb && (Platform.isWindows || Platform.isLinux)) { |
| 21 | + debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +void main() { |
| 26 | + _enablePlatformOverrideForDesktop(); |
| 27 | + runApp(MyApp()); |
| 28 | +} |
| 29 | + |
| 30 | +class MyApp extends StatelessWidget { |
| 31 | + // This widget is the root of your application. |
| 32 | + @override |
| 33 | + Widget build(BuildContext context) { |
| 34 | + return MaterialApp( |
| 35 | + title: 'Flutter Demo', |
| 36 | + theme: ThemeData( |
| 37 | + primarySwatch: Colors.blue, |
| 38 | + ), |
| 39 | + home: MyHomePage(title: 'Flutter Demo Home Page'), |
| 40 | + ); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +class MyHomePage extends StatefulWidget { |
| 45 | + MyHomePage({Key key, this.title}) : super(key: key); |
| 46 | + |
| 47 | + final String title; |
| 48 | + |
| 49 | + @override |
| 50 | + _MyHomePageState createState() => _MyHomePageState(); |
| 51 | +} |
| 52 | + |
| 53 | +class _MyHomePageState extends State<MyHomePage> { |
| 54 | + String _connectionStatus = 'Unknown'; |
| 55 | + final Connectivity _connectivity = Connectivity(); |
| 56 | + final WifiInfo _wifiInfo = WifiInfo(); |
| 57 | + StreamSubscription<ConnectivityResult> _connectivitySubscription; |
| 58 | + |
| 59 | + @override |
| 60 | + void initState() { |
| 61 | + super.initState(); |
| 62 | + initConnectivity(); |
| 63 | + _connectivitySubscription = |
| 64 | + _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); |
| 65 | + } |
| 66 | + |
| 67 | + @override |
| 68 | + void dispose() { |
| 69 | + _connectivitySubscription.cancel(); |
| 70 | + super.dispose(); |
| 71 | + } |
| 72 | + |
| 73 | + // Platform messages are asynchronous, so we initialize in an async method. |
| 74 | + Future<void> initConnectivity() async { |
| 75 | + ConnectivityResult result; |
| 76 | + // Platform messages may fail, so we use a try/catch PlatformException. |
| 77 | + try { |
| 78 | + result = await _connectivity.checkConnectivity(); |
| 79 | + } on PlatformException catch (e) { |
| 80 | + print(e.toString()); |
| 81 | + } |
| 82 | + |
| 83 | + // If the widget was removed from the tree while the asynchronous platform |
| 84 | + // message was in flight, we want to discard the reply rather than calling |
| 85 | + // setState to update our non-existent appearance. |
| 86 | + if (!mounted) { |
| 87 | + return Future.value(null); |
| 88 | + } |
| 89 | + |
| 90 | + return _updateConnectionStatus(result); |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + Widget build(BuildContext context) { |
| 95 | + return Scaffold( |
| 96 | + appBar: AppBar( |
| 97 | + title: const Text('Connectivity example app'), |
| 98 | + ), |
| 99 | + body: Center(child: Text('Connection Status: $_connectionStatus')), |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + Future<void> _updateConnectionStatus(ConnectivityResult result) async { |
| 104 | + switch (result) { |
| 105 | + case ConnectivityResult.wifi: |
| 106 | + String wifiName, wifiBSSID, wifiIP; |
| 107 | + |
| 108 | + try { |
| 109 | + if (!kIsWeb && Platform.isIOS) { |
| 110 | + LocationAuthorizationStatus status = |
| 111 | + await _wifiInfo.getLocationServiceAuthorization(); |
| 112 | + if (status == LocationAuthorizationStatus.notDetermined) { |
| 113 | + status = await _wifiInfo.requestLocationServiceAuthorization(); |
| 114 | + } |
| 115 | + if (status == LocationAuthorizationStatus.authorizedAlways || |
| 116 | + status == LocationAuthorizationStatus.authorizedWhenInUse) { |
| 117 | + wifiName = await _wifiInfo.getWifiName(); |
| 118 | + } else { |
| 119 | + wifiName = await _wifiInfo.getWifiName(); |
| 120 | + } |
| 121 | + } else { |
| 122 | + wifiName = await _wifiInfo.getWifiName(); |
| 123 | + } |
| 124 | + } on PlatformException catch (e) { |
| 125 | + print(e.toString()); |
| 126 | + wifiName = "Failed to get Wifi Name"; |
| 127 | + } |
| 128 | + |
| 129 | + try { |
| 130 | + if (!kIsWeb && Platform.isIOS) { |
| 131 | + LocationAuthorizationStatus status = |
| 132 | + await _wifiInfo.getLocationServiceAuthorization(); |
| 133 | + if (status == LocationAuthorizationStatus.notDetermined) { |
| 134 | + status = await _wifiInfo.requestLocationServiceAuthorization(); |
| 135 | + } |
| 136 | + if (status == LocationAuthorizationStatus.authorizedAlways || |
| 137 | + status == LocationAuthorizationStatus.authorizedWhenInUse) { |
| 138 | + wifiBSSID = await _wifiInfo.getWifiBSSID(); |
| 139 | + } else { |
| 140 | + wifiBSSID = await _wifiInfo.getWifiBSSID(); |
| 141 | + } |
| 142 | + } else { |
| 143 | + wifiBSSID = await _wifiInfo.getWifiBSSID(); |
| 144 | + } |
| 145 | + } on PlatformException catch (e) { |
| 146 | + print(e.toString()); |
| 147 | + wifiBSSID = "Failed to get Wifi BSSID"; |
| 148 | + } |
| 149 | + |
| 150 | + try { |
| 151 | + wifiIP = await _wifiInfo.getWifiIP(); |
| 152 | + } on PlatformException catch (e) { |
| 153 | + print(e.toString()); |
| 154 | + wifiIP = "Failed to get Wifi IP"; |
| 155 | + } |
| 156 | + |
| 157 | + setState(() { |
| 158 | + _connectionStatus = '$result\n' |
| 159 | + 'Wifi Name: $wifiName\n' |
| 160 | + 'Wifi BSSID: $wifiBSSID\n' |
| 161 | + 'Wifi IP: $wifiIP\n'; |
| 162 | + }); |
| 163 | + break; |
| 164 | + case ConnectivityResult.mobile: |
| 165 | + case ConnectivityResult.none: |
| 166 | + setState(() => _connectionStatus = result.toString()); |
| 167 | + break; |
| 168 | + default: |
| 169 | + setState(() => _connectionStatus = 'Failed to get connectivity.'); |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments