forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectivity.dart
More file actions
112 lines (96 loc) · 3.6 KB
/
connectivity.dart
File metadata and controls
112 lines (96 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
/// Connection Status Check Result
///
/// WiFi: Device connected via Wi-Fi
/// Mobile: Device connected to cellular network
/// None: Device not connected to any network
enum ConnectivityResult { wifi, mobile, none }
class Connectivity {
/// Constructs a singleton instance of [Connectivity].
///
/// [Connectivity] is designed to work as a singleton.
// When a second instance is created, the first instance will not be able to listen to the
// EventChannel because it is overridden. Forcing the class to be a singleton class can prevent
// misusage of creating a second instance from a programmer.
factory Connectivity() {
if (_singleton == null) {
_singleton = Connectivity._();
}
return _singleton;
}
Connectivity._();
static Connectivity _singleton;
Stream<ConnectivityResult> _onConnectivityChanged;
@visibleForTesting
static const MethodChannel methodChannel = MethodChannel(
'plugins.flutter.io/connectivity',
);
@visibleForTesting
static const EventChannel eventChannel = EventChannel(
'plugins.flutter.io/connectivity_status',
);
/// Fires whenever the connectivity state changes.
Stream<ConnectivityResult> get onConnectivityChanged {
if (_onConnectivityChanged == null) {
_onConnectivityChanged = eventChannel
.receiveBroadcastStream()
.map((dynamic event) => _parseConnectivityResult(event));
}
return _onConnectivityChanged;
}
/// Checks the connection status of the device.
///
/// Do not use the result of this function to decide whether you can reliably
/// make a network request. It only gives you the radio status.
///
/// Instead listen for connectivity changes via [onConnectivityChanged] stream.
Future<ConnectivityResult> checkConnectivity() async {
final String result = await methodChannel.invokeMethod<String>('check');
return _parseConnectivityResult(result);
}
/// Obtains the wifi name (SSID) of the connected network
///
/// Please note that it DOESN'T WORK on emulators (returns null).
///
/// From android 8.0 onwards the GPS must be ON (high accuracy)
/// in order to be able to obtain the SSID.
Future<String> getWifiName() async {
String wifiName = await methodChannel.invokeMethod<String>('wifiName');
// as Android might return <unknown ssid>, uniforming result
// our iOS implementation will return null
if (wifiName == '<unknown ssid>') wifiName = null;
return wifiName;
}
/// Obtains the wifi BSSID of the connected network.
///
/// Please note that it DOESN'T WORK on emulators (returns null).
///
/// From Android 8.0 onwards the GPS must be ON (high accuracy)
/// in order to be able to obtain the BSSID.
Future<String> getWifiBSSID() async {
return await methodChannel.invokeMethod<String>('wifiBSSID');
}
/// Obtains the IP address of the connected wifi network
Future<String> getWifiIP() async {
return await methodChannel.invokeMethod<String>('wifiIPAddress');
}
Future<String> getMobileConnectionType() async {
return await methodChannel.invokeMethod<String>('getMobileConnectionType');
}
}
ConnectivityResult _parseConnectivityResult(String state) {
switch (state) {
case 'wifi':
return ConnectivityResult.wifi;
case 'mobile':
return ConnectivityResult.mobile;
case 'none':
default:
return ConnectivityResult.none;
}
}