d2c8775b6bdaf1002ea2bfb396f33343de6c9657
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothLeAdapter.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Threading.Tasks;
19 using System.Collections.Generic;
20 using System.Collections.Specialized;
21 using System.Runtime.InteropServices;
22
23 namespace Tizen.Network.Bluetooth {
24
25     /// <summary>
26     /// This is the BluetoothLeAdvertiser class. It handles the LE advertising operation amd callback.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class BluetoothLeAdvertiser
30     {
31         private static readonly BluetoothLeAdvertiser _instance = new BluetoothLeAdvertiser();
32
33         internal static BluetoothLeAdvertiser Instance
34         {
35             get
36             {
37                 return _instance;
38             }
39         }
40
41         private BluetoothLeAdvertiser()
42         {
43         }
44
45         /// <summary>
46         /// This event is called when the LE advertising state changes.
47         /// </summary>
48         /// <since_tizen> 3 </since_tizen>
49         public event EventHandler<AdvertisingStateChangedEventArgs> AdvertisingStateChanged
50         {
51             add
52             {
53                 BluetoothLeImplAdapter.Instance.AdapterLeAdvertisingStateChanged += value;
54             }
55             remove
56             {
57                 BluetoothLeImplAdapter.Instance.AdapterLeAdvertisingStateChanged -= value;
58             }
59         }
60         /// <summary>
61         /// Starts advertising using the advertise data object.
62         /// </summary>
63         /// <remarks>
64         /// The Bluetooth must be enabled.
65         /// </remarks>
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)
71         {
72             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
73             {
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);
78                 }
79             }
80             else
81             {
82                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
83             }
84         }
85
86         /// <summary>
87         /// Stops the advertising.
88         /// </summary>
89         /// <remarks>
90         /// The Bluetooth must be enabled.
91         /// </remarks>
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)
97         {
98             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
99             {
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);
104                 }
105             }
106             else
107             {
108                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
109             }
110         }
111     }
112
113     /// <summary>
114     /// This is the BluetoothLeDevice class.
115     /// It handles the LE device operations like getting data from the scan result information.
116     /// </summary>
117     /// <since_tizen> 3 </since_tizen>
118     public class BluetoothLeDevice
119     {
120         //properties of Bluetoothlesacandata
121         private string _remoteAddress;
122         private BluetoothLeDeviceAddressType _addressType;
123         private int _rssi;
124         private byte[] _advDataValue;
125         private byte[] _scanDataValue;
126         private BluetoothLePacketType _packetType;
127         private BluetoothLeScanData _scanData;
128
129         /// <summary>
130         /// This event is called when the GATT client connects/disconnects with the server.
131         /// </summary>
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
135         {
136             add
137             {
138                 BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged += value;
139             }
140             remove
141             {
142                 BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged -= value;
143             }
144         }
145
146         internal BluetoothLeDevice(BluetoothLeScanData scanData)
147         {
148             _scanData = new BluetoothLeScanData ();
149             _scanData = scanData;
150
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;
158
159             Log.Info (Globals.LogTag, "AdvDataLength" + _scanData.AdvDataLength);
160             if (_scanData.AdvDataLength > 0)
161             {
162                 _advDataValue = new byte[_scanData.AdvDataLength];
163                 scanData.AdvData.CopyTo(_advDataValue, 0);
164             }
165
166             Log.Info(Globals.LogTag, "ScanDataLength" + _scanData.ScanDataLength);
167             //  Check length before copying
168             if (_scanData.ScanDataLength > 0)
169             {
170                 _scanDataValue = new byte[_scanData.ScanDataLength];
171                 scanData.ScanData.CopyTo(_scanDataValue, 0);
172             }
173         }
174
175         /// <summary>
176         /// BluetoothLeDevice destructor.
177         /// </summary>
178         ~BluetoothLeDevice()
179         {
180             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
181             {
182                 BluetoothLeImplAdapter.Instance.FreeServiceDataList();
183             }
184         }
185
186         /// <summary>
187         /// The remote address.
188         /// </summary>
189         /// <since_tizen> 3 </since_tizen>
190         public string RemoteAddress
191         {
192             get
193             {
194                 return _remoteAddress;
195             }
196         }
197
198         /// <summary>
199         /// The type of the address.
200         /// </summary>
201         /// <since_tizen> 3 </since_tizen>
202         public BluetoothLeDeviceAddressType AddressType
203         {
204             get
205             {
206                 return _addressType;
207             }
208         }
209
210         /// <summary>
211         /// The rssi value.
212         /// </summary>
213         /// <since_tizen> 3 </since_tizen>
214         public int Rssi
215         {
216             get
217             {
218                 return _rssi;
219             }
220         }
221
222         /// <summary>
223         /// The advertsing data information.
224         /// </summary>
225         /// <since_tizen> 3 </since_tizen>
226         public byte[] AdvertsingDataInformation
227         {
228             get
229             {
230                 return _advDataValue;
231             }
232         }
233
234         /// <summary>
235         /// The scan data information.
236         /// </summary>
237         /// <since_tizen> 3 </since_tizen>
238         public byte[] ScanDataInformation
239         {
240             get
241             {
242                 return _scanDataValue;
243             }
244         }
245
246         /// <summary>
247         /// The type of the packet.
248         /// </summary>
249         /// <since_tizen> 3 </since_tizen>
250         public BluetoothLePacketType PacketType
251         {
252             get
253             {
254                 return _packetType;
255             }
256             set
257             {
258                 _packetType = value;
259             }
260         }
261
262         /// <summary>
263         /// Gets the service UUIDs list from the LE scan result information.
264         /// </summary>
265         /// <value> Gets the list of the string service UUIDs.</value>
266         /// <remarks>
267         /// The Bluetooth must be enabled.
268         /// </remarks>
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
273         {
274             get
275             {
276                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
277                 {
278                     Log.Info(Globals.LogTag, "Retrieving Service uuid- ");
279                     return BluetoothLeImplAdapter.Instance.GetLeScanResultServiceUuids(_scanData, _packetType);
280                 }
281                 return null;
282             }
283         }
284
285         /// <summary>
286         /// Gets the device name from the LE scan result information.
287         /// </summary>
288         /// <value> Gets the device name.</value>
289         /// <remarks>
290         /// The Bluetooth must be enabled.
291         /// </remarks>
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
296         {
297             get
298             {
299                 Log.Info(Globals.LogTag, "Retrieving device name- ");
300                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
301                 {
302                     return BluetoothLeImplAdapter.Instance.GetLeScanResultDeviceName(_scanData, _packetType);
303                 }
304                 return null;
305             }
306         }
307         /// <summary>
308         /// Gets the transmission power level from the LE scan result information.
309         /// </summary>
310         /// <value> Gets the transmission power level in dB.</value>
311         /// <remarks>
312         /// The Bluetooth must be enabled.
313         /// </remarks>
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
318         {
319             get
320             {
321                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
322                 {
323                     return BluetoothLeImplAdapter.Instance.GetScanResultTxPowerLevel(_scanData, _packetType);
324                 }
325                 return -1;
326             }
327         }
328
329         /// <summary>
330         /// Gets the service solicitation UUID list from the scan result information.
331         /// </summary>
332         /// <value> Gets the list of the service solicitation UUIDs.</value>
333         /// <remarks>
334         /// The Bluetooth must be enabled.
335         /// </remarks>
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
340         {
341             get
342             {
343                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
344                 {
345                     return BluetoothLeImplAdapter.Instance.GetScanResultSvcSolicitationUuids(_scanData, _packetType);
346                 }
347                 return null;
348             }
349         }
350         /// <summary>
351         /// Gets the manufacturer data from the scan result information.
352         /// </summary>
353         /// <value> Gets the appearance value.</value>
354         /// <remarks>
355         /// The Bluetooth must be enabled.
356         /// </remarks>
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
361         {
362             get
363             {
364                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
365                 {
366                     return BluetoothLeImplAdapter.Instance.GetScanResultAppearance(_scanData, _packetType);
367                 }
368                 return -1;
369             }
370         }
371         /// <summary>
372         /// Gets the manufacturer data from the scan result information.
373         /// </summary>
374         /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
375         /// <remarks>
376         /// The Bluetooth must be enabled.
377         /// </remarks>
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
382         {
383             get
384             {
385                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
386                 {
387                     return BluetoothLeImplAdapter.Instance.GetScanResultManufacturerData(_scanData, _packetType);
388                 }
389                 return null;
390             }
391         }
392
393         /// <summary>
394         /// Gets the service data list from the scan result information.
395         /// </summary>
396         /// <remarks>
397         /// The Bluetooth must be enabled.
398         /// </remarks>
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()
404         {
405             int serviceCount = 0;
406             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
407             {
408                 return BluetoothLeImplAdapter.Instance.GetScanResultServiceDataList(_scanData,
409                                             _packetType, out serviceCount);
410             }
411             return null;
412         }
413
414
415         /// <summary>
416         /// Creates a GATT connection with the remote device.
417         /// </summary>
418         /// <remarks>
419         /// The Bluetooth must be enabled.
420         /// </remarks>
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)
429         {
430             BluetoothGattClient client = null;
431             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
432             {
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);
436                 }
437                 else
438                 {
439                     client = BluetoothGattClient.CreateClient(_remoteAddress);
440                 }
441             }
442             else
443             {
444                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
445             }
446             return client;
447         }
448
449         /// <summary>
450         /// Disconnects a GATT connection with the remote device.
451         /// </summary>
452         /// <remarks>
453         /// The Bluetooth must be enabled.
454         /// </remarks>
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()
461         {
462             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
463             {
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);
467                 }
468             }
469             else
470             {
471                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
472             }
473         }
474     }
475
476     /// <summary>
477     /// Bluetooth LE advertise data. Handles the data advertising.
478     /// </summary>
479     /// <since_tizen> 3 </since_tizen>
480     public class BluetoothLeAdvertiseData:IDisposable
481     {
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;
489
490         /// <summary>
491         /// The default constructor initializes an object of the BluetoothLeAdvertiseData.
492         /// </summary>
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()
498         {
499             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
500             {
501                 Log.Debug(Globals.LogTag, " Creating LeAdvertiser()");
502                 int ret = Interop.Bluetooth.CreateAdvertiser(out _handle);
503                 if (ret != (int)BluetoothError.None)
504                 {
505                     Log.Error(Globals.LogTag, "Failed to create advertiser object- " + (BluetoothError)ret);
506                     BluetoothErrorFactory.ThrowBluetoothException(ret);
507                 }
508             }
509             else
510             {
511                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
512             }
513         }
514
515         /// <summary>
516         /// BluetoothLeAdvertiseData destructor.
517         /// </summary>
518         ~BluetoothLeAdvertiseData()
519         {
520             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
521             {
522                 //clean-up
523                 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeAdvertisingPacket);
524                 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeScanResponsePacket);
525                 BluetoothLeImplAdapter.Instance.DestroyAdvertiser (_handle);
526             }
527             Dispose(false);
528         }
529
530         internal IntPtr GetHandle()
531         {
532             return _handle;
533         }
534
535         /// <summary>
536         /// The advertising mode to control the advertising power and latency.
537         /// </summary>
538         /// <remarks>
539         /// The Bluetooth must be enabled.
540         /// </remarks>
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
546         {
547             get
548             {
549                 return _mode;
550             }
551             set
552             {
553                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
554                 {
555                     _mode = value;
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);
560                     }
561                 }
562             }
563         }
564
565         /// <summary>
566         /// The advertising connectable type.
567         /// </summary>
568         /// <remarks>
569         /// The Bluetooth must be enabled.
570         /// </remarks>
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
576         {
577             get
578             {
579                 return _advertisingConnectable;
580             }
581             set
582             {
583                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
584                 {
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);
590                     }
591                 }
592             }
593         }
594
595         /// <summary>
596         /// Dispose
597         /// </summary>
598         /// <since_tizen> 3 </since_tizen>
599         public void Dispose()
600         {
601             Dispose(true);
602             GC.SuppressFinalize(this);
603         }
604
605         private void Dispose(bool disposing)
606         {
607             //todo...
608         }
609
610         /// <summary>
611         /// The type of the packet.
612         /// </summary>
613         /// <since_tizen> 3 </since_tizen>
614         public BluetoothLePacketType PacketType
615         {
616             get
617             {
618                 return _packetType;
619             }
620             set
621             {
622                 _packetType = value;
623             }
624         }
625         /// <summary>
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.
628         /// </summary>
629         /// <remarks>
630         /// The Bluetooth must be enabled.
631         /// </remarks>
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
637         {
638             get
639             {
640                 return _appearance;
641             }
642             set
643             {
644                 _appearance = value;
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);
650                     }
651                 }
652             }
653         }
654         /// <summary>
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.
657         /// </summary>
658         /// <remarks>
659         /// The Bluetooth must be enabled.
660         /// </remarks>
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
666         {
667             get
668             {
669                 return _includeDeviceName;
670             }
671             set
672             {
673                 _includeDeviceName = value;
674                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
675                 {
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);
680                     }
681                 }
682             }
683         }
684
685
686         /// <summary>
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.
689         /// </summary>
690         /// <remarks>
691         /// The Bluetooth must be enabled.
692         /// </remarks>
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
698         {
699             get
700             {
701                 return _includePowerLevel;
702             }
703             set
704             {
705                 _includePowerLevel = value;
706                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
707                 {
708                     int ret = Interop.Bluetooth.SetAdvertisingTxPowerLevel(GetHandle(), _packetType, _includePowerLevel);
709                     if (ret != (int)BluetoothError.None)
710                     {
711                         Log.Error(Globals.LogTag, "Failed to add advertising service solicitation uuid- " + (BluetoothError)ret);
712                     }
713                 }
714             }
715         }
716         /// <summary>
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.
720         /// </summary>
721         /// <remarks>
722         /// The Bluetooth must be enabled.
723         /// </remarks>
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)
731         {
732             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
733             {
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);
738                 }
739             }
740             else
741             {
742                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
743             }
744         }
745
746         /// <summary>
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.
750         /// </summary>
751         /// <remarks>
752         /// The Bluetooth must be enabled.
753         /// </remarks>
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)
762         {
763             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
764             {
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);
770                 }
771             }
772             else
773             {
774                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
775             }
776         }
777
778         /// <summary>
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.
782         /// </summary>
783         /// <remarks>
784         /// The Bluetooth must be enabled.
785         /// </remarks>
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)
793         {
794             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
795             {
796                 IntPtr serviceDataPtr;
797                 serviceDataPtr = Marshal.AllocHGlobal(data.DataLength);
798                 Marshal.Copy(data.Data, 0, serviceDataPtr, data.DataLength);
799
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)
805                 {
806                     Log.Error(Globals.LogTag, "Failed to add service data to advertising data- " + (BluetoothError)ret);
807                     BluetoothErrorFactory.ThrowBluetoothException(ret);
808                 }
809             }
810             else
811             {
812                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
813             }
814         }
815
816         /// <summary>
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.
819         /// </summary>
820         /// <remarks>
821         /// The Bluetooth must be enabled.
822         /// </remarks>
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)
831         {
832             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
833             {
834                 IntPtr manufDataPtr;
835                 manufDataPtr = Marshal.AllocHGlobal(manufacturerData.DataLength);
836                 Marshal.Copy(manufacturerData.Data, 0, manufDataPtr, manufacturerData.DataLength);
837
838                 int ret = Interop.Bluetooth.AddAdvertisingManufData(GetHandle(), packetType,
839                     manufacturerData.Id, manufDataPtr, manufacturerData.DataLength);
840                 if (ret != (int)BluetoothError.None)
841                 {
842                     Log.Error(Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
843                     BluetoothErrorFactory.ThrowBluetoothException(ret);
844                 }
845             }
846             else
847             {
848                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
849             }
850         }
851
852         /// <summary>
853         /// Clears all data to be advertised or responded to the scan request from the LE scanning device.
854         /// </summary>
855         /// <remarks>
856         /// The Bluetooth must be enabled.
857         /// </remarks>
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)
863         {
864             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
865             {
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);
869                 }
870             }
871             else
872             {
873                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
874             }
875         }
876     }
877 }