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.Threading.Tasks;
19 using System.Collections.Generic;
20 using System.Collections.Specialized;
21 using System.Runtime.InteropServices;
23 namespace Tizen.Network.Bluetooth {
26 /// This is the BluetoothLeAdvertiser class. It handles the LE advertising operation amd callback.
28 /// <since_tizen> 3 </since_tizen>
29 public class BluetoothLeAdvertiser
31 private static readonly BluetoothLeAdvertiser _instance = new BluetoothLeAdvertiser();
33 internal static BluetoothLeAdvertiser Instance
41 private BluetoothLeAdvertiser()
46 /// This event is called when the LE advertising state changes.
48 /// <since_tizen> 3 </since_tizen>
49 public event EventHandler<AdvertisingStateChangedEventArgs> AdvertisingStateChanged
53 BluetoothLeImplAdapter.Instance.AdapterLeAdvertisingStateChanged += value;
57 BluetoothLeImplAdapter.Instance.AdapterLeAdvertisingStateChanged -= value;
61 /// Starts advertising using the advertise data object.
64 /// The Bluetooth must be enabled.
66 /// <param name="advertiseData">The advertiser object carrying information of the advertising.</param>
67 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
68 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
69 /// <since_tizen> 3 </since_tizen>
70 public void StartAdvertising(BluetoothLeAdvertiseData advertiseData)
72 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
74 int ret = BluetoothLeImplAdapter.Instance.StartAdvertising (advertiseData.GetHandle ());
75 if (ret != (int)BluetoothError.None) {
76 Log.Error (Globals.LogTag, "Failed to start advertising- " + (BluetoothError)ret);
77 BluetoothErrorFactory.ThrowBluetoothException(ret);
82 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
87 /// Stops the advertising.
90 /// The Bluetooth must be enabled.
92 /// <param name="advertiseData">The advertiser object carrying information of the advertising.</param>
93 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
94 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
95 /// <since_tizen> 3 </since_tizen>
96 public void StopAdvertising(BluetoothLeAdvertiseData advertiseData)
98 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
100 int ret = BluetoothLeImplAdapter.Instance.StopAdvertising (advertiseData.GetHandle ());
101 if (ret != (int)BluetoothError.None) {
102 Log.Error (Globals.LogTag, "Failed to stop advertising operation- " + (BluetoothError)ret);
103 BluetoothErrorFactory.ThrowBluetoothException(ret);
108 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
114 /// This is the BluetoothLeDevice class.
115 /// It handles the LE device operations like getting data from the scan result.
117 /// <since_tizen> 3 </since_tizen>
118 public class BluetoothLeDevice
120 //properties of Bluetoothlesacandata
121 private string _remoteAddress;
122 private BluetoothLeDeviceAddressType _addressType;
124 private byte[] _advDataValue;
125 private byte[] _scanDataValue;
126 private BluetoothLePacketType _packetType;
127 private BluetoothLeScanData _scanData;
130 /// This event is called when the GATT client connects/disconnects with the server.
132 /// <since_tizen> 3 </since_tizen>
133 [Obsolete("Deprecated since API level 6. Please use ConnectionStateChanged event on BluetoothGattClient.")]
134 public event EventHandler<GattConnectionStateChangedEventArgs> GattConnectionStateChanged
138 BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged += value;
142 BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged -= value;
146 internal BluetoothLeDevice(BluetoothLeScanData scanData)
148 _scanData = new BluetoothLeScanData ();
149 _scanData = scanData;
151 Log.Info (Globals.LogTag, "Rssi" + _scanData.Rssi);
152 _rssi = scanData.Rssi;
153 Log.Info (Globals.LogTag, "RemoteAddress" + _scanData.RemoteAddress);
154 if (scanData.RemoteAddress != null)
155 _remoteAddress = scanData.RemoteAddress;
156 Log.Info (Globals.LogTag, "AddressType" + _scanData.AddressType);
157 _addressType = scanData.AddressType;
159 Log.Info (Globals.LogTag, "AdvDataLength" + _scanData.AdvDataLength);
160 if (_scanData.AdvDataLength > 0)
162 _advDataValue = new byte[_scanData.AdvDataLength];
163 scanData.AdvData.CopyTo(_advDataValue, 0);
166 Log.Info(Globals.LogTag, "ScanDataLength" + _scanData.ScanDataLength);
167 // Check length before copying
168 if (_scanData.ScanDataLength > 0)
170 _scanDataValue = new byte[_scanData.ScanDataLength];
171 scanData.ScanData.CopyTo(_scanDataValue, 0);
176 /// BluetoothLeDevice destructor.
180 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
182 BluetoothLeImplAdapter.Instance.FreeServiceDataList();
187 /// The remote address.
189 /// <since_tizen> 3 </since_tizen>
190 public string RemoteAddress
194 return _remoteAddress;
199 /// The type of the address.
201 /// <since_tizen> 3 </since_tizen>
202 public BluetoothLeDeviceAddressType AddressType
213 /// <since_tizen> 3 </since_tizen>
223 /// The advertsing data information.
225 /// <since_tizen> 3 </since_tizen>
226 public byte[] AdvertsingDataInformation
230 return _advDataValue;
235 /// The scan data information.
237 /// <since_tizen> 3 </since_tizen>
238 public byte[] ScanDataInformation
242 return _scanDataValue;
247 /// The type of the packet.
249 /// <since_tizen> 3 </since_tizen>
250 [Obsolete("Deprecated since API level 6. Please use new methods with this type on BluetoothLeDevice.")]
251 public BluetoothLePacketType PacketType
264 /// Gets the service UUIDs list from the LE scan result.
266 /// <value> Gets the list of the string service UUIDs.</value>
268 /// The Bluetooth must be enabled.
270 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
271 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
272 /// <since_tizen> 3 </since_tizen>
273 [Obsolete("Deprecated since API level 6. Please use GetServiceUuid() method on BluetoothLeDevice.")]
274 public IEnumerable<string> ServiceUuid
278 return GetServiceUuid(_packetType);
283 /// Gets the service UUIDs list from the LE scan result.
285 /// <value> Gets the list of the string service UUIDs.</value>
286 /// <remarks>The Bluetooth must be enabled.</remarks>
287 /// <param name="packetType"> The enumeration for BLE packet type.</param>
288 /// <returns>The service UUID value</returns>
289 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
290 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
291 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
292 /// <since_tizen> 6 </since_tizen>
293 public IEnumerable<string> GetServiceUuid(BluetoothLePacketType packetType)
295 return BluetoothLeImplAdapter.Instance.GetLeScanResultServiceUuids(_scanData, packetType);
299 /// Gets the device name from the LE scan result.
301 /// <value> Gets the device name.</value>
303 /// The Bluetooth must be enabled.
305 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
306 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
307 /// <since_tizen> 3 </since_tizen>
308 [Obsolete("Deprecated since API level 6. Please use GetDeviceName() method on BluetoothLeDevice.")]
309 public string DeviceName
313 return GetDeviceName(_packetType);
318 /// Gets the device name from the LE scan result.
320 /// <value> Gets the device name.</value>
321 /// <remarks>The Bluetooth must be enabled.</remarks>
322 /// <param name="packetType"> The enumeration for BLE packet type.</param>
323 /// <returns>The device name value</returns>
324 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
325 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
326 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
327 /// <since_tizen> 6 </since_tizen>
328 public string GetDeviceName(BluetoothLePacketType packetType)
330 return BluetoothLeImplAdapter.Instance.GetLeScanResultDeviceName(_scanData, packetType);
334 /// Gets the transmission power level from the LE scan result.
336 /// <value> Gets the transmission power level in dB.</value>
338 /// The Bluetooth must be enabled.
340 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
341 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
342 /// <since_tizen> 3 </since_tizen>
343 [Obsolete("Deprecated since API level 6. Please use GetTxPowerLevel() method on BluetoothLeDevice.")]
344 public int TxPowerLevel
348 return GetTxPowerLevel(_packetType);
353 /// Gets the transmission power level from the LE scan result.
355 /// <value> Gets the transmission power level in dB.</value>
356 /// <remarks>The Bluetooth must be enabled.</remarks>
357 /// <param name="packetType"> The enumeration for BLE packet type.</param>
358 /// <returns>The tx power level value</returns>
359 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
360 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
361 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
362 /// <since_tizen> 6 </since_tizen>
363 public int GetTxPowerLevel(BluetoothLePacketType packetType)
365 return BluetoothLeImplAdapter.Instance.GetScanResultTxPowerLevel(_scanData, packetType);
369 /// Gets the service solicitation UUID list from the scan result.
371 /// <value> Gets the list of the service solicitation UUIDs.</value>
373 /// The Bluetooth must be enabled.
375 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
376 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
377 /// <since_tizen> 3 </since_tizen>
378 [Obsolete("Deprecated since API level 6. Please use GetServiceSolicitationUuid() method on BluetoothLeDevice.")]
379 public IEnumerable<string> ServiceSolictationUuid
383 return GetServiceSolicitationUuid(_packetType);
388 /// Gets the service solicitation UUID list from the scan result.
390 /// <value> Gets the list of the service solicitation UUIDs.</value>
391 /// <remarks>The Bluetooth must be enabled.</remarks>
392 /// <param name="packetType"> The enumeration for BLE packet type.</param>
393 /// <returns>The service solicitation UUID value</returns>
394 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
395 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
396 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
397 /// <since_tizen> 6 </since_tizen>
398 public IEnumerable<string> GetServiceSolicitationUuid(BluetoothLePacketType packetType)
400 return BluetoothLeImplAdapter.Instance.GetScanResultSvcSolicitationUuids(_scanData, packetType);
404 /// Gets the manufacturer data from the scan result.
406 /// <value> Gets the appearance value.</value>
408 /// The Bluetooth must be enabled.
410 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
411 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
412 /// <since_tizen> 3 </since_tizen>
413 [Obsolete("Deprecated since API level 6. Please use GetAppearance() method on BluetoothLeDevice.")]
414 public int Appearance
418 return GetAppearance(_packetType);
423 /// Gets the manufacturer data from the scan result.
425 /// <value> Gets the appearance value.</value>
426 /// <remarks>The Bluetooth must be enabled.</remarks>
427 /// <param name="packetType"> The enumeration for BLE packet type.</param>
428 /// <returns>The appearance value</returns>
429 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
430 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
431 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
432 /// <since_tizen> 6 </since_tizen>
433 public int GetAppearance(BluetoothLePacketType packetType)
435 return BluetoothLeImplAdapter.Instance.GetScanResultAppearance(_scanData, packetType);
439 /// Gets the manufacturer data from the scan result.
441 /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
443 /// The Bluetooth must be enabled.
445 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
446 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>///
447 /// <since_tizen> 3 </since_tizen>
448 [Obsolete("Deprecated since API level 6. Please use GetManufacturerData() method on BluetoothLeDevice.")]
449 public ManufacturerData ManufacturerData
453 return GetManufacturerData(_packetType);
458 /// Gets the manufacturer data from the scan result.
460 /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
461 /// <remarks>The Bluetooth must be enabled.</remarks>
462 /// <param name="packetType"> The enumeration for BLE packet type.</param>
463 /// <returns>The manufacturer data object</returns>
464 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
465 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
466 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>///
467 /// <since_tizen> 6 </since_tizen>
468 public ManufacturerData GetManufacturerData(BluetoothLePacketType packetType)
470 return BluetoothLeImplAdapter.Instance.GetScanResultManufacturerData(_scanData, packetType);
474 /// Gets the service data list from the scan result.
477 /// The Bluetooth must be enabled.
479 /// <returns> Returns the service data list.</returns>
480 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
481 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
482 /// <since_tizen> 3 </since_tizen>
483 [Obsolete("Deprecated since API level 6. Please use GetServiceDataList() method on BluetoothLeDevice.")]
484 public IEnumerable<BluetoothLeServiceData> GetServiceDataList()
486 return GetServiceDataList(_packetType);
490 /// Gets the service data list from the scan result.
492 /// <remarks>The Bluetooth must be enabled.</remarks>
493 /// <param name="packetType"> The packet type.</param>
494 /// <returns> Returns the service data list.</returns>
495 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
496 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
497 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
498 /// <since_tizen> 6 </since_tizen>
499 public IEnumerable<BluetoothLeServiceData> GetServiceDataList(BluetoothLePacketType packetType)
501 return BluetoothLeImplAdapter.Instance.GetScanResultServiceDataList(_scanData, packetType);
505 /// Creates a GATT connection with the remote device.
508 /// The Bluetooth must be enabled.
510 /// <param name="autoConnect"> The auto connect flag.</param>
511 /// <returns>client instance</returns>
512 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
513 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
514 /// or when the gatt connection attempt to remote device fails.</exception>
515 /// <since_tizen> 3 </since_tizen>
516 [Obsolete("Deprecated since API level 6. Please use CreateClient() and ConnectAsync() method on BluetoothGattClient.")]
517 public BluetoothGattClient GattConnect(bool autoConnect)
519 BluetoothGattClient client = null;
520 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
522 int ret = BluetoothLeImplAdapter.Instance.GattConnect (_remoteAddress, autoConnect);
523 if (ret != (int)BluetoothError.None) {
524 Log.Error (Globals.LogTag, "Failed to create GATT Connection with remote device- " + (BluetoothError)ret);
528 client = BluetoothGattClient.CreateClient(_remoteAddress);
533 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
539 /// Disconnects a GATT connection with the remote device.
542 /// The Bluetooth must be enabled.
544 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
545 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
546 /// or when the GATT disconnection attempt to remote device fails.</exception>
547 /// <since_tizen> 3 </since_tizen>
548 [Obsolete("Deprecated since API level 6. Please use DisconnectAsync() method on BluetoothGattClient.")]
549 public void GattDisconnect()
551 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
553 int ret = BluetoothLeImplAdapter.Instance.GattDisconnect (_remoteAddress);
554 if (ret != (int)BluetoothError.None) {
555 Log.Error (Globals.LogTag, "Failed to disconnect GATT connection with remote device- " + (BluetoothError)ret);
560 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
566 /// Bluetooth LE advertise data. Handles the data advertising.
568 /// <since_tizen> 3 </since_tizen>
569 public class BluetoothLeAdvertiseData:IDisposable
571 private IntPtr _handle = IntPtr.Zero;
572 private BluetoothLeAdvertisingMode _mode;
573 private bool _advertisingConnectable;
574 private BluetoothLePacketType _packetType;
575 private int _appearance;
576 private bool _includePowerLevel;
577 private bool _includeDeviceName;
580 /// The default constructor initializes an object of the BluetoothLeAdvertiseData.
582 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
583 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
584 /// or when create advertiser fails.</exception>
585 /// <since_tizen> 3 </since_tizen>
586 public BluetoothLeAdvertiseData()
588 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
590 Log.Debug(Globals.LogTag, " Creating LeAdvertiser()");
591 int ret = Interop.Bluetooth.CreateAdvertiser(out _handle);
592 if (ret != (int)BluetoothError.None)
594 Log.Error(Globals.LogTag, "Failed to create advertiser object- " + (BluetoothError)ret);
595 BluetoothErrorFactory.ThrowBluetoothException(ret);
600 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
605 /// BluetoothLeAdvertiseData destructor.
607 ~BluetoothLeAdvertiseData()
609 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
612 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeAdvertisingPacket);
613 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeScanResponsePacket);
614 BluetoothLeImplAdapter.Instance.DestroyAdvertiser (_handle);
619 internal IntPtr GetHandle()
625 /// The advertising mode to control the advertising power and latency.
628 /// The Bluetooth must be enabled.
630 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
631 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
632 /// or when the set advertising mode fails.</exception>
633 /// <since_tizen> 3 </since_tizen>
634 public BluetoothLeAdvertisingMode AdvertisingMode
642 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
645 int ret = Interop.Bluetooth.SetAdvertisingMode (GetHandle (), _mode);
646 if (ret != (int)BluetoothError.None) {
647 Log.Error (Globals.LogTag, "Failed to set advertising mode- " + (BluetoothError)ret);
648 BluetoothErrorFactory.ThrowBluetoothException (ret);
655 /// The advertising connectable type.
658 /// The Bluetooth must be enabled.
660 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
661 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
662 /// or when the set advertising connectable mode fails.</exception>
663 /// <since_tizen> 3 </since_tizen>
664 public bool AdvertisingConnectable
668 return _advertisingConnectable;
672 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
674 _advertisingConnectable = value;
675 int ret = Interop.Bluetooth.SetAdvertisingConnectable (GetHandle (), _advertisingConnectable);
676 if (ret != (int)BluetoothError.None) {
677 Log.Error (Globals.LogTag, "Failed to set advertising connectable value- " + (BluetoothError)ret);
678 BluetoothErrorFactory.ThrowBluetoothException (ret);
687 /// <since_tizen> 3 </since_tizen>
688 public void Dispose()
691 GC.SuppressFinalize(this);
694 private void Dispose(bool disposing)
700 /// The type of the packet.
702 /// <since_tizen> 3 </since_tizen>
703 public BluetoothLePacketType PacketType
715 /// Sets the external appearance of this device to the advertise or the scan response data.
716 /// Please refer to the adopted Bluetooth specification for the appearance.
719 /// The Bluetooth must be enabled.
721 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
722 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
723 /// or when the set appearance fails.</exception>
724 /// <since_tizen> 3 </since_tizen>
725 public int Appearance
734 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) {
735 int ret = Interop.Bluetooth.SetAdvertisingAppearance (GetHandle (), _packetType, _appearance);
736 if (ret != (int)BluetoothError.None) {
737 Log.Error (Globals.LogTag, "Failed to add appearance value to advertising data- " + (BluetoothError)ret);
738 BluetoothErrorFactory.ThrowBluetoothException(ret);
745 /// Sets whether the device name has to be included in the advertise or the scan response data.
746 /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
749 /// The Bluetooth must be enabled.
751 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
752 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
753 /// or when the set advertising device name fails.</exception>
754 /// <since_tizen> 3 </since_tizen>
755 public bool IncludeDeviceName
759 return _includeDeviceName;
763 _includeDeviceName = value;
764 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
766 int ret = Interop.Bluetooth.SetAdvertisingDeviceName(GetHandle(), _packetType, _includeDeviceName);
767 if (ret != (int)BluetoothError.None) {
768 Log.Error (Globals.LogTag, "Failed to add device name to advertising data- " + (BluetoothError)ret);
769 BluetoothErrorFactory.ThrowBluetoothException(ret);
777 /// Sets whether the transmission power level should be included in the advertise or the scan response data.
778 /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
781 /// The Bluetooth must be enabled.
783 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
784 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
785 /// or when the set advertising TC power level fails.</exception>
786 /// <since_tizen> 3 </since_tizen>
787 public bool IncludeTxPowerLevel
791 return _includePowerLevel;
795 _includePowerLevel = value;
796 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
798 int ret = Interop.Bluetooth.SetAdvertisingTxPowerLevel(GetHandle(), _packetType, _includePowerLevel);
799 if (ret != (int)BluetoothError.None)
801 Log.Error(Globals.LogTag, "Failed to add advertising service solicitation uuid- " + (BluetoothError)ret);
807 /// Adds a service UUID to the advertise or the scan response data.
808 /// The maximum advertised or responded data size is 31 bytes
809 /// including the data type and the system wide data.
812 /// The Bluetooth must be enabled.
814 /// <param name="packetType">The packet type.</param>
815 /// <param name="serviceUuid"> The service UUID to add to advertise data.</param>
816 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
817 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
818 /// or when the add advertising service UUID procedure fails.</exception>
819 /// <since_tizen> 3 </since_tizen>
820 public void AddAdvertisingServiceUuid(BluetoothLePacketType packetType, string serviceUuid)
822 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
824 int ret = Interop.Bluetooth.AddAdvertisingServiceUuid (GetHandle (), packetType, serviceUuid);
825 if (ret != (int)BluetoothError.None) {
826 Log.Error (Globals.LogTag, "Failed to add service uuid to advertising data- " + (BluetoothError)ret);
827 BluetoothErrorFactory.ThrowBluetoothException (ret);
832 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
837 /// Adds a service solicitation UUID to advertise or scan the response data.
838 /// The maximum advertised or responded data size is 31 bytes
839 /// including the data type and the system wide data.
842 /// The Bluetooth must be enabled.
844 /// <param name="packetType">The packet type.</param>
845 /// <param name="serviceSolicitationUuid"> The service solicitation UUID to add to advertise data.</param>
846 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
847 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
848 /// or when the add advertising service solicitation UUID procedure fails.</exception>
849 /// <since_tizen> 3 </since_tizen>
850 public void AddAdvertisingServiceSolicitationUuid(BluetoothLePacketType packetType,
851 string serviceSolicitationUuid)
853 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
855 int ret = Interop.Bluetooth.AddAdvertisingServiceSolicitationUuid(GetHandle(), packetType,
856 serviceSolicitationUuid);
857 if (ret != (int)BluetoothError.None) {
858 Log.Error (Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
859 BluetoothErrorFactory.ThrowBluetoothException(ret);
864 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
869 /// Adds a service data to the advertise or the scan response data.
870 /// The maximum advertised or responded data size is 31 bytes
871 /// including data type and system wide data.
874 /// The Bluetooth must be enabled.
876 /// <param name="packetType">The packet type.</param>
877 /// <param name="data"> The service data to be added to advertising.</param>
878 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
879 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
880 /// or when the add advertising data procedure fails.</exception>
881 /// <since_tizen> 3 </since_tizen>
882 public void AddAdvertisingServiceData(BluetoothLePacketType packetType, BluetoothServiceData data)
884 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
886 IntPtr serviceDataPtr;
887 serviceDataPtr = Marshal.AllocHGlobal(data.DataLength);
888 Marshal.Copy(data.Data, 0, serviceDataPtr, data.DataLength);
890 for (int i = 0; i < data.DataLength; i++)
891 Log.Error (Globals.LogTag, " service data is " + data.Data [i]);
892 int ret = Interop.Bluetooth.AddAdvertisingServiceData(GetHandle(), packetType,
893 data.Uuid, serviceDataPtr, data.DataLength);
894 if (ret != (int)BluetoothError.None)
896 Log.Error(Globals.LogTag, "Failed to add service data to advertising data- " + (BluetoothError)ret);
897 BluetoothErrorFactory.ThrowBluetoothException(ret);
902 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
907 /// Adds the manufacturer specific data to the advertise or the scan response data.
908 /// Please refer to the adopted Bluetooth specification for the the appearance.
911 /// The Bluetooth must be enabled.
913 /// <param name="packetType">The packet type.</param>
914 /// <param name="manufacturerData"> The manufacturer specific data.</param>
915 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
916 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
917 /// or when the add advertising manufacturer data procedure fails.</exception>
918 /// <since_tizen> 3 </since_tizen>
919 public void AddAdvertisingManufacturerData(BluetoothLePacketType packetType,
920 ManufacturerData manufacturerData)
922 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
925 manufDataPtr = Marshal.AllocHGlobal(manufacturerData.DataLength);
926 Marshal.Copy(manufacturerData.Data, 0, manufDataPtr, manufacturerData.DataLength);
928 int ret = Interop.Bluetooth.AddAdvertisingManufData(GetHandle(), packetType,
929 manufacturerData.Id, manufDataPtr, manufacturerData.DataLength);
930 if (ret != (int)BluetoothError.None)
932 Log.Error(Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
933 BluetoothErrorFactory.ThrowBluetoothException(ret);
938 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
943 /// Clears all data to be advertised or responded to the scan request from the LE scanning device.
946 /// The Bluetooth must be enabled.
948 /// <param name="packetType">The packet type to be cleared.</param>
949 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
950 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
951 /// or when the clear advertising data procedure fails.</exception>
952 internal void ClearAdvertisingData(BluetoothLePacketType packetType)
954 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
956 int ret = Interop.Bluetooth.ClearAdvertisingData (GetHandle (), packetType);
957 if (ret != (int)BluetoothError.None) {
958 Log.Error (Globals.LogTag, "Failed to Clear Advertising Data- " + (BluetoothError)ret);
963 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);