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 information.
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 public BluetoothLePacketType PacketType
263 /// Gets the service UUIDs list from the LE scan result information.
265 /// <value> Gets the list of the string service UUIDs.</value>
267 /// The Bluetooth must be enabled.
269 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
270 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
271 /// <since_tizen> 3 </since_tizen>
272 public IEnumerable<string> ServiceUuid
276 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
278 Log.Info(Globals.LogTag, "Retrieving Service uuid- ");
279 return BluetoothLeImplAdapter.Instance.GetLeScanResultServiceUuids(_scanData, _packetType);
286 /// Gets the device name from the LE scan result information.
288 /// <value> Gets the device name.</value>
290 /// The Bluetooth must be enabled.
292 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
293 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
294 /// <since_tizen> 3 </since_tizen>
295 public string DeviceName
299 Log.Info(Globals.LogTag, "Retrieving device name- ");
300 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
302 return BluetoothLeImplAdapter.Instance.GetLeScanResultDeviceName(_scanData, _packetType);
308 /// Gets the transmission power level from the LE scan result information.
310 /// <value> Gets the transmission power level in dB.</value>
312 /// The Bluetooth must be enabled.
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> 3 </since_tizen>
317 public int TxPowerLevel
321 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
323 return BluetoothLeImplAdapter.Instance.GetScanResultTxPowerLevel(_scanData, _packetType);
330 /// Gets the service solicitation UUID list from the scan result information.
332 /// <value> Gets the list of the service solicitation UUIDs.</value>
334 /// The Bluetooth must be enabled.
336 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
337 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
338 /// <since_tizen> 3 </since_tizen>
339 public IEnumerable<string> ServiceSolictationUuid
343 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
345 return BluetoothLeImplAdapter.Instance.GetScanResultSvcSolicitationUuids(_scanData, _packetType);
351 /// Gets the manufacturer data from the scan result information.
353 /// <value> Gets the appearance value.</value>
355 /// The Bluetooth must be enabled.
357 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
358 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
359 /// <since_tizen> 3 </since_tizen>
360 public int Appearance
364 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
366 return BluetoothLeImplAdapter.Instance.GetScanResultAppearance(_scanData, _packetType);
372 /// Gets the manufacturer data from the scan result information.
374 /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
376 /// The Bluetooth must be enabled.
378 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
379 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>///
380 /// <since_tizen> 3 </since_tizen>
381 public ManufacturerData ManufacturerData
385 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
387 return BluetoothLeImplAdapter.Instance.GetScanResultManufacturerData(_scanData, _packetType);
394 /// Gets the service data list from the scan result information.
397 /// The Bluetooth must be enabled.
399 /// <returns> Returns the service data list.</returns>
400 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
401 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
402 /// <since_tizen> 3 </since_tizen>
403 public IEnumerable<BluetoothLeServiceData> GetServiceDataList()
405 int serviceCount = 0;
406 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
408 return BluetoothLeImplAdapter.Instance.GetScanResultServiceDataList(_scanData,
409 _packetType, out serviceCount);
416 /// Creates a GATT connection with the remote device.
419 /// The Bluetooth must be enabled.
421 /// <param name="autoConnect"> The auto connect flag.</param>
422 /// <returns>client instance</returns>
423 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
424 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
425 /// or when the gatt connection attempt to remote device fails.</exception>
426 /// <since_tizen> 3 </since_tizen>
427 [Obsolete("Deprecated since API level 6. Please use CreateClient() and ConnectAsync() method on BluetoothGattClient.")]
428 public BluetoothGattClient GattConnect(bool autoConnect)
430 BluetoothGattClient client = null;
431 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
433 int ret = BluetoothLeImplAdapter.Instance.GattConnect (_remoteAddress, autoConnect);
434 if (ret != (int)BluetoothError.None) {
435 Log.Error (Globals.LogTag, "Failed to create GATT Connection with remote device- " + (BluetoothError)ret);
439 client = BluetoothGattClient.CreateClient(_remoteAddress);
444 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
450 /// Disconnects a GATT connection with the remote device.
453 /// The Bluetooth must be enabled.
455 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
456 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
457 /// or when the GATT disconnection attempt to remote device fails.</exception>
458 /// <since_tizen> 3 </since_tizen>
459 [Obsolete("Deprecated since API level 6. Please use DisconnectAsync() method on BluetoothGattClient.")]
460 public void GattDisconnect()
462 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
464 int ret = BluetoothLeImplAdapter.Instance.GattDisconnect (_remoteAddress);
465 if (ret != (int)BluetoothError.None) {
466 Log.Error (Globals.LogTag, "Failed to disconnect GATT connection with remote device- " + (BluetoothError)ret);
471 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
477 /// Bluetooth LE advertise data. Handles the data advertising.
479 /// <since_tizen> 3 </since_tizen>
480 public class BluetoothLeAdvertiseData:IDisposable
482 private IntPtr _handle = IntPtr.Zero;
483 private BluetoothLeAdvertisingMode _mode;
484 private bool _advertisingConnectable;
485 private BluetoothLePacketType _packetType;
486 private int _appearance;
487 private bool _includePowerLevel;
488 private bool _includeDeviceName;
491 /// The default constructor initializes an object of the BluetoothLeAdvertiseData.
493 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
494 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
495 /// or when create advertiser fails.</exception>
496 /// <since_tizen> 3 </since_tizen>
497 public BluetoothLeAdvertiseData()
499 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
501 Log.Debug(Globals.LogTag, " Creating LeAdvertiser()");
502 int ret = Interop.Bluetooth.CreateAdvertiser(out _handle);
503 if (ret != (int)BluetoothError.None)
505 Log.Error(Globals.LogTag, "Failed to create advertiser object- " + (BluetoothError)ret);
506 BluetoothErrorFactory.ThrowBluetoothException(ret);
511 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
516 /// BluetoothLeAdvertiseData destructor.
518 ~BluetoothLeAdvertiseData()
520 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
523 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeAdvertisingPacket);
524 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeScanResponsePacket);
525 BluetoothLeImplAdapter.Instance.DestroyAdvertiser (_handle);
530 internal IntPtr GetHandle()
536 /// The advertising mode to control the advertising power and latency.
539 /// The Bluetooth must be enabled.
541 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
542 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
543 /// or when the set advertising mode fails.</exception>
544 /// <since_tizen> 3 </since_tizen>
545 public BluetoothLeAdvertisingMode AdvertisingMode
553 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
556 int ret = Interop.Bluetooth.SetAdvertisingMode (GetHandle (), _mode);
557 if (ret != (int)BluetoothError.None) {
558 Log.Error (Globals.LogTag, "Failed to set advertising mode- " + (BluetoothError)ret);
559 BluetoothErrorFactory.ThrowBluetoothException (ret);
566 /// The advertising connectable type.
569 /// The Bluetooth must be enabled.
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 the set advertising connectable mode fails.</exception>
574 /// <since_tizen> 3 </since_tizen>
575 public bool AdvertisingConnectable
579 return _advertisingConnectable;
583 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
585 _advertisingConnectable = value;
586 int ret = Interop.Bluetooth.SetAdvertisingConnectable (GetHandle (), _advertisingConnectable);
587 if (ret != (int)BluetoothError.None) {
588 Log.Error (Globals.LogTag, "Failed to set advertising connectable value- " + (BluetoothError)ret);
589 BluetoothErrorFactory.ThrowBluetoothException (ret);
598 /// <since_tizen> 3 </since_tizen>
599 public void Dispose()
602 GC.SuppressFinalize(this);
605 private void Dispose(bool disposing)
611 /// The type of the packet.
613 /// <since_tizen> 3 </since_tizen>
614 public BluetoothLePacketType PacketType
626 /// Sets the external appearance of this device to the advertise or the scan response data.
627 /// Please refer to the adopted Bluetooth specification for the appearance.
630 /// The Bluetooth must be enabled.
632 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
633 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
634 /// or when the set appearance fails.</exception>
635 /// <since_tizen> 3 </since_tizen>
636 public int Appearance
645 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) {
646 int ret = Interop.Bluetooth.SetAdvertisingAppearance (GetHandle (), _packetType, _appearance);
647 if (ret != (int)BluetoothError.None) {
648 Log.Error (Globals.LogTag, "Failed to add appearance value to advertising data- " + (BluetoothError)ret);
649 BluetoothErrorFactory.ThrowBluetoothException(ret);
655 /// Sets whether the device name has to be included in the advertise or the scan response data.
656 /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
659 /// The Bluetooth must be enabled.
661 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
662 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
663 /// or when the set advertising device name fails.</exception>
664 /// <since_tizen> 3 </since_tizen>
665 public bool IncludeDeviceName
669 return _includeDeviceName;
673 _includeDeviceName = value;
674 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
676 int ret = Interop.Bluetooth.SetAdvertisingDeviceName(GetHandle(), _packetType, _includeDeviceName);
677 if (ret != (int)BluetoothError.None) {
678 Log.Error (Globals.LogTag, "Failed to add device name to advertising data- " + (BluetoothError)ret);
679 BluetoothErrorFactory.ThrowBluetoothException(ret);
687 /// Sets whether the transmission power level should be included in the advertise or the scan response data.
688 /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
691 /// The Bluetooth must be enabled.
693 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
694 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
695 /// or when the set advertising TC power level fails.</exception>
696 /// <since_tizen> 3 </since_tizen>
697 public bool IncludeTxPowerLevel
701 return _includePowerLevel;
705 _includePowerLevel = value;
706 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
708 int ret = Interop.Bluetooth.SetAdvertisingTxPowerLevel(GetHandle(), _packetType, _includePowerLevel);
709 if (ret != (int)BluetoothError.None)
711 Log.Error(Globals.LogTag, "Failed to add advertising service solicitation uuid- " + (BluetoothError)ret);
717 /// Adds a service UUID to the advertise or the scan response data.
718 /// The maximum advertised or responded data size is 31 bytes
719 /// including the data type and the system wide data.
722 /// The Bluetooth must be enabled.
724 /// <param name="packetType">The packet type.</param>
725 /// <param name="serviceUuid"> The service UUID to add to advertise data.</param>
726 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
727 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
728 /// or when the add advertising service UUID procedure fails.</exception>
729 /// <since_tizen> 3 </since_tizen>
730 public void AddAdvertisingServiceUuid(BluetoothLePacketType packetType, string serviceUuid)
732 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
734 int ret = Interop.Bluetooth.AddAdvertisingServiceUuid (GetHandle (), packetType, serviceUuid);
735 if (ret != (int)BluetoothError.None) {
736 Log.Error (Globals.LogTag, "Failed to add service uuid to advertising data- " + (BluetoothError)ret);
737 BluetoothErrorFactory.ThrowBluetoothException (ret);
742 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
747 /// Adds a service solicitation UUID to advertise or scan the response data.
748 /// The maximum advertised or responded data size is 31 bytes
749 /// including the data type and the system wide data.
752 /// The Bluetooth must be enabled.
754 /// <param name="packetType">The packet type.</param>
755 /// <param name="serviceSolicitationUuid"> The service solicitation UUID to add to advertise data.</param>
756 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
757 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
758 /// or when the add advertising service solicitation UUID procedure fails.</exception>
759 /// <since_tizen> 3 </since_tizen>
760 public void AddAdvertisingServiceSolicitationUuid(BluetoothLePacketType packetType,
761 string serviceSolicitationUuid)
763 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
765 int ret = Interop.Bluetooth.AddAdvertisingServiceSolicitationUuid(GetHandle(), packetType,
766 serviceSolicitationUuid);
767 if (ret != (int)BluetoothError.None) {
768 Log.Error (Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
769 BluetoothErrorFactory.ThrowBluetoothException(ret);
774 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
779 /// Adds a service data to the advertise or the scan response data.
780 /// The maximum advertised or responded data size is 31 bytes
781 /// including data type and system wide data.
784 /// The Bluetooth must be enabled.
786 /// <param name="packetType">The packet type.</param>
787 /// <param name="data"> The service data to be added to advertising.</param>
788 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
789 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
790 /// or when the add advertising data procedure fails.</exception>
791 /// <since_tizen> 3 </since_tizen>
792 public void AddAdvertisingServiceData(BluetoothLePacketType packetType, BluetoothServiceData data)
794 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
796 IntPtr serviceDataPtr;
797 serviceDataPtr = Marshal.AllocHGlobal(data.DataLength);
798 Marshal.Copy(data.Data, 0, serviceDataPtr, data.DataLength);
800 for (int i = 0; i < data.DataLength; i++)
801 Log.Error (Globals.LogTag, " service data is " + data.Data [i]);
802 int ret = Interop.Bluetooth.AddAdvertisingServiceData(GetHandle(), packetType,
803 data.Uuid, serviceDataPtr, data.DataLength);
804 if (ret != (int)BluetoothError.None)
806 Log.Error(Globals.LogTag, "Failed to add service data to advertising data- " + (BluetoothError)ret);
807 BluetoothErrorFactory.ThrowBluetoothException(ret);
812 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
817 /// Adds the manufacturer specific data to the advertise or the scan response data.
818 /// Please refer to the adopted Bluetooth specification for the the appearance.
821 /// The Bluetooth must be enabled.
823 /// <param name="packetType">The packet type.</param>
824 /// <param name="manufacturerData"> The manufacturer specific data.</param>
825 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
826 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
827 /// or when the add advertising manufacturer data procedure fails.</exception>
828 /// <since_tizen> 3 </since_tizen>
829 public void AddAdvertisingManufacturerData(BluetoothLePacketType packetType,
830 ManufacturerData manufacturerData)
832 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
835 manufDataPtr = Marshal.AllocHGlobal(manufacturerData.DataLength);
836 Marshal.Copy(manufacturerData.Data, 0, manufDataPtr, manufacturerData.DataLength);
838 int ret = Interop.Bluetooth.AddAdvertisingManufData(GetHandle(), packetType,
839 manufacturerData.Id, manufDataPtr, manufacturerData.DataLength);
840 if (ret != (int)BluetoothError.None)
842 Log.Error(Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
843 BluetoothErrorFactory.ThrowBluetoothException(ret);
848 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
853 /// Clears all data to be advertised or responded to the scan request from the LE scanning device.
856 /// The Bluetooth must be enabled.
858 /// <param name="packetType">The packet type to be cleared.</param>
859 /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
860 /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
861 /// or when the clear advertising data procedure fails.</exception>
862 internal void ClearAdvertisingData(BluetoothLePacketType packetType)
864 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
866 int ret = Interop.Bluetooth.ClearAdvertisingData (GetHandle (), packetType);
867 if (ret != (int)BluetoothError.None) {
868 Log.Error (Globals.LogTag, "Failed to Clear Advertising Data- " + (BluetoothError)ret);
873 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);