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="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
38 return BluetoothAdapterImpl.Instance.IsBluetoothEnabled;
43 /// The local adapter address.
46 /// The Bluetooth must be enabled.
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
54 if (IsBluetoothEnabled)
56 return BluetoothAdapterImpl.Instance.Address;
66 /// The name of the local adapter.
69 /// The Bluetooth must be enabled.
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
77 if (IsBluetoothEnabled)
79 return BluetoothAdapterImpl.Instance.Name;
88 BluetoothAdapterImpl.Instance.Name = value;
93 /// The visibility mode of the Bluetooth adapter.
96 /// The Bluetooth must be enabled.
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
104 if (IsBluetoothEnabled)
106 return BluetoothAdapterImpl.Instance.Visibility;
110 return VisibilityMode.NonDiscoverable;
116 /// A property to check whether the device discovery process is in progress.
119 /// The Bluetooth must be enabled.
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
127 if (IsBluetoothEnabled)
129 return BluetoothAdapterImpl.Instance.IsDiscoveryInProgress;
139 /// The remaining time, in seconds, until the visibility mode is changed from TimeLimitedDiscoverable to NonDiscoverable.
142 /// The Bluetooth must be enabled.
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
150 if (IsBluetoothEnabled)
152 return BluetoothAdapterImpl.Instance.RemainingTimeAsVisible;
162 /// The StateChanged event is raised when the Bluetooth adapter state is changed.
164 static public event EventHandler<StateChangedEventArgs> StateChanged
168 BluetoothAdapterImpl.Instance.StateChanged += value;
172 BluetoothAdapterImpl.Instance.StateChanged -= value;
177 /// The NameChanged event is raised when the Bluetooth adapter name is changed.
179 static public event EventHandler<NameChangedEventArgs> NameChanged
183 BluetoothAdapterImpl.Instance.NameChanged += value;
187 BluetoothAdapterImpl.Instance.NameChanged -= value;
192 /// The VisibilityModeChanged event is raised when the Bluetooth adapter visibility mode is changed.
194 static public event EventHandler<VisibilityModeChangedEventArgs> VisibilityModeChanged
198 BluetoothAdapterImpl.Instance.VisibilityModeChanged += value;
202 BluetoothAdapterImpl.Instance.VisibilityModeChanged -= value;
207 /// The VisibilityDurationChanged event is raised very second until the visibility mode is changed to NonDiscoverable.
209 static public event EventHandler<VisibilityDurationChangedEventArgs> VisibilityDurationChanged
213 BluetoothAdapterImpl.Instance.VisibilityDurationChanged += value;
217 BluetoothAdapterImpl.Instance.VisibilityDurationChanged -= value;
222 /// The DiscoveryStateChanged event is raised when the device discovery state is changed.
224 static public event EventHandler<DiscoveryStateChangedEventArgs> DiscoveryStateChanged
228 BluetoothAdapterImpl.Instance.DiscoveryStateChanged += value;
232 BluetoothAdapterImpl.Instance.DiscoveryStateChanged -= value;
237 /// This event is called when the LE scan result is obtained.
239 static public event EventHandler<AdapterLeScanResultChangedEventArgs> ScanResultChanged
243 BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged += value;
246 BluetoothLeImplAdapter.Instance.AdapterLeScanResultChanged -= value;
251 /// Starts the device discovery process.
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.
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()
262 if (IsBluetoothEnabled)
264 BluetoothAdapterImpl.Instance.StartDiscovery();
269 /// Stops the device discovery process.
272 /// The device discovery process must be in progress with StartDiscovery().
273 /// If this succeeds, the DiscoveryStateChanged event will be invoked.
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()
280 if (IsDiscoveryInProgress)
282 BluetoothAdapterImpl.Instance.StopDiscovery();
287 /// Retrieves the device information of all bonded devices.
290 /// The Bluetooth must be enabled.
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()
298 if (IsBluetoothEnabled)
300 return BluetoothAdapterImpl.Instance.GetBondedDevices();
309 /// Gets the device information of a bonded device.
312 /// The Bluetooth must be enabled.
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)
320 if (IsBluetoothEnabled)
322 return BluetoothAdapterImpl.Instance.GetBondedDevice(address);
331 /// Checks whether the UUID of service is used or not.
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)
339 return BluetoothAdapterImpl.Instance.IsServiceUsed(serviceUuid);
343 /// Gets the hash and the randomizer value of the local OOB data object.
346 /// The Bluetooth must be enabled.
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()
354 if (IsBluetoothEnabled && Globals.IsInitialize)
356 return BluetoothAdapterImpl.Instance.GetLocalOobData();
365 /// Sets the hash and the randmoizer value of the OOB data into the remote device.
368 /// The Bluetooth must be enabled.
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)
377 if (IsBluetoothEnabled && Globals.IsInitialize)
379 BluetoothAdapterImpl.Instance.SetRemoteOobData(address, oobData);
384 /// Removes the hash and the randomizer value of the OOB data from the remote device.
387 /// The Bluetooth must be enabled.
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)
395 if (IsBluetoothEnabled && Globals.IsInitialize)
397 BluetoothAdapterImpl.Instance.RemoveRemoteOobData(address);
402 /// Starts the Bluetooth LE scan operation to discover BLE devices
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()
412 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
414 int ret = BluetoothLeImplAdapter.Instance.StartScan ();
415 if (ret != (int)BluetoothError.None)
417 Log.Error(Globals.LogTag, "Failed to in start the le scan operation, Error - " + (BluetoothError)ret);
418 BluetoothErrorFactory.ThrowBluetoothException(ret);
423 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
428 /// Stops the Bluetooth LE scan operation.
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()
438 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
440 int ret = BluetoothLeImplAdapter.Instance.StopScan ();
441 if (ret != (int)BluetoothError.None)
443 Log.Error(Globals.LogTag, "Failed to stop the le scan operation, Error - " + (BluetoothError)ret);
444 BluetoothErrorFactory.ThrowBluetoothException(ret);
449 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
454 /// Returns the BluetoothLeAdvertiser instance.
457 /// The Bluetooth must be enabled before calling this API.
459 /// <returns>The BluetoothLeAdvertiser instance.</returns>
460 static public BluetoothLeAdvertiser GetBluetoothLeAdvertiser()
462 return BluetoothLeAdvertiser.Instance;
466 /// Registers a rfcomm server socket with a specific UUID.
469 /// The Bluetooth must be enabled before calling this API.
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)
478 if (IsBluetoothEnabled && Globals.IsInitialize)
480 return BluetoothAdapterImpl.Instance.CreateServerSocket (serviceUuid);
489 /// Removes the rfcomm server socket which was created using CreateServerSocket().
492 /// The socket must be created with CreateServerSocket(). The ConnectionStateChanged event is raised after this API is called.
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)
500 if (IsBluetoothEnabled && Globals.IsInitialize)
502 BluetoothAdapterImpl.Instance.DestroyServerSocket(socket);