Release 4.0.0-preview1-00051
[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="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
33         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
34         static public bool IsBluetoothEnabled
35         {
36             get
37             {
38                 return BluetoothAdapterImpl.Instance.IsBluetoothEnabled;
39             }
40         }
41
42         /// <summary>
43         /// The local adapter address.
44         /// </summary>
45         /// <remarks>
46         /// The Bluetooth must be enabled.
47         /// </remarks>
48         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
49         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
50         static public string Address
51         {
52             get
53             {
54                 if (IsBluetoothEnabled)
55                 {
56                     return BluetoothAdapterImpl.Instance.Address;
57                 }
58                 else
59                 {
60                     return null;
61                 }
62             }
63         }
64
65         /// <summary>
66         /// The name of the local adapter.
67         /// </summary>
68         /// <remarks>
69         /// The Bluetooth must be enabled.
70         /// </remarks>
71         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
72         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
73         static public string Name
74         {
75             get
76             {
77                 if (IsBluetoothEnabled)
78                 {
79                     return BluetoothAdapterImpl.Instance.Name;
80                 }
81                 else
82                 {
83                     return null;
84                 }
85             }
86             set
87             {
88                 BluetoothAdapterImpl.Instance.Name = value;
89             }
90         }
91
92         /// <summary>
93         /// The visibility mode of the Bluetooth adapter.
94         /// </summary>
95         /// <remarks>
96         /// The Bluetooth must be enabled.
97         /// </remarks>
98         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
99         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
100         static public VisibilityMode Visibility
101         {
102             get
103             {
104                 if (IsBluetoothEnabled)
105                 {
106                     return BluetoothAdapterImpl.Instance.Visibility;
107                 }
108                 else
109                 {
110                     return VisibilityMode.NonDiscoverable;
111                 }
112             }
113         }
114
115         /// <summary>
116         /// A property to check whether the device discovery process is in progress.
117         /// </summary>
118         /// <remarks>
119         /// The Bluetooth must be enabled.
120         /// </remarks>
121         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
122         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
123         static public bool IsDiscoveryInProgress
124         {
125             get
126             {
127                 if (IsBluetoothEnabled)
128                 {
129                     return BluetoothAdapterImpl.Instance.IsDiscoveryInProgress;
130                 }
131                 else
132                 {
133                     return false;
134                 }
135             }
136         }
137
138         /// <summary>
139         /// The remaining time, in seconds, until the visibility mode is changed from TimeLimitedDiscoverable to NonDiscoverable.
140         /// </summary>
141         /// <remarks>
142         /// The Bluetooth must be enabled.
143         /// </remarks>
144         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
145         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
146         static public int RemainingTimeAsVisible
147         {
148             get
149             {
150                 if (IsBluetoothEnabled)
151                 {
152                     return BluetoothAdapterImpl.Instance.RemainingTimeAsVisible;
153                 }
154                 else
155                 {
156                     return 0;
157                 }
158             }
159         }
160
161         /// <summary>
162         /// The StateChanged event is raised when the Bluetooth adapter state is changed.
163         /// </summary>
164         static public event EventHandler<StateChangedEventArgs> StateChanged
165         {
166             add
167             {
168                 BluetoothAdapterImpl.Instance.StateChanged += value;
169             }
170             remove
171             {
172                 BluetoothAdapterImpl.Instance.StateChanged -= value;
173             }
174         }
175
176         /// <summary>
177         /// The NameChanged event is raised when the Bluetooth adapter name is changed.
178         /// </summary>
179         static public event EventHandler<NameChangedEventArgs> NameChanged
180         {
181             add
182             {
183                 BluetoothAdapterImpl.Instance.NameChanged += value;
184             }
185             remove
186             {
187                 BluetoothAdapterImpl.Instance.NameChanged -= value;
188             }
189         }
190
191         /// <summary>
192         /// The VisibilityModeChanged event is raised when the Bluetooth adapter visibility mode is changed.
193         /// </summary>
194         static public event EventHandler<VisibilityModeChangedEventArgs> VisibilityModeChanged
195         {
196             add
197             {
198                 BluetoothAdapterImpl.Instance.VisibilityModeChanged += value;
199             }
200             remove
201             {
202                 BluetoothAdapterImpl.Instance.VisibilityModeChanged -= value;
203             }
204         }
205
206         /// <summary>
207         /// The VisibilityDurationChanged event is raised very second until the visibility mode is changed to NonDiscoverable.
208         /// </summary>
209         static public event EventHandler<VisibilityDurationChangedEventArgs> VisibilityDurationChanged
210         {
211             add
212             {
213                 BluetoothAdapterImpl.Instance.VisibilityDurationChanged += value;
214             }
215             remove
216             {
217                 BluetoothAdapterImpl.Instance.VisibilityDurationChanged -= value;
218             }
219         }
220
221         /// <summary>
222         /// The DiscoveryStateChanged event is raised when the device discovery state is changed.
223         /// </summary>
224         static public event EventHandler<DiscoveryStateChangedEventArgs> DiscoveryStateChanged
225         {
226             add
227             {
228                 BluetoothAdapterImpl.Instance.DiscoveryStateChanged += value;
229             }
230             remove
231             {
232                 BluetoothAdapterImpl.Instance.DiscoveryStateChanged -= value;
233             }
234         }
235
236         /// <summary>
237         /// This event is called when the LE scan result is obtained.
238         /// </summary>
239         static public event EventHandler<AdapterLeScanResultChangedEventArgs> ScanResultChanged
240         {
241             add
242             {
243                 BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged += value;
244             }
245             remove {
246                 BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged -= value;
247             }
248         }
249
250         /// <summary>
251         /// Starts the device discovery process.
252         /// </summary>
253         /// <remarks>
254         /// The Bluetooth must be enabled and the device discovery process can be stopped by StopDiscovery().
255         /// If this succeeds, the DiscoveryStateChanged event will be invoked.
256         /// </remarks>
257         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
258         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
259         /// or the start discovery fails.</exception>
260         static public void StartDiscovery()
261         {
262             if (IsBluetoothEnabled)
263             {
264                 BluetoothAdapterImpl.Instance.StartDiscovery();
265             }
266         }
267
268         /// <summary>
269         /// Stops the device discovery process.
270         /// </summary>
271         /// <remarks>
272         /// The device discovery process must be in progress with StartDiscovery().
273         /// If this succeeds, the DiscoveryStateChanged event will be invoked.
274         /// </remarks>
275         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
276         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled or 
277         /// the discovery process is not is progress. </exception>
278         static public void StopDiscovery()
279         {
280             if (IsDiscoveryInProgress)
281             {
282                 BluetoothAdapterImpl.Instance.StopDiscovery();
283             }
284         }
285
286         /// <summary>
287         /// Retrieves the device information of all bonded devices.
288         /// </summary>
289         /// <remarks>
290         /// The Bluetooth must be enabled.
291         /// </remarks>
292         /// <returns> The list of the bonded BluetoothDeviceInfo objects.</returns>
293         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
294         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
295         /// or reading the Bonded devices list is failed.</exception>
296         static public IEnumerable<BluetoothDevice> GetBondedDevices()
297         {
298             if (IsBluetoothEnabled)
299             {
300                 return BluetoothAdapterImpl.Instance.GetBondedDevices();
301             }
302             else
303             {
304                 return null;
305             }
306         }
307
308         /// <summary>
309         /// Gets the device information of a bonded device.
310         /// </summary>
311         /// <remarks>
312         /// The Bluetooth must be enabled.
313         /// </remarks>
314         /// <returns> Information of the bonded BluetoothDeviceInfo object.</returns>
315         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
316         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
317         /// or reading the bonded device information fails.</exception>
318         static public BluetoothDevice GetBondedDevice(string address)
319         {
320             if (IsBluetoothEnabled)
321             {
322                 return BluetoothAdapterImpl.Instance.GetBondedDevice(address);
323             }
324             else
325             {
326                 return null;
327             }
328         }
329
330         /// <summary>
331         /// Checks whether the UUID of service is used or not.
332         /// </summary>
333         /// <returns><c>true</c> if the specified serviceUuid is used, otherwise <c>false</c>.</returns>
334         /// <param name="serviceUuid">The UUID of Service.</param>
335         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
336         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
337         static public bool IsServiceUsed(string serviceUuid)
338         {
339             return BluetoothAdapterImpl.Instance.IsServiceUsed(serviceUuid);
340         }
341
342         /// <summary>
343         /// Gets the hash and the randomizer value of the local OOB data object.
344         /// </summary>
345         /// <remarks>
346         /// The Bluetooth must be enabled.
347         /// </remarks>
348         /// <returns>The BluetoothOobData object.</returns>
349         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
350         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
351         /// or the read OObData procedure is failed.</exception>
352         static public BluetoothOobData GetLocalOobData()
353         {
354             if (IsBluetoothEnabled && Globals.IsInitialize)
355             {
356                 return BluetoothAdapterImpl.Instance.GetLocalOobData();
357             }
358             else
359             {
360                 return null;
361             }
362         }
363
364         /// <summary>
365         /// Sets the hash and the randmoizer value of the OOB data into the remote device.
366         /// </summary>
367         /// <remarks>
368         /// The Bluetooth must be enabled.
369         /// </remarks>
370         /// <param name="address">The remote device address.</param>
371         /// <param name="oobData">The BluetoothOobData object.</param>
372         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
373         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
374         /// or the set OobData procedure is failed.</exception>
375         static public void SetRemoteOobData(string address, BluetoothOobData oobData)
376         {
377             if (IsBluetoothEnabled && Globals.IsInitialize)
378             {
379                 BluetoothAdapterImpl.Instance.SetRemoteOobData(address, oobData);
380             }
381         }
382
383         /// <summary>
384         /// Removes the hash and the randomizer value of the OOB data from the remote device.
385         /// </summary>
386         /// <remarks>
387         /// The Bluetooth must be enabled.
388         /// </remarks>
389         /// <param name="address">The remote device address.</param>
390         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
391         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled.
392         /// or if the Remove Oobdata procedure is failed.</exception>
393         static public void RemoveRemoteOobData(string address)
394         {
395             if (IsBluetoothEnabled && Globals.IsInitialize)
396             {
397                 BluetoothAdapterImpl.Instance.RemoveRemoteOobData(address);
398             }
399         }
400
401         /// <summary>
402         /// Starts the Bluetooth LE scan operation to discover BLE devices
403         /// </summary>
404         /// <remarks>
405         /// The Bluetooth must be enabled.
406         /// </remarks>The result of the operation StartLeScan.
407         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
408         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth LE is not enabled
409         /// or the Start LE scan is failed.</exception>
410         static public void StartLeScan()
411         {
412             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
413             {
414                 int ret = BluetoothLeImplAdapter.Instance.StartScan ();
415                 if (ret != (int)BluetoothError.None)
416                 {
417                     Log.Error(Globals.LogTag, "Failed to in start the le scan operation, Error - " + (BluetoothError)ret);
418                     BluetoothErrorFactory.ThrowBluetoothException(ret);
419                 }
420             }
421             else
422             {
423                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
424             }
425         }
426
427         /// <summary>
428         /// Stops the Bluetooth LE scan operation.
429         /// </summary>
430         /// <remarks>
431         /// The Bluetooth must be enabled.
432         /// </remarks>The result of the operation stopLescan.
433         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
434         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth LE is not enabled
435         /// or the Stop LE scan is failed.</exception>
436         static public void StopLeScan()
437         {
438             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
439             {
440                 int ret = BluetoothLeImplAdapter.Instance.StopScan ();
441                 if (ret != (int)BluetoothError.None)
442                 {
443                     Log.Error(Globals.LogTag, "Failed to stop the le scan operation, Error - " + (BluetoothError)ret);
444                     BluetoothErrorFactory.ThrowBluetoothException(ret);
445                 }
446             }
447             else
448             {
449                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
450             }
451         }
452
453         /// <summary>
454         /// Returns the BluetoothLeAdvertiser instance.
455         /// </summary>
456         /// <remarks>
457         /// The Bluetooth must be enabled before calling this API.
458         /// </remarks>
459         /// <returns>The BluetoothLeAdvertiser instance.</returns>
460         static public BluetoothLeAdvertiser GetBluetoothLeAdvertiser()
461         {
462             return BluetoothLeAdvertiser.Instance;
463         }
464
465         /// <summary>
466         /// Registers a rfcomm server socket with a specific UUID.
467         /// </summary>
468         /// <remarks>
469         /// The Bluetooth must be enabled before calling this API.
470         /// </remarks>
471         /// <returns>The BluetoothServerSocket instance.</returns>
472         /// <param name="serviceUuid">The UUID of service to provide.</param>
473         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
474         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
475         /// or the socket create error occurs.</exception>
476         static public BluetoothServerSocket CreateServerSocket(string serviceUuid)
477         {
478             if (IsBluetoothEnabled && Globals.IsInitialize)
479             {
480                 return BluetoothAdapterImpl.Instance.CreateServerSocket (serviceUuid);
481             }
482             else
483             {
484                 return null;
485             }
486         }
487
488         /// <summary>
489         /// Removes the rfcomm server socket which was created using CreateServerSocket().
490         /// </summary>
491         /// <remarks>
492         /// The socket must be created with CreateServerSocket(). The ConnectionStateChanged event is raised after this API is called.
493         /// </remarks>
494         /// <param name="socket">The server socket instance is created using CreateServerSocket().</param>
495         /// <exception cref="System.NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
496         /// <exception cref="System.InvalidOperationException">Thrown when the Bluetooth is not enabled
497         /// or the socket destroy error occurs.</exception>
498         static public void DestroyServerSocket(BluetoothServerSocket socket)
499         {
500             if (IsBluetoothEnabled && Globals.IsInitialize)
501             {
502                 BluetoothAdapterImpl.Instance.DestroyServerSocket(socket);
503             }
504         }
505     }
506 }