-
Notifications
You must be signed in to change notification settings - Fork 170
/
wifi_iot.dart
executable file
·422 lines (369 loc) · 13.3 KB
/
wifi_iot.dart
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
enum WIFI_AP_STATE { WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED }
const MethodChannel _channel = const MethodChannel('wifi_iot');
const EventChannel _eventChannel = const EventChannel('plugins.wififlutter.io/wifi_scan');
class WiFiForIoTPlugin {
static Future<bool> isWiFiAPEnabled() async {
Map<String, String> htArguments = new Map();
bool bResult;
try {
bResult = await _channel.invokeMethod('isWiFiAPEnabled', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return (bResult != null && bResult);
}
static void setWiFiAPEnabled(bool state) async {
Map<String, bool> htArguments = new Map();
htArguments["state"] = state;
try {
await _channel.invokeMethod('setWiFiAPEnabled', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
}
static Future<bool> isWiFiAPSSIDHidden() async {
Map<String, String> htArguments = new Map();
bool bResult;
try {
bResult = await _channel.invokeMethod('isSSIDHidden', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return (bResult != null && bResult);
}
static setWiFiAPSSIDHidden(bool hidden) async {
Map<String, bool> htArguments = new Map();
htArguments["hidden"] = hidden;
try {
await _channel.invokeMethod('setSSIDHidden', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
}
static Future<int> getWiFiAPState() async {
Map<String, String> htArguments = new Map();
int iResult;
try {
iResult = await _channel.invokeMethod('getWiFiAPState', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return iResult;
}
static Future<List<APClient>> getClientList(bool onlyReachables, int reachableTimeout) async {
Map<String, Object> htArguments = new Map();
htArguments["onlyReachables"] = onlyReachables;
htArguments["reachableTimeout"] = reachableTimeout;
String sResult;
List<APClient> htResult = new List();
try {
sResult = await _channel.invokeMethod('getClientList', htArguments);
htResult = APClient.parse(sResult);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return htResult;
}
static void setWiFiAPConfiguration(Object poWiFiConfig) async {
Map<String, bool> htArguments = new Map();
htArguments["wifi_config"] = poWiFiConfig;
await _channel.invokeMethod('setWiFiAPConfiguration', htArguments);
}
static Future<String> getWiFiAPSSID() async {
Map<String, String> htArguments = new Map();
String sResult;
try {
sResult = await _channel.invokeMethod('getWiFiAPSSID', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return sResult;
}
static setWiFiAPSSID(String psSSID) async {
Map<String, String> htArguments = new Map();
htArguments["ssid"] = psSSID;
try {
await _channel.invokeMethod('setWiFiAPSSID', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
}
static Future<String> getWiFiAPPreSharedKey() async {
Map<String, String> htArguments = new Map();
String sResult;
try {
sResult = await _channel.invokeMethod('getWiFiAPPreSharedKey', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return sResult;
}
static setWiFiAPPreSharedKey(String psPreSharedKey) async {
Map<String, String> htArguments = new Map();
htArguments["preSharedKey"] = psPreSharedKey;
try {
await _channel.invokeMethod('setWiFiAPPreSharedKey', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
}
static Stream<List<WifiNetwork>> _onWifiScanResultReady;
static Stream<List<WifiNetwork>> get onWifiScanResultReady{
if(_onWifiScanResultReady == null){
_onWifiScanResultReady = _eventChannel
.receiveBroadcastStream()
.map((dynamic event) => WifiNetwork.parse(event));
}
return _onWifiScanResultReady;
}
static Future<List<WifiNetwork>> _loadWifiList() async {
Map<String, String> htArguments = new Map();
String sResult;
List<WifiNetwork> htResult = new List();
try {
sResult = await _channel.invokeMethod('loadWifiList', htArguments);
htResult = WifiNetwork.parse(sResult);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return htResult;
}
static Future<List<WifiNetwork>> loadWifiList() async {
List<WifiNetwork> result = (await _loadWifiList() ?? new List<WifiNetwork>());
if(result.length >= 1) return result;
result.clear();
return await WiFiForIoTPlugin.onWifiScanResultReady.first;
}
static void forceWifiUsage(bool useWifi) async {
Map<String, bool> htArguments = new Map();
htArguments["useWifi"] = useWifi;
try {
await _channel.invokeMethod('forceWifiUsage', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
}
static Future<bool> isEnabled() async {
Map<String, String> htArguments = new Map();
bool bResult;
try {
bResult = await _channel.invokeMethod('isEnabled', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return (bResult != null && bResult);
}
static setEnabled(bool state) async {
Map<String, bool> htArguments = new Map();
htArguments["state"] = state;
try {
await _channel.invokeMethod('setEnabled', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
}
static Future<bool> connect(String ssid, {String password, NetworkSecurity security = NetworkSecurity.NONE, bool joinOnce = true}) async {
if (!await isEnabled()) await setEnabled(true);
bool bResult;
try {
bResult = await _channel.invokeMethod('connect', {
"ssid": ssid.toString(),
"password": password.toString(),
"join_once": joinOnce,
"security": security?.toString()?.substring('$NetworkSecurity'.length + 1),
});
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return (bResult != null && bResult);
}
static Future<bool> findAndConnect(String ssid, {String password, bool joinOnce = true}) async {
if (!await isEnabled()) {
await setEnabled(true);
}
// Map<String, Object> htArguments = new Map();
// htArguments["ssid"] = ssid;
// htArguments["password"] = password;
// if (joinOnce != null && isWep != null) {
// htArguments["join_once"] = joinOnce;
// htArguments["is_wep"] = isWep;
// }
bool bResult;
try {
bResult = await _channel.invokeMethod('findAndConnect', {
"ssid": ssid.toString(),
"password": password.toString(),
"join_once": joinOnce,
});
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return (bResult != null && bResult);
}
static Future<bool> isConnected() async {
Map<String, String> htArguments = new Map();
bool bResult;
try {
bResult = await _channel.invokeMethod('isConnected', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return (bResult != null && bResult);
}
static disconnect() async {
Map<String, bool> htArguments = new Map();
try {
await _channel.invokeMethod('disconnect', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
}
static Future<String> getSSID() async {
Map<String, String> htArguments = new Map();
String sResult;
try {
sResult = await _channel.invokeMethod('getSSID', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return sResult;
}
static Future<String> getBSSID() async {
Map<String, String> htArguments = new Map();
String sResult;
try {
sResult = await _channel.invokeMethod('getBSSID', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return sResult;
}
static Future<int> getCurrentSignalStrength() async {
Map<String, String> htArguments = new Map();
int iResult;
try {
iResult = await _channel.invokeMethod('getCurrentSignalStrength', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return iResult;
}
static Future<int> getFrequency() async {
Map<String, String> htArguments = new Map();
int iResult;
try {
iResult = await _channel.invokeMethod('getFrequency', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return iResult;
}
static Future<String> getIP() async {
Map<String, String> htArguments = new Map();
String sResult;
try {
sResult = await _channel.invokeMethod('getIP', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return sResult;
}
static Future<bool> removeWifiNetwork(String ssid) async {
Map<String, String> htArguments = new Map();
htArguments["ssid"] = ssid;
bool bResult;
try {
bResult = await _channel.invokeMethod('removeWifiNetwork', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return (bResult != null && bResult);
}
static Future<bool> isRegisteredWifiNetwork(String ssid) async {
Map<String, String> htArguments = new Map();
htArguments["ssid"] = ssid;
bool bResult;
try {
bResult = await _channel.invokeMethod('isRegisteredWifiNetwork', htArguments);
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return (bResult != null && bResult);
}
}
class APClient {
String ipAddr;
String hwAddr;
String device;
bool isReachable;
APClient.fromJson(Map<String, dynamic> json)
: ipAddr = json['IPAddr'],
hwAddr = json['HWAddr'],
device = json['Device'],
isReachable = json['isReachable'];
Map<String, dynamic> toJson() => {
'IPAddr': ipAddr,
'HWAddr': hwAddr,
'Device': device,
'isReachable': isReachable,
};
static List<APClient> parse(String psString) {
/// []
List<APClient> htList = new List();
List<dynamic> htMapClients = json.decode(psString);
// List<Map<String, dynamic>> htMapClients = json.decode(psString);
htMapClients.forEach((htMapClient) {
// htMapClients.forEach((Map<String, dynamic> htMapClient) {
htList.add(new APClient.fromJson(htMapClient));
});
return htList;
}
}
class WifiNetwork {
/// NOTE Parsing JSON :
/// http://cogitas.net/parse-json-dart-flutter/
String ssid;
String bssid;
String capabilities;
int frequency;
int level;
int timestamp;
String password;
WifiNetwork.fromJson(Map<String, dynamic> json)
: ssid = json['SSID'],
bssid = json['BSSID'],
capabilities = json['capabilities'],
frequency = json['frequency'],
level = json['level'],
timestamp = json['timestamp'];
Map<String, dynamic> toJson() => {
'SSID': ssid,
'BSSID': bssid,
'capabilities': capabilities,
'frequency': frequency,
'level': level,
'timestamp': timestamp,
};
static List<WifiNetwork> parse(String psString) {
/// [{"SSID":"Florian","BSSID":"30:7e:cb:8c:48:e4","capabilities":"[WPA-PSK-CCMP+TKIP][ESS]","frequency":2462,"level":-64,"timestamp":201307720907},{"SSID":"Pi3-AP","BSSID":"b8:27:eb:b1:fa:e1","capabilities":"[WPA2-PSK-CCMP][ESS]","frequency":2437,"level":-66,"timestamp":201307720892},{"SSID":"AlternaDom-SonOff","BSSID":"b8:27:eb:98:b4:81","capabilities":"[WPA2-PSK-CCMP][ESS]","frequency":2437,"level":-86,"timestamp":201307720897},{"SSID":"SFR_1CF0_2GEXT","BSSID":"9c:3d:cf:58:98:07","capabilities":"[WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]","frequency":2412,"level":-87,"timestamp":201307720887},{"SSID":"Freebox-5CC952","BSSID":"f4:ca:e5:96:71:c4","capabilities":"[WPA-PSK-CCMP][ESS]","frequency":2442,"level":-90,"timestamp":201307720902}]
List<WifiNetwork> htList = new List();
try{
List<dynamic> htMapNetworks = json.decode(psString);
// List<Map<String, dynamic>> htMapNetworks = json.decode(psString);
htMapNetworks.forEach((htMapNetwork) {
// htMapNetworks.forEach((Map<String, dynamic> htMapNetwork) {
htList.add(new WifiNetwork.fromJson(htMapNetwork));
});
} on FormatException catch (e) {
print("FormatException : ${e.toString()}");
print("psString = '$psString'");
}
return htList;
}
}
enum NetworkSecurity {
WPA, WEP, NONE
}