/
WiFiManager.cs
executable file
·504 lines (472 loc) · 23.2 KB
/
WiFiManager.cs
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Tizen.Network.WiFi
{
/// <summary>
/// A class for managing the WiFiManager handle.
/// </summary>
/// <since_tizen> 3 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SafeWiFiManagerHandle : SafeHandle
{
private int _tid;
internal SafeWiFiManagerHandle() : base(IntPtr.Zero, true)
{
}
/// <summary>
/// Checks the validity of the handle.
/// </summary>
/// <value>Represents the validity of the handle.</value>
/// <since_tizen> 3 </since_tizen>
public override bool IsInvalid
{
get
{
return this.handle == IntPtr.Zero;
}
}
/// <summary>
/// Release the handle
/// </summary>
protected override bool ReleaseHandle()
{
Interop.WiFi.Deinitialize(_tid, this.handle);
this.SetHandle(IntPtr.Zero);
return true;
}
internal void SetTID(int id)
{
_tid = id;
Log.Info(Globals.LogTag, "New Handle for Thread " + _tid);
}
}
/// <summary>
/// A manager class which allows applications to connect to a Wireless Local Area Network (WLAN) and transfer data over the network.
/// The Wi-Fi Manager enables your application to activate and deactivate a local Wi-Fi device, and to connect to a WLAN network in the infrastructure mode.
/// </summary>
/// <since_tizen> 3 </since_tizen>
static public class WiFiManager
{
/// <summary>
/// The local MAC address.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>Represents the MAC address of the Wi-Fi.</value>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
static public string MacAddress
{
get
{
return WiFiManagerImpl.Instance.MacAddress;
}
}
/// <summary>
/// The name of the network interface.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>Interface name of the Wi-Fi.</value>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
static public string InterfaceName
{
get
{
return WiFiManagerImpl.Instance.InterfaceName;
}
}
/// <summary>
/// The network connection state.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>Represents the connection state of the Wi-Fi.</value>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
static public WiFiConnectionState ConnectionState
{
get
{
return WiFiManagerImpl.Instance.ConnectionState;
}
}
/// <summary>
/// A property to check whether Wi-Fi is activated.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>Boolean value to check whether Wi-Fi is activated or not.</value>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
static public bool IsActive
{
get
{
return WiFiManagerImpl.Instance.IsActive;
}
}
/// <summary>
/// The Wi-Fi scan state.
/// </summary>
/// <since_tizen> 6 </since_tizen>
/// <value>Represents the scan state of the Wi-Fi.</value>
static public WiFiScanState ScanState
{
get
{
return WiFiManagerImpl.Instance.ScanState;
}
}
/// <summary>
/// DeviceStateChanged is raised when the device state is changed.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <feature>http://tizen.org/feature/network.wifi</feature>
static public event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged
{
add
{
WiFiManagerImpl.Instance.DeviceStateChanged += value;
}
remove
{
WiFiManagerImpl.Instance.DeviceStateChanged -= value;
}
}
/// <summary>
/// ConnectionStateChanged is raised when the connection state is changed.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <feature>http://tizen.org/feature/network.wifi</feature>
static public event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
{
add
{
WiFiManagerImpl.Instance.ConnectionStateChanged += value;
}
remove
{
WiFiManagerImpl.Instance.ConnectionStateChanged -= value;
}
}
/// <summary>
/// RssiLevelChanged is raised when the RSSI of the connected Wi-Fi is changed.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <feature>http://tizen.org/feature/network.wifi</feature>
static public event EventHandler<RssiLevelChangedEventArgs> RssiLevelChanged
{
add
{
WiFiManagerImpl.Instance.RssiLevelChanged += value;
}
remove
{
WiFiManagerImpl.Instance.RssiLevelChanged -= value;
}
}
/// <summary>
/// BackgroundScanFinished is raised when the background scan is finished.
/// The background scan starts automatically when Wi-Fi is activated. The callback will be invoked periodically.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <feature>http://tizen.org/feature/network.wifi</feature>
static public event EventHandler BackgroundScanFinished
{
add
{
WiFiManagerImpl.Instance.BackgroundScanFinished += value;
}
remove
{
WiFiManagerImpl.Instance.BackgroundScanFinished -= value;
}
}
/// <summary>
/// ScanStateChanged is raised when the scan state is changed.
/// </summary>
/// <since_tizen> 6 </since_tizen>
/// <feature>http://tizen.org/feature/network.wifi</feature>
static public event EventHandler<ScanStateChangedEventArgs> ScanStateChanged
{
add
{
WiFiManagerImpl.Instance.ScanStateChanged += value;
}
remove
{
WiFiManagerImpl.Instance.ScanStateChanged -= value;
}
}
/// <summary>
/// Gets the Wi-Fi safe handle.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>The instance of the SafeWiFiManagerHandle.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public static SafeWiFiManagerHandle GetWiFiHandle()
{
return WiFiManagerImpl.Instance.GetSafeHandle();
}
/// <summary>
/// Gets the result of the scan.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>A list of the WiFiAP objects.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public IEnumerable<WiFiAP> GetFoundAPs()
{
return WiFiManagerImpl.Instance.GetFoundAPs();
}
/// <summary>
/// Gets the result of ScanSpecificAPAsync(string essid) API.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>A list containing the WiFiAP objects.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public IEnumerable<WiFiAP> GetFoundSpecificAPs()
{
return WiFiManagerImpl.Instance.GetFoundSpecificAPs();
}
/// <summary>
/// Gets the result of the BssidScanAsync() API.
/// </summary>
/// <since_tizen> 5 </since_tizen>
/// <returns>A list of the WiFiAP objects.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public IEnumerable<WiFiAP> GetFoundBssids()
{
return WiFiManagerImpl.Instance.GetFoundBssids();
}
/// <summary>
/// Gets the list of Wi-Fi configurations.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>A list containing the WiFiConfiguration objects.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
{
return WiFiManagerImpl.Instance.GetWiFiConfigurations();
}
/// <summary>
/// Saves the Wi-Fi configuration of the access point.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="configuration">The configuration to be stored.</param>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="ArgumentNullException">Thrown when WiFiConfiguration is passed as null.</exception>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public void SaveWiFiConfiguration(WiFiConfiguration configuration)
{
WiFiManagerImpl.Instance.SaveWiFiNetworkConfiguration(configuration);
}
/// <summary>
/// Gets the object of the connected WiFiAP.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>The connected Wi-Fi access point (AP) information.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public WiFiAP GetConnectedAP()
{
return WiFiManagerImpl.Instance.GetConnectedAP();
}
/// <summary>
/// Activates the Wi-Fi asynchronously.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns> A task indicating whether the activate method is done or not.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="NowInProgressException">Thrown when the Wi-Fi activation is now in progress.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public Task ActivateAsync()
{
return WiFiManagerImpl.Instance.ActivateAsync();
}
/// <summary>
/// Activates the Wi-Fi asynchronously and displays the Wi-Fi picker (popup) when the Wi-Fi is not automatically connected.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>A task indicating whether the ActivateWithPicker method is done or not.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="NowInProgressException">Thrown when the Wi-Fi activation is now in progress.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public Task ActivateWithPickerAsync()
{
return WiFiManagerImpl.Instance.ActivateWithWiFiPickerTestedAsync();
}
/// <summary>
/// Deactivates the Wi-Fi asynchronously.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>A task indicating whether the deactivate method is done or not.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="NowInProgressException">Thrown when the Wi-Fi deactivation is now in progress.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public Task DeactivateAsync()
{
return WiFiManagerImpl.Instance.DeactivateAsync();
}
/// <summary>
/// Starts the scan asynchronously.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>A task indicating whether the scan method is done or not.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public Task ScanAsync()
{
return WiFiManagerImpl.Instance.ScanAsync();
}
/// <summary>
/// Starts a specific access point scan asynchronously.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns>A task indicating whether the ScanSpecificAP method is done or not.</returns>
/// <param name="essid">The ESSID of the hidden AP.</param>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public Task ScanSpecificAPAsync(string essid)
{
return WiFiManagerImpl.Instance.ScanSpecificAPAsync(essid);
}
/// <summary>
/// Starts BSSID scan asynchronously.
/// </summary>
/// <remarks>
/// This method must be called from MainThread.
/// </remarks>
/// <since_tizen> 5 </since_tizen>
/// <returns>A task indicating whether the BssidScanAsync method is done or not.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
static public Task BssidScanAsync()
{
return WiFiManagerImpl.Instance.BssidScanAsync();
}
/// <summary>
/// Create Specific scan handle.
/// </summary>
/// <since_tizen> 9 </since_tizen>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
static public void CreateSpecificHandle()
{
WiFiManagerImpl.Instance.CreateSpecificScanHandle();
}
/// <summary>
/// Destroys Specific scan handle.
/// </summary>
/// <since_tizen> 9 </since_tizen>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
static public void DestroySpecificHandle()
{
WiFiManagerImpl.Instance.DestroySpecificScanHandle();
}
/// <summary>
/// Starts Multi Scan.
/// </summary>
/// <remarks>
/// This method must be called from MainThread.
/// </remarks>
/// <since_tizen> 9 </since_tizen>
/// <param name="frequency">Frequency for which MultiScan is to be run.</param>
/// <returns>A task indicating whether the StartMultiScan method is done or not.</returns>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
/// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
static public Task StartMultiScan(int frequency)
{
WiFiManagerImpl.Instance.SetSpecificScanFreq(frequency);
return WiFiManagerImpl.Instance.StartMultiScan();
}
}
}