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