Setting since_tizen 3/4 on Tizen.NET API
[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         public event EventHandler<GattConnectionStateChangedEventArgs> GattConnectionStateChanged
134         {
135             add
136             {
137                 BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged += value;
138             }
139             remove
140             {
141                 BluetoothLeImplAdapter.Instance.LeGattConnectionStateChanged -= value;
142             }
143         }
144
145         internal BluetoothLeDevice(BluetoothLeScanData scanData)
146         {
147             _scanData = new BluetoothLeScanData ();
148             _scanData = scanData;
149
150             Log.Info (Globals.LogTag, "Rssi" + _scanData.Rssi);
151             _rssi = scanData.Rssi;
152             Log.Info (Globals.LogTag, "RemoteAddress" + _scanData.RemoteAddress);
153             if (scanData.RemoteAddress != null)
154                 _remoteAddress = scanData.RemoteAddress;
155             Log.Info (Globals.LogTag, "AddressType" + _scanData.AddressType);
156             _addressType = scanData.AddressType;
157
158             Log.Info (Globals.LogTag, "AdvDataLength" + _scanData.AdvDataLength);
159             if (_scanData.AdvDataLength > 0)
160             {
161                 _advDataValue = new byte[_scanData.AdvDataLength];
162                 scanData.AdvData.CopyTo(_advDataValue, 0);
163             }
164
165             Log.Info(Globals.LogTag, "ScanDataLength" + _scanData.ScanDataLength);
166             //  Check length before copying
167             if (_scanData.ScanDataLength > 0)
168             {
169                 _scanDataValue = new byte[_scanData.ScanDataLength];
170                 scanData.ScanData.CopyTo(_scanDataValue, 0);
171             }
172         }
173
174         /// <summary>
175         /// BluetoothLeDevice destructor.
176         /// </summary>
177         ~BluetoothLeDevice()
178         {
179             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
180             {
181                 BluetoothLeImplAdapter.Instance.FreeServiceDataList();
182             }
183         }
184
185         /// <summary>
186         /// The remote address.
187         /// </summary>
188         /// <since_tizen> 3 </since_tizen>
189         public string RemoteAddress
190         {
191             get
192             {
193                 return _remoteAddress;
194             }
195         }
196
197         /// <summary>
198         /// The type of the address.
199         /// </summary>
200         /// <since_tizen> 3 </since_tizen>
201         public BluetoothLeDeviceAddressType AddressType
202         {
203             get
204             {
205                 return _addressType;
206             }
207         }
208
209         /// <summary>
210         /// The rssi value.
211         /// </summary>
212         /// <since_tizen> 3 </since_tizen>
213         public int Rssi
214         {
215             get
216             {
217                 return _rssi;
218             }
219         }
220
221         /// <summary>
222         /// The advertsing data information.
223         /// </summary>
224         /// <since_tizen> 3 </since_tizen>
225         public byte[] AdvertsingDataInformation
226         {
227             get
228             {
229                 return _advDataValue;
230             }
231         }
232
233         /// <summary>
234         /// The scan data information.
235         /// </summary>
236         /// <since_tizen> 3 </since_tizen>
237         public byte[] ScanDataInformation
238         {
239             get
240             {
241                 return _scanDataValue;
242             }
243         }
244
245         /// <summary>
246         /// The type of the packet.
247         /// </summary>
248         /// <since_tizen> 3 </since_tizen>
249         public BluetoothLePacketType PacketType
250         {
251             get
252             {
253                 return _packetType;
254             }
255             set
256             {
257                 _packetType = value;
258             }
259         }
260
261         /// <summary>
262         /// Gets the service UUIDs list from the LE scan result information.
263         /// </summary>
264         /// <value> Gets the list of the string service UUIDs.</value>
265         /// <remarks>
266         /// The Bluetooth must be enabled.
267         /// </remarks>
268         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
269         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
270         /// <since_tizen> 3 </since_tizen>
271         public IEnumerable<string> ServiceUuid
272         {
273             get
274             {
275                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
276                 {
277                     Log.Info(Globals.LogTag, "Retrieving Service uuid- ");
278                     return BluetoothLeImplAdapter.Instance.GetLeScanResultServiceUuids(_scanData, _packetType);
279                 }
280                 return null;
281             }
282         }
283
284         /// <summary>
285         /// Gets the device name from the LE scan result information.
286         /// </summary>
287         /// <value> Gets the device name.</value>
288         /// <remarks>
289         /// The Bluetooth must be enabled.
290         /// </remarks>
291         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
292         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
293         /// <since_tizen> 3 </since_tizen>
294         public string DeviceName
295         {
296             get
297             {
298                 Log.Info(Globals.LogTag, "Retrieving device name- ");
299                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
300                 {
301                     return BluetoothLeImplAdapter.Instance.GetLeScanResultDeviceName(_scanData, _packetType);
302                 }
303                 return null;
304             }
305         }
306         /// <summary>
307         /// Gets the transmission power level from the LE scan result information.
308         /// </summary>
309         /// <value> Gets the transmission power level in dB.</value>
310         /// <remarks>
311         /// The Bluetooth must be enabled.
312         /// </remarks>
313         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
314         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
315         /// <since_tizen> 3 </since_tizen>
316         public int TxPowerLevel
317         {
318             get
319             {
320                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
321                 {
322                     return BluetoothLeImplAdapter.Instance.GetScanResultTxPowerLevel(_scanData, _packetType);
323                 }
324                 return -1;
325             }
326         }
327
328         /// <summary>
329         /// Gets the service solicitation UUID list from the scan result information.
330         /// </summary>
331         /// <value> Gets the list of the service solicitation UUIDs.</value>
332         /// <remarks>
333         /// The Bluetooth must be enabled.
334         /// </remarks>
335         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
336         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
337         /// <since_tizen> 3 </since_tizen>
338         public IEnumerable<string> ServiceSolictationUuid
339         {
340             get
341             {
342                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
343                 {
344                     return BluetoothLeImplAdapter.Instance.GetScanResultSvcSolicitationUuids(_scanData, _packetType);
345                 }
346                 return null;
347             }
348         }
349         /// <summary>
350         /// Gets the manufacturer data from the scan result information.
351         /// </summary>
352         /// <value> Gets the appearance value.</value>
353         /// <remarks>
354         /// The Bluetooth must be enabled.
355         /// </remarks>
356         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
357         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
358         /// <since_tizen> 3 </since_tizen>
359         public int Appearance
360         {
361             get
362             {
363                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
364                 {
365                     return BluetoothLeImplAdapter.Instance.GetScanResultAppearance(_scanData, _packetType);
366                 }
367                 return -1;
368             }
369         }
370         /// <summary>
371         /// Gets the manufacturer data from the scan result information.
372         /// </summary>
373         /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
374         /// <remarks>
375         /// The Bluetooth must be enabled.
376         /// </remarks>
377         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
378         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>/// 
379         /// <since_tizen> 3 </since_tizen>
380         public ManufacturerData ManufacturerData
381         {
382             get
383             {
384                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
385                 {
386                     return BluetoothLeImplAdapter.Instance.GetScanResultManufacturerData(_scanData, _packetType);
387                 }
388                 return null;
389             }
390         }
391
392         /// <summary>
393         /// Gets the service data list from the scan result information.
394         /// </summary>
395         /// <remarks>
396         /// The Bluetooth must be enabled.
397         /// </remarks>
398         /// <returns> Returns the service data list.</returns>
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         public IEnumerable<BluetoothLeServiceData> GetServiceDataList()
403         {
404             int serviceCount = 0;
405             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
406             {
407                 return BluetoothLeImplAdapter.Instance.GetScanResultServiceDataList(_scanData,
408                                             _packetType, out serviceCount);
409             }
410             return null;
411         }
412
413
414         /// <summary>
415         /// Creates a GATT connection with the remote device.
416         /// </summary>
417         /// <remarks>
418         /// The Bluetooth must be enabled.
419         /// </remarks>
420         /// <param name="autoConnect"> The auto connect flag.</param>
421         /// <returns>client instance</returns>
422         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
423         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
424         /// or when the gatt connection attempt to remote device fails.</exception>
425         /// <since_tizen> 3 </since_tizen>
426         public BluetoothGattClient GattConnect(bool autoConnect)
427         {
428             BluetoothGattClient client = null;
429             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
430             {
431                 int ret = BluetoothLeImplAdapter.Instance.GattConnect (_remoteAddress, autoConnect);
432                 if (ret != (int)BluetoothError.None) {
433                     Log.Error (Globals.LogTag, "Failed to create GATT Connection with remote device- " + (BluetoothError)ret);
434                 }
435                 else
436                 {
437                     client = BluetoothGattClient.CreateClient(_remoteAddress);
438                 }
439             }
440             else
441             {
442                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
443             }
444             return client;
445         }
446
447         /// <summary>
448         /// Disconnects a GATT connection with the remote device.
449         /// </summary>
450         /// <remarks>
451         /// The Bluetooth must be enabled.
452         /// </remarks>
453         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
454         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
455         /// or when the GATT disconnection attempt to remote device fails.</exception>
456         /// <since_tizen> 3 </since_tizen>
457         public void GattDisconnect()
458         {
459             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
460             {
461                 int ret = BluetoothLeImplAdapter.Instance.GattDisconnect (_remoteAddress);
462                 if (ret != (int)BluetoothError.None) {
463                     Log.Error (Globals.LogTag, "Failed to disconnect GATT connection with remote device- " + (BluetoothError)ret);
464                 }
465             }
466             else
467             {
468                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
469             }
470         }
471     }
472
473     /// <summary>
474     /// Bluetooth LE advertise data. Handles the data advertising.
475     /// </summary>
476     /// <since_tizen> 3 </since_tizen>
477     public class BluetoothLeAdvertiseData:IDisposable
478     {
479         private IntPtr _handle = IntPtr.Zero;
480         private BluetoothLeAdvertisingMode _mode;
481         private bool _advertisingConnectable;
482         private BluetoothLePacketType _packetType;
483         private int _appearance;
484         private bool _includePowerLevel;
485         private bool _includeDeviceName;
486
487         /// <summary>
488         /// The default constructor initializes an object of the BluetoothLeAdvertiseData.
489         /// </summary>
490         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
491         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
492         /// or when create advertiser fails.</exception>
493         /// <since_tizen> 3 </since_tizen>
494         public BluetoothLeAdvertiseData()
495         {
496             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
497             {
498                 Log.Debug(Globals.LogTag, " Creating LeAdvertiser()");
499                 int ret = Interop.Bluetooth.CreateAdvertiser(out _handle);
500                 if (ret != (int)BluetoothError.None)
501                 {
502                     Log.Error(Globals.LogTag, "Failed to create advertiser object- " + (BluetoothError)ret);
503                     BluetoothErrorFactory.ThrowBluetoothException(ret);
504                 }
505             }
506             else
507             {
508                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
509             }
510         }
511
512         /// <summary>
513         /// BluetoothLeAdvertiseData destructor.
514         /// </summary>
515         ~BluetoothLeAdvertiseData()
516         {
517             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
518             {
519                 //clean-up
520                 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeAdvertisingPacket);
521                 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeScanResponsePacket);
522                 BluetoothLeImplAdapter.Instance.DestroyAdvertiser (_handle);
523             }
524             Dispose(false);
525         }
526
527         internal IntPtr GetHandle()
528         {
529             return _handle;
530         }
531
532         /// <summary>
533         /// The advertising mode to control the advertising power and latency.
534         /// </summary>
535         /// <remarks>
536         /// The Bluetooth must be enabled.
537         /// </remarks>
538         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
539         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
540         /// or when the set advertising mode fails.</exception>
541         /// <since_tizen> 3 </since_tizen>
542         public BluetoothLeAdvertisingMode AdvertisingMode
543         {
544             get
545             {
546                 return _mode;
547             }
548             set
549             {
550                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
551                 {
552                     _mode = value;
553                     int ret = Interop.Bluetooth.SetAdvertisingMode (GetHandle (), _mode);
554                     if (ret != (int)BluetoothError.None) {
555                         Log.Error (Globals.LogTag, "Failed to set advertising mode- " + (BluetoothError)ret);
556                         BluetoothErrorFactory.ThrowBluetoothException (ret);
557                     }
558                 }
559             }
560         }
561
562         /// <summary>
563         /// The advertising connectable type.
564         /// </summary>
565         /// <remarks>
566         /// The Bluetooth must be enabled.
567         /// </remarks>
568         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
569         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
570         /// or when the set advertising connectable mode fails.</exception>
571         /// <since_tizen> 3 </since_tizen>
572         public bool AdvertisingConnectable
573         {
574             get
575             {
576                 return _advertisingConnectable;
577             }
578             set
579             {
580                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
581                 {
582                     _advertisingConnectable = value;
583                     int ret = Interop.Bluetooth.SetAdvertisingConnectable (GetHandle (), _advertisingConnectable);
584                     if (ret != (int)BluetoothError.None) {
585                         Log.Error (Globals.LogTag, "Failed to set advertising connectable value- " + (BluetoothError)ret);
586                         BluetoothErrorFactory.ThrowBluetoothException (ret);
587                     }
588                 }
589             }
590         }
591
592         /// <summary>
593         /// Dispose
594         /// </summary>
595         /// <since_tizen> 3 </since_tizen>
596         public void Dispose()
597         {
598             Dispose(true);
599             GC.SuppressFinalize(this);
600         }
601
602         private void Dispose(bool disposing)
603         {
604             //todo...
605         }
606
607         /// <summary>
608         /// The type of the packet.
609         /// </summary>
610         /// <since_tizen> 3 </since_tizen>
611         public BluetoothLePacketType PacketType
612         {
613             get
614             {
615                 return _packetType;
616             }
617             set
618             {
619                 _packetType = value;
620             }
621         }
622         /// <summary>
623         /// Sets the external appearance of this device to the advertise or the scan response data.
624         /// Please refer to the adopted Bluetooth specification for the appearance.
625         /// </summary>
626         /// <remarks>
627         /// The Bluetooth must be enabled.
628         /// </remarks>
629         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
630         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
631         /// or when the set appearance fails.</exception>
632         /// <since_tizen> 3 </since_tizen>
633         public int Appearance
634         {
635             get
636             {
637                 return _appearance;
638             }
639             set
640             {
641                 _appearance = value;
642                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) {
643                     int ret = Interop.Bluetooth.SetAdvertisingAppearance (GetHandle (), _packetType, _appearance);
644                     if (ret != (int)BluetoothError.None) {
645                         Log.Error (Globals.LogTag, "Failed to add appearance value to advertising data- " + (BluetoothError)ret);
646                         BluetoothErrorFactory.ThrowBluetoothException(ret);
647                     }
648                 }
649             }
650         }
651         /// <summary>
652         /// Sets whether the device name has to be included in the advertise or the scan response data.
653         /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
654         /// </summary>
655         /// <remarks>
656         /// The Bluetooth must be enabled.
657         /// </remarks>
658         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
659         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
660         /// or when the set advertising device name fails.</exception>
661         /// <since_tizen> 3 </since_tizen>
662         public bool IncludeDeviceName
663         {
664             get
665             {
666                 return _includeDeviceName;
667             }
668             set
669             {
670                 _includeDeviceName = value;
671                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
672                 {
673                     int ret = Interop.Bluetooth.SetAdvertisingDeviceName(GetHandle(), _packetType, _includeDeviceName);
674                     if (ret != (int)BluetoothError.None) {
675                         Log.Error (Globals.LogTag, "Failed to add device name to advertising data- " + (BluetoothError)ret);
676                         BluetoothErrorFactory.ThrowBluetoothException(ret);
677                     }
678                 }
679             }
680         }
681
682
683         /// <summary>
684         /// Sets whether the transmission power level should be included in the advertise or the scan response data.
685         /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
686         /// </summary>
687         /// <remarks>
688         /// The Bluetooth must be enabled.
689         /// </remarks>
690         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
691         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
692         /// or when the set advertising TC power level fails.</exception>
693         /// <since_tizen> 3 </since_tizen>
694         public bool IncludeTxPowerLevel
695         {
696             get
697             {
698                 return _includePowerLevel;
699             }
700             set
701             {
702                 _includePowerLevel = value;
703                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
704                 {
705                     int ret = Interop.Bluetooth.SetAdvertisingTxPowerLevel(GetHandle(), _packetType, _includePowerLevel);
706                     if (ret != (int)BluetoothError.None)
707                     {
708                         Log.Error(Globals.LogTag, "Failed to add advertising service solicitation uuid- " + (BluetoothError)ret);
709                     }
710                 }
711             }
712         }
713         /// <summary>
714         /// Adds a service UUID to the advertise or the scan response data.
715         /// The maximum advertised or responded data size is 31 bytes
716         /// including the data type and the system wide data.
717         /// </summary>
718         /// <remarks>
719         /// The Bluetooth must be enabled.
720         /// </remarks>
721         /// <param name="packetType">The packet type.</param>
722         /// <param name="serviceUuid"> The service UUID to add to advertise data.</param>
723         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
724         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
725         /// or when the add advertising service UUID procedure fails.</exception>
726         /// <since_tizen> 3 </since_tizen>
727         public void AddAdvertisingServiceUuid(BluetoothLePacketType packetType, string serviceUuid)
728         {
729             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
730             {
731                 int ret = Interop.Bluetooth.AddAdvertisingServiceUuid (GetHandle (), packetType, serviceUuid);
732                 if (ret != (int)BluetoothError.None) {
733                     Log.Error (Globals.LogTag, "Failed to add service uuid to advertising data- " + (BluetoothError)ret);
734                     BluetoothErrorFactory.ThrowBluetoothException (ret);
735                 }
736             }
737             else
738             {
739                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
740             }
741         }
742
743         /// <summary>
744         /// Adds a service solicitation UUID to advertise or scan the response data.
745         /// The maximum advertised or responded data size is 31 bytes
746         /// including the data type and the system wide data.
747         /// </summary>
748         /// <remarks>
749         /// The Bluetooth must be enabled.
750         /// </remarks>
751         /// <param name="packetType">The packet type.</param>
752         /// <param name="serviceSolicitationUuid"> The service solicitation UUID to add to advertise data.</param>
753         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
754         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
755         /// or when the add advertising service solicitation UUID procedure fails.</exception>
756         /// <since_tizen> 3 </since_tizen>
757         public void AddAdvertisingServiceSolicitationUuid(BluetoothLePacketType packetType,
758                                                         string serviceSolicitationUuid)
759         {
760             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
761             {
762                 int ret = Interop.Bluetooth.AddAdvertisingServiceSolicitationUuid(GetHandle(), packetType,
763                                                                 serviceSolicitationUuid);
764                 if (ret != (int)BluetoothError.None) {
765                     Log.Error (Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
766                     BluetoothErrorFactory.ThrowBluetoothException(ret);
767                 }
768             }
769             else
770             {
771                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
772             }
773         }
774
775         /// <summary>
776         /// Adds a service data to the advertise or the scan response data.
777         /// The maximum advertised or responded data size is 31 bytes
778         /// including data type and system wide data.
779         /// </summary>
780         /// <remarks>
781         /// The Bluetooth must be enabled.
782         /// </remarks>
783         /// <param name="packetType">The packet type.</param>
784         /// <param name="data"> The service data to be added to advertising.</param>
785         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
786         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
787         /// or when the add advertising data procedure fails.</exception>
788         /// <since_tizen> 3 </since_tizen>
789         public void AddAdvertisingServiceData(BluetoothLePacketType packetType, BluetoothServiceData data)
790         {
791             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
792             {
793                 IntPtr serviceDataPtr;
794                 serviceDataPtr = Marshal.AllocHGlobal(data.DataLength);
795                 Marshal.Copy(data.Data, 0, serviceDataPtr, data.DataLength);
796
797                 for (int i = 0; i < 3; i++)
798                     Log.Error (Globals.LogTag, " service data is  " + data.Data [i]);
799                 int ret = Interop.Bluetooth.AddAdvertisingServiceData(GetHandle(), packetType,
800                     data.Uuid, serviceDataPtr, data.DataLength);
801                 if (ret != (int)BluetoothError.None)
802                 {
803                     Log.Error(Globals.LogTag, "Failed to add service data to advertising data- " + (BluetoothError)ret);
804                     BluetoothErrorFactory.ThrowBluetoothException(ret);
805                 }
806             }
807             else
808             {
809                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
810             }
811         }
812
813         /// <summary>
814         /// Adds the manufacturer specific data to the advertise or the scan response data.
815         /// Please refer to the adopted Bluetooth specification for the the appearance.
816         /// </summary>
817         /// <remarks>
818         /// The Bluetooth must be enabled.
819         /// </remarks>
820         /// <param name="packetType">The packet type.</param>
821         /// <param name="manufacturerData"> The manufacturer specific data.</param>
822         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
823         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
824         /// or when the add advertising manufacturer data procedure fails.</exception>
825         /// <since_tizen> 3 </since_tizen>
826         public void AddAdvertisingManufacturerData(BluetoothLePacketType packetType,
827                                     ManufacturerData manufacturerData)
828         {
829             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
830             {
831                 IntPtr manufDataPtr;
832                 manufDataPtr = Marshal.AllocHGlobal(manufacturerData.DataLength);
833                 Marshal.Copy(manufacturerData.Data, 0, manufDataPtr, manufacturerData.DataLength);
834
835                 int ret = Interop.Bluetooth.AddAdvertisingManufData(GetHandle(), packetType,
836                     manufacturerData.Id, manufDataPtr, manufacturerData.DataLength);
837                 if (ret != (int)BluetoothError.None)
838                 {
839                     Log.Error(Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
840                     BluetoothErrorFactory.ThrowBluetoothException(ret);
841                 }
842             }
843             else
844             {
845                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
846             }
847         }
848
849         /// <summary>
850         /// Clears all data to be advertised or responded to the scan request from the LE scanning device.
851         /// </summary>
852         /// <remarks>
853         /// The Bluetooth must be enabled.
854         /// </remarks>
855         /// <param name="packetType">The packet type to be cleared.</param>
856         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
857         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
858         /// or when the clear advertising data procedure fails.</exception>
859         internal void ClearAdvertisingData(BluetoothLePacketType packetType)
860         {
861             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
862             {
863                 int ret = Interop.Bluetooth.ClearAdvertisingData (GetHandle (), packetType);
864                 if (ret != (int)BluetoothError.None) {
865                     Log.Error (Globals.LogTag, "Failed to Clear Advertising Data- " + (BluetoothError)ret);
866                 }
867             }
868             else
869             {
870                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
871             }
872         }
873     }
874 }