Release 4.0.0-preview1-00285
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothAdapter.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19
20 namespace Tizen.Network.Bluetooth
21 {
22     /// <summary>
23     /// This class is used to control the Bluetooth adapter and get the list of bonded devices.<br/>
24     /// The BluetoothAdapter class is used to discover neighbouring bluetooth devices.
25     /// </summary>
26     /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
27     static public class BluetoothAdapter
28     {
29         /// <summary>
30         /// A property to check whether the Bluetooth is enabled.
31         /// </summary>
32         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
33         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
34         static public bool IsBluetoothEnabled
35         {
36             get
37             {
38                 try
39                 {
40                     return BluetoothAdapterImpl.Instance.IsBluetoothEnabled;
41                 }
42                 catch (TypeInitializationException e)
43                 {
44                     throw e.InnerException;
45                 }
46             }
47         }
48
49         /// <summary>
50         /// The local adapter address.
51         /// </summary>
52         /// <remarks>
53         /// The Bluetooth must be enabled.
54         /// </remarks>
55         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
56         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
57         static public string Address
58         {
59             get
60             {
61                 if (IsBluetoothEnabled)
62                 {
63                     return BluetoothAdapterImpl.Instance.Address;
64                 }
65                 else
66                 {
67                     return null;
68                 }
69             }
70         }
71
72         /// <summary>
73         /// The name of the local adapter.
74         /// </summary>
75         /// <remarks>
76         /// The Bluetooth must be enabled.
77         /// </remarks>
78         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
79         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
80         static public string Name
81         {
82             get
83             {
84                 if (IsBluetoothEnabled)
85                 {
86                     return BluetoothAdapterImpl.Instance.Name;
87                 }
88                 else
89                 {
90                     return null;
91                 }
92             }
93             set
94             {
95                 try
96                 {
97                     BluetoothAdapterImpl.Instance.Name = value;
98                 }
99                 catch (TypeInitializationException e)
100                 {
101                     throw e.InnerException;
102                 }
103             }
104         }
105
106         /// <summary>
107         /// The visibility mode of the Bluetooth adapter.
108         /// </summary>
109         /// <remarks>
110         /// The Bluetooth must be enabled.
111         /// </remarks>
112         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
113         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
114         static public VisibilityMode Visibility
115         {
116             get
117             {
118                 if (IsBluetoothEnabled)
119                 {
120                     return BluetoothAdapterImpl.Instance.Visibility;
121                 }
122                 else
123                 {
124                     return VisibilityMode.NonDiscoverable;
125                 }
126             }
127         }
128
129         /// <summary>
130         /// A property to check whether the device discovery process is in progress.
131         /// </summary>
132         /// <remarks>
133         /// The Bluetooth must be enabled.
134         /// </remarks>
135         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
136         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
137         static public bool IsDiscoveryInProgress
138         {
139             get
140             {
141                 if (IsBluetoothEnabled)
142                 {
143                     return BluetoothAdapterImpl.Instance.IsDiscoveryInProgress;
144                 }
145                 else
146                 {
147                     return false;
148                 }
149             }
150         }
151
152         /// <summary>
153         /// The remaining time, in seconds, until the visibility mode is changed from TimeLimitedDiscoverable to NonDiscoverable.
154         /// </summary>
155         /// <remarks>
156         /// The Bluetooth must be enabled.
157         /// </remarks>
158         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
159         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
160         static public int RemainingTimeAsVisible
161         {
162             get
163             {
164                 if (IsBluetoothEnabled)
165                 {
166                     return BluetoothAdapterImpl.Instance.RemainingTimeAsVisible;
167                 }
168                 else
169                 {
170                     return 0;
171                 }
172             }
173         }
174
175         /// <summary>
176         /// The StateChanged event is raised when the Bluetooth adapter state is changed.
177         /// </summary>
178         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
179         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
180         static public event EventHandler<StateChangedEventArgs> StateChanged
181         {
182             add
183             {
184                 try
185                 {
186                     BluetoothAdapterImpl.Instance.StateChanged += value;
187                 }
188                 catch (TypeInitializationException e)
189                 {
190                     throw e.InnerException;
191                 }
192             }
193             remove
194             {
195                 try
196                 {
197                     BluetoothAdapterImpl.Instance.StateChanged -= value;
198                 }
199                 catch (TypeInitializationException e)
200                 {
201                     throw e.InnerException;
202                 }
203             }
204         }
205
206         /// <summary>
207         /// The NameChanged event is raised when the Bluetooth adapter name is changed.
208         /// </summary>
209         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
210         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
211         static public event EventHandler<NameChangedEventArgs> NameChanged
212         {
213             add
214             {
215                 try
216                 {
217                     BluetoothAdapterImpl.Instance.NameChanged += value;
218                 }
219                 catch (TypeInitializationException e)
220                 {
221                     throw e.InnerException;
222                 }
223             }
224             remove
225             {
226                 try
227                 {
228                     BluetoothAdapterImpl.Instance.NameChanged -= value;
229                 }
230                 catch (TypeInitializationException e)
231                 {
232                     throw e.InnerException;
233                 }
234             }
235         }
236
237         /// <summary>
238         /// The VisibilityModeChanged event is raised when the Bluetooth adapter visibility mode is changed.
239         /// </summary>
240         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
241         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
242         static public event EventHandler<VisibilityModeChangedEventArgs> VisibilityModeChanged
243         {
244             add
245             {
246                 try
247                 {
248                     BluetoothAdapterImpl.Instance.VisibilityModeChanged += value;
249                 }
250                 catch (TypeInitializationException e)
251                 {
252                     throw e.InnerException;
253                 }
254             }
255             remove
256             {
257                 try
258                 {
259                     BluetoothAdapterImpl.Instance.VisibilityModeChanged -= value;
260                 }
261                 catch (TypeInitializationException e)
262                 {
263                     throw e.InnerException;
264                 }
265             }
266         }
267
268         /// <summary>
269         /// The VisibilityDurationChanged event is raised very second until the visibility mode is changed to NonDiscoverable.
270         /// </summary>
271         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
272         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
273         static public event EventHandler<VisibilityDurationChangedEventArgs> VisibilityDurationChanged
274         {
275             add
276             {
277                 try
278                 {
279                     BluetoothAdapterImpl.Instance.VisibilityDurationChanged += value;
280                 }
281                 catch (TypeInitializationException e)
282                 {
283                     throw e.InnerException;
284                 }
285             }
286             remove
287             {
288                 try
289                 {
290                     BluetoothAdapterImpl.Instance.VisibilityDurationChanged -= value;
291                 }
292                 catch (TypeInitializationException e)
293                 {
294                     throw e.InnerException;
295                 }
296             }
297         }
298
299         /// <summary>
300         /// The DiscoveryStateChanged event is raised when the device discovery state is changed.
301         /// </summary>
302         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
303         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
304         static public event EventHandler<DiscoveryStateChangedEventArgs> DiscoveryStateChanged
305         {
306             add
307             {
308                 try
309                 {
310                     BluetoothAdapterImpl.Instance.DiscoveryStateChanged += value;
311                 }
312                 catch (TypeInitializationException e)
313                 {
314                     throw e.InnerException;
315                 }
316             }
317             remove
318             {
319                 try
320                 {
321                     BluetoothAdapterImpl.Instance.DiscoveryStateChanged -= value;
322                 }
323                 catch (TypeInitializationException e)
324                 {
325                     throw e.InnerException;
326                 }
327             }
328         }
329
330         /// <summary>
331         /// This event is called when the LE scan result is obtained.
332         /// </summary>
333         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
334         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
335         static public event EventHandler<AdapterLeScanResultChangedEventArgs> ScanResultChanged
336         {
337             add
338             {
339                 try
340                 {
341                     BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged += value;
342                 }
343                 catch (TypeInitializationException e)
344                 {
345                     throw e.InnerException;
346                 }
347             }
348             remove
349             {
350                 try
351                 {
352                     BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged -= value;
353                 }
354                 catch (TypeInitializationException e)
355                 {
356                     throw e.InnerException;
357                 }
358             }
359         }
360
361         /// <summary>
362         /// Starts the device discovery process.
363         /// </summary>
364         /// <remarks>
365         /// The Bluetooth must be enabled and the device discovery process can be stopped by StopDiscovery().
366         /// If this succeeds, the DiscoveryStateChanged event will be invoked.
367         /// </remarks>
368         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
369         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
370         /// or the start discovery fails.</exception>
371         static public void StartDiscovery()
372         {
373             if (IsBluetoothEnabled)
374             {
375                 BluetoothAdapterImpl.Instance.StartDiscovery();
376             }
377         }
378
379         /// <summary>
380         /// Stops the device discovery process.
381         /// </summary>
382         /// <remarks>
383         /// The device discovery process must be in progress with StartDiscovery().
384         /// If this succeeds, the DiscoveryStateChanged event will be invoked.
385         /// </remarks>
386         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
387         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled or 
388         /// the discovery process is not is progress. </exception>
389         static public void StopDiscovery()
390         {
391             if (IsDiscoveryInProgress)
392             {
393                 BluetoothAdapterImpl.Instance.StopDiscovery();
394             }
395         }
396
397         /// <summary>
398         /// Retrieves the device information of all bonded devices.
399         /// </summary>
400         /// <remarks>
401         /// The Bluetooth must be enabled.
402         /// </remarks>
403         /// <returns> The list of the bonded BluetoothDeviceInfo objects.</returns>
404         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
405         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
406         /// or reading the Bonded devices list is failed.</exception>
407         static public IEnumerable<BluetoothDevice> GetBondedDevices()
408         {
409             if (IsBluetoothEnabled)
410             {
411                 return BluetoothAdapterImpl.Instance.GetBondedDevices();
412             }
413             else
414             {
415                 return null;
416             }
417         }
418
419         /// <summary>
420         /// Gets the device information of a bonded device.
421         /// </summary>
422         /// <remarks>
423         /// The Bluetooth must be enabled.
424         /// </remarks>
425         /// <returns> Information of the bonded BluetoothDeviceInfo object.</returns>
426         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
427         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
428         /// or reading the bonded device information fails.</exception>
429         static public BluetoothDevice GetBondedDevice(string address)
430         {
431             if (IsBluetoothEnabled)
432             {
433                 return BluetoothAdapterImpl.Instance.GetBondedDevice(address);
434             }
435             else
436             {
437                 return null;
438             }
439         }
440
441         /// <summary>
442         /// Checks whether the UUID of service is used or not.
443         /// </summary>
444         /// <returns><c>true</c> if the specified serviceUuid is used, otherwise <c>false</c>.</returns>
445         /// <param name="serviceUuid">The UUID of Service.</param>
446         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
447         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
448         static public bool IsServiceUsed(string serviceUuid)
449         {
450             try
451             {
452                 return BluetoothAdapterImpl.Instance.IsServiceUsed(serviceUuid);
453             }
454             catch (TypeInitializationException e)
455             {
456                 throw e.InnerException;
457             }
458         }
459
460         /// <summary>
461         /// Gets the hash and the randomizer value of the local OOB data object.
462         /// </summary>
463         /// <remarks>
464         /// The Bluetooth must be enabled.
465         /// </remarks>
466         /// <returns>The BluetoothOobData object.</returns>
467         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
468         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
469         /// or the read OObData procedure is failed.</exception>
470         static public BluetoothOobData GetLocalOobData()
471         {
472             if (IsBluetoothEnabled && Globals.IsInitialize)
473             {
474                 return BluetoothAdapterImpl.Instance.GetLocalOobData();
475             }
476             else
477             {
478                 return null;
479             }
480         }
481
482         /// <summary>
483         /// Sets the hash and the randmoizer value of the OOB data into the remote device.
484         /// </summary>
485         /// <remarks>
486         /// The Bluetooth must be enabled.
487         /// </remarks>
488         /// <param name="address">The remote device address.</param>
489         /// <param name="oobData">The BluetoothOobData object.</param>
490         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
491         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
492         /// or the set OobData procedure is failed.</exception>
493         static public void SetRemoteOobData(string address, BluetoothOobData oobData)
494         {
495             if (IsBluetoothEnabled && Globals.IsInitialize)
496             {
497                 BluetoothAdapterImpl.Instance.SetRemoteOobData(address, oobData);
498             }
499         }
500
501         /// <summary>
502         /// Removes the hash and the randomizer value of the OOB data from the remote device.
503         /// </summary>
504         /// <remarks>
505         /// The Bluetooth must be enabled.
506         /// </remarks>
507         /// <param name="address">The remote device address.</param>
508         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
509         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.
510         /// or if the Remove Oobdata procedure is failed.</exception>
511         static public void RemoveRemoteOobData(string address)
512         {
513             if (IsBluetoothEnabled && Globals.IsInitialize)
514             {
515                 BluetoothAdapterImpl.Instance.RemoveRemoteOobData(address);
516             }
517         }
518
519         /// <summary>
520         /// Starts the Bluetooth LE scan operation to discover BLE devices
521         /// </summary>
522         /// <remarks>
523         /// The Bluetooth must be enabled.
524         /// </remarks>The result of the operation StartLeScan.
525         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
526         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
527         /// or the Start LE scan is failed.</exception>
528         static public void StartLeScan()
529         {
530             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
531             {
532                 int ret = BluetoothLeImplAdapter.Instance.StartScan ();
533                 if (ret != (int)BluetoothError.None)
534                 {
535                     Log.Error(Globals.LogTag, "Failed to in start the le scan operation, Error - " + (BluetoothError)ret);
536                     BluetoothErrorFactory.ThrowBluetoothException(ret);
537                 }
538             }
539             else
540             {
541                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
542             }
543         }
544
545         /// <summary>
546         /// Stops the Bluetooth LE scan operation.
547         /// </summary>
548         /// <remarks>
549         /// The Bluetooth must be enabled.
550         /// </remarks>The result of the operation stopLescan.
551         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
552         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
553         /// or the Stop LE scan is failed.</exception>
554         static public void StopLeScan()
555         {
556             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
557             {
558                 int ret = BluetoothLeImplAdapter.Instance.StopScan ();
559                 if (ret != (int)BluetoothError.None)
560                 {
561                     Log.Error(Globals.LogTag, "Failed to stop the le scan operation, Error - " + (BluetoothError)ret);
562                     BluetoothErrorFactory.ThrowBluetoothException(ret);
563                 }
564             }
565             else
566             {
567                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
568             }
569         }
570
571         /// <summary>
572         /// Returns the BluetoothLeAdvertiser instance.
573         /// </summary>
574         /// <remarks>
575         /// The Bluetooth must be enabled before calling this API.
576         /// </remarks>
577         /// <returns>The BluetoothLeAdvertiser instance.</returns>
578         static public BluetoothLeAdvertiser GetBluetoothLeAdvertiser()
579         {
580             return BluetoothLeAdvertiser.Instance;
581         }
582
583         /// <summary>
584         /// Registers a rfcomm server socket with a specific UUID.
585         /// </summary>
586         /// <remarks>
587         /// The Bluetooth must be enabled before calling this API.
588         /// </remarks>
589         /// <returns>The BluetoothServerSocket instance.</returns>
590         /// <param name="serviceUuid">The UUID of service to provide.</param>
591         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
592         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
593         /// or the socket create error occurs.</exception>
594         static public BluetoothServerSocket CreateServerSocket(string serviceUuid)
595         {
596             if (IsBluetoothEnabled && Globals.IsInitialize)
597             {
598                 return BluetoothAdapterImpl.Instance.CreateServerSocket (serviceUuid);
599             }
600             else
601             {
602                 return null;
603             }
604         }
605
606         /// <summary>
607         /// Removes the rfcomm server socket which was created using CreateServerSocket().
608         /// </summary>
609         /// <remarks>
610         /// The socket must be created with CreateServerSocket(). The ConnectionStateChanged event is raised after this API is called.
611         /// </remarks>
612         /// <param name="socket">The server socket instance is created using CreateServerSocket().</param>
613         /// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
614         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled
615         /// or the socket destroy error occurs.</exception>
616         static public void DestroyServerSocket(BluetoothServerSocket socket)
617         {
618             if (IsBluetoothEnabled && Globals.IsInitialize)
619             {
620                 BluetoothAdapterImpl.Instance.DestroyServerSocket(socket);
621             }
622         }
623     }
624 }