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 /// The remote address.
178 /// <since_tizen> 3 </since_tizen>
179 public string RemoteAddress
183 return _remoteAddress;
188 /// The type of the address.
190 /// <since_tizen> 3 </since_tizen>
191 public BluetoothLeDeviceAddressType AddressType
202 /// <since_tizen> 3 </since_tizen>
212 /// The advertsing data information.
214 /// <since_tizen> 3 </since_tizen>
215 public byte[] AdvertsingDataInformation
219 return _advDataValue;
224 /// The scan data information.
226 /// <since_tizen> 3 </since_tizen>
227 public byte[] ScanDataInformation
231 return _scanDataValue;
236 /// The type of the packet.
238 /// <since_tizen> 3 </since_tizen>
239 [Obsolete("Deprecated since API level 6. Please use new methods with this type on BluetoothLeDevice.")]
240 public BluetoothLePacketType PacketType
253 /// Gets the service UUIDs list from the LE scan result.
255 /// <value> Gets the list of the string service UUIDs.</value>
257 /// The Bluetooth must be enabled.
259 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
260 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
261 /// <since_tizen> 3 </since_tizen>
262 [Obsolete("Deprecated since API level 6. Please use GetServiceUuid() method on BluetoothLeDevice.")]
263 public IEnumerable<string> ServiceUuid
267 return GetServiceUuid(_packetType);
272 /// Gets the service UUIDs list from the LE scan result.
274 /// <value> Gets the list of the string service UUIDs.</value>
275 /// <remarks>The Bluetooth must be enabled.</remarks>
276 /// <param name="packetType"> The enumeration for BLE packet type.</param>
277 /// <returns>The service UUID value</returns>
278 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
279 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
280 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
281 /// <since_tizen> 6 </since_tizen>
282 public IEnumerable<string> GetServiceUuid(BluetoothLePacketType packetType)
284 return BluetoothLeImplAdapter.Instance.GetLeScanResultServiceUuids(_scanData, packetType);
288 /// Gets the device name from the LE scan result.
290 /// <value> Gets the device name.</value>
292 /// The Bluetooth must be enabled.
294 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
295 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
296 /// <since_tizen> 3 </since_tizen>
297 [Obsolete("Deprecated since API level 6. Please use GetDeviceName() method on BluetoothLeDevice.")]
298 public string DeviceName
302 return GetDeviceName(_packetType);
307 /// Gets the device name from the LE scan result.
309 /// <value> Gets the device name.</value>
310 /// <remarks>The Bluetooth must be enabled.</remarks>
311 /// <param name="packetType"> The enumeration for BLE packet type.</param>
312 /// <returns>The device name value</returns>
313 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
314 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
315 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
316 /// <since_tizen> 6 </since_tizen>
317 public string GetDeviceName(BluetoothLePacketType packetType)
319 return BluetoothLeImplAdapter.Instance.GetLeScanResultDeviceName(_scanData, packetType);
323 /// Gets the transmission power level from the LE scan result.
325 /// <value> Gets the transmission power level in dB.</value>
327 /// The Bluetooth must be enabled.
329 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
330 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
331 /// <since_tizen> 3 </since_tizen>
332 [Obsolete("Deprecated since API level 6. Please use GetTxPowerLevel() method on BluetoothLeDevice.")]
333 public int TxPowerLevel
337 return GetTxPowerLevel(_packetType);
342 /// Gets the transmission power level from the LE scan result.
344 /// <value> Gets the transmission power level in dB.</value>
345 /// <remarks>The Bluetooth must be enabled.</remarks>
346 /// <param name="packetType"> The enumeration for BLE packet type.</param>
347 /// <returns>The tx power level value</returns>
348 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
349 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
350 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
351 /// <since_tizen> 6 </since_tizen>
352 public int GetTxPowerLevel(BluetoothLePacketType packetType)
354 return BluetoothLeImplAdapter.Instance.GetScanResultTxPowerLevel(_scanData, packetType);
358 /// Gets the service solicitation UUID list from the scan result.
360 /// <value> Gets the list of the service solicitation UUIDs.</value>
362 /// The Bluetooth must be enabled.
364 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
365 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
366 /// <since_tizen> 3 </since_tizen>
367 [Obsolete("Deprecated since API level 6. Please use GetServiceSolicitationUuid() method on BluetoothLeDevice.")]
368 public IEnumerable<string> ServiceSolictationUuid
372 return GetServiceSolicitationUuid(_packetType);
377 /// Gets the service solicitation UUID list from the scan result.
379 /// <value> Gets the list of the service solicitation UUIDs.</value>
380 /// <remarks>The Bluetooth must be enabled.</remarks>
381 /// <param name="packetType"> The enumeration for BLE packet type.</param>
382 /// <returns>The service solicitation UUID value</returns>
383 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
384 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
385 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
386 /// <since_tizen> 6 </since_tizen>
387 public IEnumerable<string> GetServiceSolicitationUuid(BluetoothLePacketType packetType)
389 return BluetoothLeImplAdapter.Instance.GetScanResultSvcSolicitationUuids(_scanData, packetType);
393 /// Gets the manufacturer data from the scan result.
395 /// <value> Gets the appearance value.</value>
397 /// The Bluetooth must be enabled.
399 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
400 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
401 /// <since_tizen> 3 </since_tizen>
402 [Obsolete("Deprecated since API level 6. Please use GetAppearance() method on BluetoothLeDevice.")]
403 public int Appearance
407 return GetAppearance(_packetType);
412 /// Gets the manufacturer data from the scan result.
414 /// <value> Gets the appearance value.</value>
415 /// <remarks>The Bluetooth must be enabled.</remarks>
416 /// <param name="packetType"> The enumeration for BLE packet type.</param>
417 /// <returns>The appearance value</returns>
418 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
419 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
420 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
421 /// <since_tizen> 6 </since_tizen>
422 public int GetAppearance(BluetoothLePacketType packetType)
424 return BluetoothLeImplAdapter.Instance.GetScanResultAppearance(_scanData, packetType);
428 /// Gets the manufacturer data from the scan result.
430 /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
432 /// The Bluetooth must be enabled.
434 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
435 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>///
436 /// <since_tizen> 3 </since_tizen>
437 [Obsolete("Deprecated since API level 6. Please use GetManufacturerData() method on BluetoothLeDevice.")]
438 public ManufacturerData ManufacturerData
442 return GetManufacturerData(_packetType);
447 /// Gets the manufacturer data from the scan result.
449 /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
450 /// <remarks>The Bluetooth must be enabled.</remarks>
451 /// <param name="packetType"> The enumeration for BLE packet type.</param>
452 /// <returns>The manufacturer data object</returns>
453 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
454 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
455 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>///
456 /// <since_tizen> 6 </since_tizen>
457 public ManufacturerData GetManufacturerData(BluetoothLePacketType packetType)
459 return BluetoothLeImplAdapter.Instance.GetScanResultManufacturerData(_scanData, packetType);
463 /// Gets the service data list from the scan result.
466 /// The Bluetooth must be enabled.
468 /// <returns> Returns the service data list.</returns>
469 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
470 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
471 /// <since_tizen> 3 </since_tizen>
472 [Obsolete("Deprecated since API level 6. Please use GetServiceDataList() method on BluetoothLeDevice.")]
473 public IEnumerable<BluetoothLeServiceData> GetServiceDataList()
475 return GetServiceDataList(_packetType);
479 /// Gets the service data list from the scan result.
481 /// <remarks>The Bluetooth must be enabled.</remarks>
482 /// <param name="packetType"> The packet type.</param>
483 /// <returns> Returns the service data list.</returns>
484 /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
485 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
486 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
487 /// <since_tizen> 6 </since_tizen>
488 public IEnumerable<BluetoothLeServiceData> GetServiceDataList(BluetoothLePacketType packetType)
490 return BluetoothLeImplAdapter.Instance.GetScanResultServiceDataList(_scanData, packetType);
494 /// Creates a GATT connection with the remote device.
497 /// The Bluetooth must be enabled.
499 /// <param name="autoConnect"> The auto connect flag.</param>
500 /// <returns>client instance</returns>
501 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
502 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
503 /// or when the gatt connection attempt to remote device fails.</exception>
504 /// <since_tizen> 3 </since_tizen>
505 [Obsolete("Deprecated since API level 6. Please use CreateClient() and ConnectAsync() method on BluetoothGattClient.")]
506 public BluetoothGattClient GattConnect(bool autoConnect)
508 BluetoothGattClient client = null;
509 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
511 int ret = BluetoothLeImplAdapter.Instance.GattConnect (_remoteAddress, autoConnect);
512 if (ret != (int)BluetoothError.None) {
513 Log.Error (Globals.LogTag, "Failed to create GATT Connection with remote device- " + (BluetoothError)ret);
517 client = BluetoothGattClient.CreateClient(_remoteAddress);
522 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
528 /// Disconnects a GATT connection with the remote device.
531 /// The Bluetooth must be enabled.
533 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
534 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
535 /// or when the GATT disconnection attempt to remote device fails.</exception>
536 /// <since_tizen> 3 </since_tizen>
537 [Obsolete("Deprecated since API level 6. Please use DisconnectAsync() method on BluetoothGattClient.")]
538 public void GattDisconnect()
540 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
542 int ret = BluetoothLeImplAdapter.Instance.GattDisconnect (_remoteAddress);
543 if (ret != (int)BluetoothError.None) {
544 Log.Error (Globals.LogTag, "Failed to disconnect GATT connection with remote device- " + (BluetoothError)ret);
549 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
555 /// Bluetooth LE advertise data. Handles the data advertising.
557 /// <since_tizen> 3 </since_tizen>
558 public class BluetoothLeAdvertiseData:IDisposable
560 private IntPtr _handle = IntPtr.Zero;
561 private BluetoothLeAdvertisingMode _mode;
562 private bool _advertisingConnectable;
563 private BluetoothLePacketType _packetType;
564 private int _appearance;
565 private bool _includePowerLevel;
566 private bool _includeDeviceName;
569 /// The default constructor initializes an object of the BluetoothLeAdvertiseData.
571 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
572 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
573 /// or when create advertiser fails.</exception>
574 /// <since_tizen> 3 </since_tizen>
575 public BluetoothLeAdvertiseData()
577 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
579 Log.Debug(Globals.LogTag, " Creating LeAdvertiser()");
580 int ret = Interop.Bluetooth.CreateAdvertiser(out _handle);
581 if (ret != (int)BluetoothError.None)
583 Log.Error(Globals.LogTag, "Failed to create advertiser object- " + (BluetoothError)ret);
584 BluetoothErrorFactory.ThrowBluetoothException(ret);
589 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
594 /// BluetoothLeAdvertiseData destructor.
596 ~BluetoothLeAdvertiseData()
598 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
601 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeAdvertisingPacket);
602 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeScanResponsePacket);
603 BluetoothLeImplAdapter.Instance.DestroyAdvertiser (_handle);
608 internal IntPtr GetHandle()
614 /// The advertising mode to control the advertising power and latency.
617 /// The Bluetooth must be enabled.
619 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
620 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
621 /// or when the set advertising mode fails.</exception>
622 /// <since_tizen> 3 </since_tizen>
623 public BluetoothLeAdvertisingMode AdvertisingMode
631 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
634 int ret = Interop.Bluetooth.SetAdvertisingMode (GetHandle (), _mode);
635 if (ret != (int)BluetoothError.None) {
636 Log.Error (Globals.LogTag, "Failed to set advertising mode- " + (BluetoothError)ret);
637 BluetoothErrorFactory.ThrowBluetoothException (ret);
644 /// The advertising connectable type.
647 /// The Bluetooth must be enabled.
649 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
650 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
651 /// or when the set advertising connectable mode fails.</exception>
652 /// <since_tizen> 3 </since_tizen>
653 public bool AdvertisingConnectable
657 return _advertisingConnectable;
661 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
663 _advertisingConnectable = value;
664 int ret = Interop.Bluetooth.SetAdvertisingConnectable (GetHandle (), _advertisingConnectable);
665 if (ret != (int)BluetoothError.None) {
666 Log.Error (Globals.LogTag, "Failed to set advertising connectable value- " + (BluetoothError)ret);
667 BluetoothErrorFactory.ThrowBluetoothException (ret);
676 /// <since_tizen> 3 </since_tizen>
677 public void Dispose()
680 GC.SuppressFinalize(this);
683 private void Dispose(bool disposing)
689 /// The type of the packet.
691 /// <since_tizen> 3 </since_tizen>
692 public BluetoothLePacketType PacketType
704 /// Sets the external appearance of this device to the advertise or the scan response data.
705 /// Please refer to the adopted Bluetooth specification for the appearance.
708 /// The Bluetooth must be enabled.
710 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
711 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
712 /// or when the set appearance fails.</exception>
713 /// <since_tizen> 3 </since_tizen>
714 public int Appearance
723 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) {
724 int ret = Interop.Bluetooth.SetAdvertisingAppearance (GetHandle (), _packetType, _appearance);
725 if (ret != (int)BluetoothError.None) {
726 Log.Error (Globals.LogTag, "Failed to add appearance value to advertising data- " + (BluetoothError)ret);
727 BluetoothErrorFactory.ThrowBluetoothException(ret);
734 /// Sets whether the device name has to be included in the advertise or the scan response data.
735 /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
738 /// The Bluetooth must be enabled.
740 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
741 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
742 /// or when the set advertising device name fails.</exception>
743 /// <since_tizen> 3 </since_tizen>
744 public bool IncludeDeviceName
748 return _includeDeviceName;
752 _includeDeviceName = value;
753 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
755 int ret = Interop.Bluetooth.SetAdvertisingDeviceName(GetHandle(), _packetType, _includeDeviceName);
756 if (ret != (int)BluetoothError.None) {
757 Log.Error (Globals.LogTag, "Failed to add device name to advertising data- " + (BluetoothError)ret);
758 BluetoothErrorFactory.ThrowBluetoothException(ret);
766 /// Sets whether the transmission power level should be included in the advertise or the scan response data.
767 /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
770 /// The Bluetooth must be enabled.
772 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
773 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
774 /// or when the set advertising TC power level fails.</exception>
775 /// <since_tizen> 3 </since_tizen>
776 public bool IncludeTxPowerLevel
780 return _includePowerLevel;
784 _includePowerLevel = value;
785 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
787 int ret = Interop.Bluetooth.SetAdvertisingTxPowerLevel(GetHandle(), _packetType, _includePowerLevel);
788 if (ret != (int)BluetoothError.None)
790 Log.Error(Globals.LogTag, "Failed to add advertising service solicitation uuid- " + (BluetoothError)ret);
796 /// Adds a service UUID to the advertise or the scan response data.
797 /// The maximum advertised or responded data size is 31 bytes
798 /// including the data type and the system wide data.
801 /// The Bluetooth must be enabled.
803 /// <param name="packetType">The packet type.</param>
804 /// <param name="serviceUuid"> The service UUID to add to advertise data.</param>
805 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
806 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
807 /// or when the add advertising service UUID procedure fails.</exception>
808 /// <since_tizen> 3 </since_tizen>
809 public void AddAdvertisingServiceUuid(BluetoothLePacketType packetType, string serviceUuid)
811 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
813 int ret = Interop.Bluetooth.AddAdvertisingServiceUuid (GetHandle (), packetType, serviceUuid);
814 if (ret != (int)BluetoothError.None) {
815 Log.Error (Globals.LogTag, "Failed to add service uuid to advertising data- " + (BluetoothError)ret);
816 BluetoothErrorFactory.ThrowBluetoothException (ret);
821 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
826 /// Adds a service solicitation UUID to advertise or scan the response data.
827 /// The maximum advertised or responded data size is 31 bytes
828 /// including the data type and the system wide data.
831 /// The Bluetooth must be enabled.
833 /// <param name="packetType">The packet type.</param>
834 /// <param name="serviceSolicitationUuid"> The service solicitation UUID to add to advertise data.</param>
835 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
836 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
837 /// or when the add advertising service solicitation UUID procedure fails.</exception>
838 /// <since_tizen> 3 </since_tizen>
839 public void AddAdvertisingServiceSolicitationUuid(BluetoothLePacketType packetType,
840 string serviceSolicitationUuid)
842 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
844 int ret = Interop.Bluetooth.AddAdvertisingServiceSolicitationUuid(GetHandle(), packetType,
845 serviceSolicitationUuid);
846 if (ret != (int)BluetoothError.None) {
847 Log.Error (Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
848 BluetoothErrorFactory.ThrowBluetoothException(ret);
853 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
858 /// Adds a service data to the advertise or the scan response data.
859 /// The maximum advertised or responded data size is 31 bytes
860 /// including data type and system wide data.
863 /// The Bluetooth must be enabled.
865 /// <param name="packetType">The packet type.</param>
866 /// <param name="data"> The service data to be added to advertising.</param>
867 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
868 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
869 /// or when the add advertising data procedure fails.</exception>
870 /// <since_tizen> 3 </since_tizen>
871 public void AddAdvertisingServiceData(BluetoothLePacketType packetType, BluetoothServiceData data)
873 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
875 IntPtr serviceDataPtr;
876 serviceDataPtr = Marshal.AllocHGlobal(data.DataLength);
877 Marshal.Copy(data.Data, 0, serviceDataPtr, data.DataLength);
879 for (int i = 0; i < data.DataLength; i++)
880 Log.Error (Globals.LogTag, " service data is " + data.Data [i]);
881 int ret = Interop.Bluetooth.AddAdvertisingServiceData(GetHandle(), packetType,
882 data.Uuid, serviceDataPtr, data.DataLength);
883 if (ret != (int)BluetoothError.None)
885 Log.Error(Globals.LogTag, "Failed to add service data to advertising data- " + (BluetoothError)ret);
886 BluetoothErrorFactory.ThrowBluetoothException(ret);
891 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
896 /// Adds the manufacturer specific data to the advertise or the scan response data.
897 /// Please refer to the adopted Bluetooth specification for the the appearance.
900 /// The Bluetooth must be enabled.
902 /// <param name="packetType">The packet type.</param>
903 /// <param name="manufacturerData"> The manufacturer specific data.</param>
904 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
905 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
906 /// or when the add advertising manufacturer data procedure fails.</exception>
907 /// <since_tizen> 3 </since_tizen>
908 public void AddAdvertisingManufacturerData(BluetoothLePacketType packetType,
909 ManufacturerData manufacturerData)
911 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
914 manufDataPtr = Marshal.AllocHGlobal(manufacturerData.DataLength);
915 Marshal.Copy(manufacturerData.Data, 0, manufDataPtr, manufacturerData.DataLength);
917 int ret = Interop.Bluetooth.AddAdvertisingManufData(GetHandle(), packetType,
918 manufacturerData.Id, manufDataPtr, manufacturerData.DataLength);
919 if (ret != (int)BluetoothError.None)
921 Log.Error(Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
922 BluetoothErrorFactory.ThrowBluetoothException(ret);
927 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
932 /// Clears all data to be advertised or responded to the scan request from the LE scanning device.
935 /// The Bluetooth must be enabled.
937 /// <param name="packetType">The packet type to be cleared.</param>
938 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
939 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
940 /// or when the clear advertising data procedure fails.</exception>
941 internal void ClearAdvertisingData(BluetoothLePacketType packetType)
943 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
945 int ret = Interop.Bluetooth.ClearAdvertisingData (GetHandle (), packetType);
946 if (ret != (int)BluetoothError.None) {
947 Log.Error (Globals.LogTag, "Failed to Clear Advertising Data- " + (BluetoothError)ret);
952 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);