2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
20 namespace Tizen.Network.Bluetooth
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.
26 /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
27 static public class BluetoothAdapter
30 /// A property to check whether the Bluetooth is enabled.
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
40 return BluetoothAdapterImpl.Instance.IsBluetoothEnabled;
42 catch (TypeInitializationException e)
44 throw e.InnerException;
50 /// The local adapter address.
53 /// The Bluetooth must be enabled.
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
61 if (IsBluetoothEnabled)
63 return BluetoothAdapterImpl.Instance.Address;
73 /// The name of the local adapter.
76 /// The Bluetooth must be enabled.
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
84 if (IsBluetoothEnabled)
86 return BluetoothAdapterImpl.Instance.Name;
97 BluetoothAdapterImpl.Instance.Name = value;
99 catch (TypeInitializationException e)
101 throw e.InnerException;
107 /// The visibility mode of the Bluetooth adapter.
110 /// The Bluetooth must be enabled.
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
118 if (IsBluetoothEnabled)
120 return BluetoothAdapterImpl.Instance.Visibility;
124 return VisibilityMode.NonDiscoverable;
130 /// A property to check whether the device discovery process is in progress.
133 /// The Bluetooth must be enabled.
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
141 if (IsBluetoothEnabled)
143 return BluetoothAdapterImpl.Instance.IsDiscoveryInProgress;
153 /// The remaining time, in seconds, until the visibility mode is changed from TimeLimitedDiscoverable to NonDiscoverable.
156 /// The Bluetooth must be enabled.
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
164 if (IsBluetoothEnabled)
166 return BluetoothAdapterImpl.Instance.RemainingTimeAsVisible;
176 /// The StateChanged event is raised when the Bluetooth adapter state is changed.
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
186 BluetoothAdapterImpl.Instance.StateChanged += value;
188 catch (TypeInitializationException e)
190 throw e.InnerException;
197 BluetoothAdapterImpl.Instance.StateChanged -= value;
199 catch (TypeInitializationException e)
201 throw e.InnerException;
207 /// The NameChanged event is raised when the Bluetooth adapter name is changed.
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
217 BluetoothAdapterImpl.Instance.NameChanged += value;
219 catch (TypeInitializationException e)
221 throw e.InnerException;
228 BluetoothAdapterImpl.Instance.NameChanged -= value;
230 catch (TypeInitializationException e)
232 throw e.InnerException;
238 /// The VisibilityModeChanged event is raised when the Bluetooth adapter visibility mode is changed.
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
248 BluetoothAdapterImpl.Instance.VisibilityModeChanged += value;
250 catch (TypeInitializationException e)
252 throw e.InnerException;
259 BluetoothAdapterImpl.Instance.VisibilityModeChanged -= value;
261 catch (TypeInitializationException e)
263 throw e.InnerException;
269 /// The VisibilityDurationChanged event is raised very second until the visibility mode is changed to NonDiscoverable.
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
279 BluetoothAdapterImpl.Instance.VisibilityDurationChanged += value;
281 catch (TypeInitializationException e)
283 throw e.InnerException;
290 BluetoothAdapterImpl.Instance.VisibilityDurationChanged -= value;
292 catch (TypeInitializationException e)
294 throw e.InnerException;
300 /// The DiscoveryStateChanged event is raised when the device discovery state is changed.
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
310 BluetoothAdapterImpl.Instance.DiscoveryStateChanged += value;
312 catch (TypeInitializationException e)
314 throw e.InnerException;
321 BluetoothAdapterImpl.Instance.DiscoveryStateChanged -= value;
323 catch (TypeInitializationException e)
325 throw e.InnerException;
331 /// This event is called when the LE scan result is obtained.
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
341 BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged += value;
343 catch (TypeInitializationException e)
345 throw e.InnerException;
352 BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged -= value;
354 catch (TypeInitializationException e)
356 throw e.InnerException;
362 /// Starts the device discovery process.
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.
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()
373 if (IsBluetoothEnabled)
375 BluetoothAdapterImpl.Instance.StartDiscovery();
380 /// Stops the device discovery process.
383 /// The device discovery process must be in progress with StartDiscovery().
384 /// If this succeeds, the DiscoveryStateChanged event will be invoked.
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()
391 if (IsDiscoveryInProgress)
393 BluetoothAdapterImpl.Instance.StopDiscovery();
398 /// Retrieves the device information of all bonded devices.
401 /// The Bluetooth must be enabled.
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()
409 if (IsBluetoothEnabled)
411 return BluetoothAdapterImpl.Instance.GetBondedDevices();
420 /// Gets the device information of a bonded device.
423 /// The Bluetooth must be enabled.
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)
431 if (IsBluetoothEnabled)
433 return BluetoothAdapterImpl.Instance.GetBondedDevice(address);
442 /// Checks whether the UUID of service is used or not.
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)
452 return BluetoothAdapterImpl.Instance.IsServiceUsed(serviceUuid);
454 catch (TypeInitializationException e)
456 throw e.InnerException;
461 /// Gets the hash and the randomizer value of the local OOB data object.
464 /// The Bluetooth must be enabled.
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()
472 if (IsBluetoothEnabled && Globals.IsInitialize)
474 return BluetoothAdapterImpl.Instance.GetLocalOobData();
483 /// Sets the hash and the randmoizer value of the OOB data into the remote device.
486 /// The Bluetooth must be enabled.
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)
495 if (IsBluetoothEnabled && Globals.IsInitialize)
497 BluetoothAdapterImpl.Instance.SetRemoteOobData(address, oobData);
502 /// Removes the hash and the randomizer value of the OOB data from the remote device.
505 /// The Bluetooth must be enabled.
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)
513 if (IsBluetoothEnabled && Globals.IsInitialize)
515 BluetoothAdapterImpl.Instance.RemoveRemoteOobData(address);
520 /// Starts the Bluetooth LE scan operation to discover BLE devices
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()
530 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
532 int ret = BluetoothLeImplAdapter.Instance.StartScan ();
533 if (ret != (int)BluetoothError.None)
535 Log.Error(Globals.LogTag, "Failed to in start the le scan operation, Error - " + (BluetoothError)ret);
536 BluetoothErrorFactory.ThrowBluetoothException(ret);
541 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
546 /// Stops the Bluetooth LE scan operation.
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()
556 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
558 int ret = BluetoothLeImplAdapter.Instance.StopScan ();
559 if (ret != (int)BluetoothError.None)
561 Log.Error(Globals.LogTag, "Failed to stop the le scan operation, Error - " + (BluetoothError)ret);
562 BluetoothErrorFactory.ThrowBluetoothException(ret);
567 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
572 /// Returns the BluetoothLeAdvertiser instance.
575 /// The Bluetooth must be enabled before calling this API.
577 /// <returns>The BluetoothLeAdvertiser instance.</returns>
578 static public BluetoothLeAdvertiser GetBluetoothLeAdvertiser()
580 return BluetoothLeAdvertiser.Instance;
584 /// Registers a rfcomm server socket with a specific UUID.
587 /// The Bluetooth must be enabled before calling this API.
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)
596 if (IsBluetoothEnabled && Globals.IsInitialize)
598 return BluetoothAdapterImpl.Instance.CreateServerSocket (serviceUuid);
607 /// Removes the rfcomm server socket which was created using CreateServerSocket().
610 /// The socket must be created with CreateServerSocket(). The ConnectionStateChanged event is raised after this API is called.
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)
618 if (IsBluetoothEnabled && Globals.IsInitialize)
620 BluetoothAdapterImpl.Instance.DestroyServerSocket(socket);