db0e2ecc439c5ccef3ad334115f360201cf29076
[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.
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         [Obsolete("Deprecated since API level 6. Please use new methods with this type on BluetoothLeDevice.")]
251         public BluetoothLePacketType PacketType
252         {
253             get
254             {
255                 return _packetType;
256             }
257             set
258             {
259                 _packetType = value;
260             }
261         }
262
263         /// <summary>
264         /// Gets the service UUIDs list from the LE scan result.
265         /// </summary>
266         /// <value> Gets the list of the string service UUIDs.</value>
267         /// <remarks>
268         /// The Bluetooth must be enabled.
269         /// </remarks>
270         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
271         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
272         /// <since_tizen> 3 </since_tizen>
273         [Obsolete("Deprecated since API level 6. Please use GetServiceUuid() method on BluetoothLeDevice.")]
274         public IEnumerable<string> ServiceUuid
275         {
276             get
277             {
278                 return GetServiceUuid(_packetType);
279             }
280         }
281
282         /// <summary>
283         /// Gets the service UUIDs list from the LE scan result.
284         /// </summary>
285         /// <value> Gets the list of the string service UUIDs.</value>
286         /// <remarks>The Bluetooth must be enabled.</remarks>
287         /// <param name="packetType"> The enumeration for BLE packet type.</param>
288         /// <returns>The service UUID value</returns>
289         /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
290         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
291         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
292         /// <since_tizen> 6 </since_tizen>
293         public IEnumerable<string> GetServiceUuid(BluetoothLePacketType packetType)
294         {
295             return BluetoothLeImplAdapter.Instance.GetLeScanResultServiceUuids(_scanData, packetType);
296         }
297
298         /// <summary>
299         /// Gets the device name from the LE scan result.
300         /// </summary>
301         /// <value> Gets the device name.</value>
302         /// <remarks>
303         /// The Bluetooth must be enabled.
304         /// </remarks>
305         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
306         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
307         /// <since_tizen> 3 </since_tizen>
308         [Obsolete("Deprecated since API level 6. Please use GetDeviceName() method on BluetoothLeDevice.")]
309         public string DeviceName
310         {
311             get
312             {
313                 return GetDeviceName(_packetType);
314             }
315         }
316
317         /// <summary>
318         /// Gets the device name from the LE scan result.
319         /// </summary>
320         /// <value> Gets the device name.</value>
321         /// <remarks>The Bluetooth must be enabled.</remarks>
322         /// <param name="packetType"> The enumeration for BLE packet type.</param>
323         /// <returns>The device name value</returns>
324         /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
325         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
326         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
327         /// <since_tizen> 6 </since_tizen>
328         public string GetDeviceName(BluetoothLePacketType packetType)
329         {
330             return BluetoothLeImplAdapter.Instance.GetLeScanResultDeviceName(_scanData, packetType);
331         }
332
333         /// <summary>
334         /// Gets the transmission power level from the LE scan result.
335         /// </summary>
336         /// <value> Gets the transmission power level in dB.</value>
337         /// <remarks>
338         /// The Bluetooth must be enabled.
339         /// </remarks>
340         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
341         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
342         /// <since_tizen> 3 </since_tizen>
343         [Obsolete("Deprecated since API level 6. Please use GetTxPowerLevel() method on BluetoothLeDevice.")]
344         public int TxPowerLevel
345         {
346             get
347             {
348                 return GetTxPowerLevel(_packetType);
349             }
350         }
351
352         /// <summary>
353         /// Gets the transmission power level from the LE scan result.
354         /// </summary>
355         /// <value> Gets the transmission power level in dB.</value>
356         /// <remarks>The Bluetooth must be enabled.</remarks>
357         /// <param name="packetType"> The enumeration for BLE packet type.</param>
358         /// <returns>The tx power level value</returns>
359         /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
360         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
361         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
362         /// <since_tizen> 6 </since_tizen>
363         public int GetTxPowerLevel(BluetoothLePacketType packetType)
364         {
365             return BluetoothLeImplAdapter.Instance.GetScanResultTxPowerLevel(_scanData, packetType);
366         }
367
368         /// <summary>
369         /// Gets the service solicitation UUID list from the scan result.
370         /// </summary>
371         /// <value> Gets the list of the service solicitation UUIDs.</value>
372         /// <remarks>
373         /// The Bluetooth must be enabled.
374         /// </remarks>
375         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
376         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
377         /// <since_tizen> 3 </since_tizen>
378         [Obsolete("Deprecated since API level 6. Please use GetServiceSolicitationUuid() method on BluetoothLeDevice.")]
379         public IEnumerable<string> ServiceSolictationUuid
380         {
381             get
382             {
383                 return GetServiceSolicitationUuid(_packetType);
384             }
385         }
386
387         /// <summary>
388         /// Gets the service solicitation UUID list from the scan result.
389         /// </summary>
390         /// <value> Gets the list of the service solicitation UUIDs.</value>
391         /// <remarks>The Bluetooth must be enabled.</remarks>
392         /// <param name="packetType"> The enumeration for BLE packet type.</param>
393         /// <returns>The service solicitation UUID value</returns>
394         /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
395         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
396         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
397         /// <since_tizen> 6 </since_tizen>
398         public IEnumerable<string> GetServiceSolicitationUuid(BluetoothLePacketType packetType)
399         {
400             return BluetoothLeImplAdapter.Instance.GetScanResultSvcSolicitationUuids(_scanData, packetType);
401         }
402
403         /// <summary>
404         /// Gets the manufacturer data from the scan result.
405         /// </summary>
406         /// <value> Gets the appearance value.</value>
407         /// <remarks>
408         /// The Bluetooth must be enabled.
409         /// </remarks>
410         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
411         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
412         /// <since_tizen> 3 </since_tizen>
413         [Obsolete("Deprecated since API level 6. Please use GetAppearance() method on BluetoothLeDevice.")]
414         public int Appearance
415         {
416             get
417             {
418                 return GetAppearance(_packetType);
419             }
420         }
421
422         /// <summary>
423         /// Gets the manufacturer data from the scan result.
424         /// </summary>
425         /// <value> Gets the appearance value.</value>
426         /// <remarks>The Bluetooth must be enabled.</remarks>
427         /// <param name="packetType"> The enumeration for BLE packet type.</param>
428         /// <returns>The appearance value</returns>
429         /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
430         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
431         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
432         /// <since_tizen> 6 </since_tizen>
433         public int GetAppearance(BluetoothLePacketType packetType)
434         {
435             return BluetoothLeImplAdapter.Instance.GetScanResultAppearance(_scanData, packetType);
436         }
437
438         /// <summary>
439         /// Gets the manufacturer data from the scan result.
440         /// </summary>
441         /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
442         /// <remarks>
443         /// The Bluetooth must be enabled.
444         /// </remarks>
445         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
446         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>/// 
447         /// <since_tizen> 3 </since_tizen>
448         [Obsolete("Deprecated since API level 6. Please use GetManufacturerData() method on BluetoothLeDevice.")]
449         public ManufacturerData ManufacturerData
450         {
451             get
452             {
453                 return GetManufacturerData(_packetType);
454             }
455         }
456
457         /// <summary>
458         /// Gets the manufacturer data from the scan result.
459         /// </summary>
460         /// <value> Gets the manufacturer data containing the manucturer data and ID information.</value>
461         /// <remarks>The Bluetooth must be enabled.</remarks>
462         /// <param name="packetType"> The enumeration for BLE packet type.</param>
463         /// <returns>The manufacturer data object</returns>
464         /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
465         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
466         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>/// 
467         /// <since_tizen> 6 </since_tizen>
468         public ManufacturerData GetManufacturerData(BluetoothLePacketType packetType)
469         {
470             return BluetoothLeImplAdapter.Instance.GetScanResultManufacturerData(_scanData, packetType);
471         }
472
473         /// <summary>
474         /// Gets the service data list from the scan result.
475         /// </summary>
476         /// <remarks>
477         /// The Bluetooth must be enabled.
478         /// </remarks>
479         /// <returns> Returns the service data list.</returns>
480         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
481         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
482         /// <since_tizen> 3 </since_tizen>
483         [Obsolete("Deprecated since API level 6. Please use GetServiceDataList() method on BluetoothLeDevice.")]
484         public IEnumerable<BluetoothLeServiceData> GetServiceDataList()
485         {
486             return GetServiceDataList(_packetType);
487         }
488
489         /// <summary>
490         /// Gets the service data list from the scan result.
491         /// </summary>
492         /// <remarks>The Bluetooth must be enabled.</remarks>
493         /// <param name="packetType"> The packet type.</param>
494         /// <returns> Returns the service data list.</returns>
495         /// <feature>http://tizen.org/feature/network.bluetooth.le</feature>
496         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
497         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled.</exception>
498         /// <since_tizen> 6 </since_tizen>
499         public IEnumerable<BluetoothLeServiceData> GetServiceDataList(BluetoothLePacketType packetType)
500         {
501             return BluetoothLeImplAdapter.Instance.GetScanResultServiceDataList(_scanData, packetType);
502         }
503
504         /// <summary>
505         /// Creates a GATT connection with the remote device.
506         /// </summary>
507         /// <remarks>
508         /// The Bluetooth must be enabled.
509         /// </remarks>
510         /// <param name="autoConnect"> The auto connect flag.</param>
511         /// <returns>client instance</returns>
512         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
513         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
514         /// or when the gatt connection attempt to remote device fails.</exception>
515         /// <since_tizen> 3 </since_tizen>
516         [Obsolete("Deprecated since API level 6. Please use CreateClient() and ConnectAsync() method on BluetoothGattClient.")]
517         public BluetoothGattClient GattConnect(bool autoConnect)
518         {
519             BluetoothGattClient client = null;
520             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
521             {
522                 int ret = BluetoothLeImplAdapter.Instance.GattConnect (_remoteAddress, autoConnect);
523                 if (ret != (int)BluetoothError.None) {
524                     Log.Error (Globals.LogTag, "Failed to create GATT Connection with remote device- " + (BluetoothError)ret);
525                 }
526                 else
527                 {
528                     client = BluetoothGattClient.CreateClient(_remoteAddress);
529                 }
530             }
531             else
532             {
533                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
534             }
535             return client;
536         }
537
538         /// <summary>
539         /// Disconnects a GATT connection with the remote device.
540         /// </summary>
541         /// <remarks>
542         /// The Bluetooth must be enabled.
543         /// </remarks>
544         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
545         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
546         /// or when the GATT disconnection attempt to remote device fails.</exception>
547         /// <since_tizen> 3 </since_tizen>
548         [Obsolete("Deprecated since API level 6. Please use DisconnectAsync() method on BluetoothGattClient.")]
549         public void GattDisconnect()
550         {
551             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
552             {
553                 int ret = BluetoothLeImplAdapter.Instance.GattDisconnect (_remoteAddress);
554                 if (ret != (int)BluetoothError.None) {
555                     Log.Error (Globals.LogTag, "Failed to disconnect GATT connection with remote device- " + (BluetoothError)ret);
556                 }
557             }
558             else
559             {
560                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
561             }
562         }
563     }
564
565     /// <summary>
566     /// Bluetooth LE advertise data. Handles the data advertising.
567     /// </summary>
568     /// <since_tizen> 3 </since_tizen>
569     public class BluetoothLeAdvertiseData:IDisposable
570     {
571         private IntPtr _handle = IntPtr.Zero;
572         private BluetoothLeAdvertisingMode _mode;
573         private bool _advertisingConnectable;
574         private BluetoothLePacketType _packetType;
575         private int _appearance;
576         private bool _includePowerLevel;
577         private bool _includeDeviceName;
578
579         /// <summary>
580         /// The default constructor initializes an object of the BluetoothLeAdvertiseData.
581         /// </summary>
582         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
583         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
584         /// or when create advertiser fails.</exception>
585         /// <since_tizen> 3 </since_tizen>
586         public BluetoothLeAdvertiseData()
587         {
588             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
589             {
590                 Log.Debug(Globals.LogTag, " Creating LeAdvertiser()");
591                 int ret = Interop.Bluetooth.CreateAdvertiser(out _handle);
592                 if (ret != (int)BluetoothError.None)
593                 {
594                     Log.Error(Globals.LogTag, "Failed to create advertiser object- " + (BluetoothError)ret);
595                     BluetoothErrorFactory.ThrowBluetoothException(ret);
596                 }
597             }
598             else
599             {
600                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
601             }
602         }
603
604         /// <summary>
605         /// BluetoothLeAdvertiseData destructor.
606         /// </summary>
607         ~BluetoothLeAdvertiseData()
608         {
609             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
610             {
611                 //clean-up
612                 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeAdvertisingPacket);
613                 ClearAdvertisingData (BluetoothLePacketType.BluetoothLeScanResponsePacket);
614                 BluetoothLeImplAdapter.Instance.DestroyAdvertiser (_handle);
615             }
616             Dispose(false);
617         }
618
619         internal IntPtr GetHandle()
620         {
621             return _handle;
622         }
623
624         /// <summary>
625         /// The advertising mode to control the advertising power and latency.
626         /// </summary>
627         /// <remarks>
628         /// The Bluetooth must be enabled.
629         /// </remarks>
630         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
631         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
632         /// or when the set advertising mode fails.</exception>
633         /// <since_tizen> 3 </since_tizen>
634         public BluetoothLeAdvertisingMode AdvertisingMode
635         {
636             get
637             {
638                 return _mode;
639             }
640             set
641             {
642                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
643                 {
644                     _mode = value;
645                     int ret = Interop.Bluetooth.SetAdvertisingMode (GetHandle (), _mode);
646                     if (ret != (int)BluetoothError.None) {
647                         Log.Error (Globals.LogTag, "Failed to set advertising mode- " + (BluetoothError)ret);
648                         BluetoothErrorFactory.ThrowBluetoothException (ret);
649                     }
650                 }
651             }
652         }
653
654         /// <summary>
655         /// The advertising connectable type.
656         /// </summary>
657         /// <remarks>
658         /// The Bluetooth must be enabled.
659         /// </remarks>
660         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
661         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
662         /// or when the set advertising connectable mode fails.</exception>
663         /// <since_tizen> 3 </since_tizen>
664         public bool AdvertisingConnectable
665         {
666             get
667             {
668                 return _advertisingConnectable;
669             }
670             set
671             {
672                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
673                 {
674                     _advertisingConnectable = value;
675                     int ret = Interop.Bluetooth.SetAdvertisingConnectable (GetHandle (), _advertisingConnectable);
676                     if (ret != (int)BluetoothError.None) {
677                         Log.Error (Globals.LogTag, "Failed to set advertising connectable value- " + (BluetoothError)ret);
678                         BluetoothErrorFactory.ThrowBluetoothException (ret);
679                     }
680                 }
681             }
682         }
683
684         /// <summary>
685         /// Dispose
686         /// </summary>
687         /// <since_tizen> 3 </since_tizen>
688         public void Dispose()
689         {
690             Dispose(true);
691             GC.SuppressFinalize(this);
692         }
693
694         private void Dispose(bool disposing)
695         {
696             //todo...
697         }
698
699         /// <summary>
700         /// The type of the packet.
701         /// </summary>
702         /// <since_tizen> 3 </since_tizen>
703         public BluetoothLePacketType PacketType
704         {
705             get
706             {
707                 return _packetType;
708             }
709             set
710             {
711                 _packetType = value;
712             }
713         }
714         /// <summary>
715         /// Sets the external appearance of this device to the advertise or the scan response data.
716         /// Please refer to the adopted Bluetooth specification for the appearance.
717         /// </summary>
718         /// <remarks>
719         /// The Bluetooth must be enabled.
720         /// </remarks>
721         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
722         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
723         /// or when the set appearance fails.</exception>
724         /// <since_tizen> 3 </since_tizen>
725         public int Appearance
726         {
727             get
728             {
729                 return _appearance;
730             }
731             set
732             {
733                 _appearance = value;
734                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) {
735                     int ret = Interop.Bluetooth.SetAdvertisingAppearance (GetHandle (), _packetType, _appearance);
736                     if (ret != (int)BluetoothError.None) {
737                         Log.Error (Globals.LogTag, "Failed to add appearance value to advertising data- " + (BluetoothError)ret);
738                         BluetoothErrorFactory.ThrowBluetoothException(ret);
739                     }
740                 }
741             }
742         }
743
744         /// <summary>
745         /// Sets whether the device name has to be included in the advertise or the scan response data.
746         /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
747         /// </summary>
748         /// <remarks>
749         /// The Bluetooth must be enabled.
750         /// </remarks>
751         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
752         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
753         /// or when the set advertising device name fails.</exception>
754         /// <since_tizen> 3 </since_tizen>
755         public bool IncludeDeviceName
756         {
757             get
758             {
759                 return _includeDeviceName;
760             }
761             set
762             {
763                 _includeDeviceName = value;
764                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
765                 {
766                     int ret = Interop.Bluetooth.SetAdvertisingDeviceName(GetHandle(), _packetType, _includeDeviceName);
767                     if (ret != (int)BluetoothError.None) {
768                         Log.Error (Globals.LogTag, "Failed to add device name to advertising data- " + (BluetoothError)ret);
769                         BluetoothErrorFactory.ThrowBluetoothException(ret);
770                     }
771                 }
772             }
773         }
774
775
776         /// <summary>
777         /// Sets whether the transmission power level should be included in the advertise or the scan response data.
778         /// The maximum advertised or responded data size is 31 bytes including the data type and the system wide data.
779         /// </summary>
780         /// <remarks>
781         /// The Bluetooth must be enabled.
782         /// </remarks>
783         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
784         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
785         /// or when the set advertising TC power level fails.</exception>
786         /// <since_tizen> 3 </since_tizen>
787         public bool IncludeTxPowerLevel
788         {
789             get
790             {
791                 return _includePowerLevel;
792             }
793             set
794             {
795                 _includePowerLevel = value;
796                 if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
797                 {
798                     int ret = Interop.Bluetooth.SetAdvertisingTxPowerLevel(GetHandle(), _packetType, _includePowerLevel);
799                     if (ret != (int)BluetoothError.None)
800                     {
801                         Log.Error(Globals.LogTag, "Failed to add advertising service solicitation uuid- " + (BluetoothError)ret);
802                     }
803                 }
804             }
805         }
806         /// <summary>
807         /// Adds a service UUID to the advertise or the scan response data.
808         /// The maximum advertised or responded data size is 31 bytes
809         /// including the data type and the system wide data.
810         /// </summary>
811         /// <remarks>
812         /// The Bluetooth must be enabled.
813         /// </remarks>
814         /// <param name="packetType">The packet type.</param>
815         /// <param name="serviceUuid"> The service UUID to add to advertise data.</param>
816         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
817         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
818         /// or when the add advertising service UUID procedure fails.</exception>
819         /// <since_tizen> 3 </since_tizen>
820         public void AddAdvertisingServiceUuid(BluetoothLePacketType packetType, string serviceUuid)
821         {
822             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
823             {
824                 int ret = Interop.Bluetooth.AddAdvertisingServiceUuid (GetHandle (), packetType, serviceUuid);
825                 if (ret != (int)BluetoothError.None) {
826                     Log.Error (Globals.LogTag, "Failed to add service uuid to advertising data- " + (BluetoothError)ret);
827                     BluetoothErrorFactory.ThrowBluetoothException (ret);
828                 }
829             }
830             else
831             {
832                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
833             }
834         }
835
836         /// <summary>
837         /// Adds a service solicitation UUID to advertise or scan the response data.
838         /// The maximum advertised or responded data size is 31 bytes
839         /// including the data type and the system wide data.
840         /// </summary>
841         /// <remarks>
842         /// The Bluetooth must be enabled.
843         /// </remarks>
844         /// <param name="packetType">The packet type.</param>
845         /// <param name="serviceSolicitationUuid"> The service solicitation UUID to add to advertise data.</param>
846         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
847         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
848         /// or when the add advertising service solicitation UUID procedure fails.</exception>
849         /// <since_tizen> 3 </since_tizen>
850         public void AddAdvertisingServiceSolicitationUuid(BluetoothLePacketType packetType,
851                                                         string serviceSolicitationUuid)
852         {
853             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
854             {
855                 int ret = Interop.Bluetooth.AddAdvertisingServiceSolicitationUuid(GetHandle(), packetType,
856                                                                 serviceSolicitationUuid);
857                 if (ret != (int)BluetoothError.None) {
858                     Log.Error (Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
859                     BluetoothErrorFactory.ThrowBluetoothException(ret);
860                 }
861             }
862             else
863             {
864                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
865             }
866         }
867
868         /// <summary>
869         /// Adds a service data to the advertise or the scan response data.
870         /// The maximum advertised or responded data size is 31 bytes
871         /// including data type and system wide data.
872         /// </summary>
873         /// <remarks>
874         /// The Bluetooth must be enabled.
875         /// </remarks>
876         /// <param name="packetType">The packet type.</param>
877         /// <param name="data"> The service data to be added to advertising.</param>
878         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
879         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
880         /// or when the add advertising data procedure fails.</exception>
881         /// <since_tizen> 3 </since_tizen>
882         public void AddAdvertisingServiceData(BluetoothLePacketType packetType, BluetoothServiceData data)
883         {
884             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
885             {
886                 IntPtr serviceDataPtr;
887                 serviceDataPtr = Marshal.AllocHGlobal(data.DataLength);
888                 Marshal.Copy(data.Data, 0, serviceDataPtr, data.DataLength);
889
890                 for (int i = 0; i < data.DataLength; i++)
891                     Log.Error (Globals.LogTag, " service data is  " + data.Data [i]);
892                 int ret = Interop.Bluetooth.AddAdvertisingServiceData(GetHandle(), packetType,
893                     data.Uuid, serviceDataPtr, data.DataLength);
894                 if (ret != (int)BluetoothError.None)
895                 {
896                     Log.Error(Globals.LogTag, "Failed to add service data to advertising data- " + (BluetoothError)ret);
897                     BluetoothErrorFactory.ThrowBluetoothException(ret);
898                 }
899             }
900             else
901             {
902                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
903             }
904         }
905
906         /// <summary>
907         /// Adds the manufacturer specific data to the advertise or the scan response data.
908         /// Please refer to the adopted Bluetooth specification for the the appearance.
909         /// </summary>
910         /// <remarks>
911         /// The Bluetooth must be enabled.
912         /// </remarks>
913         /// <param name="packetType">The packet type.</param>
914         /// <param name="manufacturerData"> The manufacturer specific data.</param>
915         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
916         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
917         /// or when the add advertising manufacturer data procedure fails.</exception>
918         /// <since_tizen> 3 </since_tizen>
919         public void AddAdvertisingManufacturerData(BluetoothLePacketType packetType,
920                                     ManufacturerData manufacturerData)
921         {
922             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
923             {
924                 IntPtr manufDataPtr;
925                 manufDataPtr = Marshal.AllocHGlobal(manufacturerData.DataLength);
926                 Marshal.Copy(manufacturerData.Data, 0, manufDataPtr, manufacturerData.DataLength);
927
928                 int ret = Interop.Bluetooth.AddAdvertisingManufData(GetHandle(), packetType,
929                     manufacturerData.Id, manufDataPtr, manufacturerData.DataLength);
930                 if (ret != (int)BluetoothError.None)
931                 {
932                     Log.Error(Globals.LogTag, "Failed to add service solicitation uuid to advertising data- " + (BluetoothError)ret);
933                     BluetoothErrorFactory.ThrowBluetoothException(ret);
934                 }
935             }
936             else
937             {
938                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
939             }
940         }
941
942         /// <summary>
943         /// Clears all data to be advertised or responded to the scan request from the LE scanning device.
944         /// </summary>
945         /// <remarks>
946         /// The Bluetooth must be enabled.
947         /// </remarks>
948         /// <param name="packetType">The packet type to be cleared.</param>
949         /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception>
950         /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled
951         /// or when the clear advertising data procedure fails.</exception>
952         internal void ClearAdvertisingData(BluetoothLePacketType packetType)
953         {
954             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
955             {
956                 int ret = Interop.Bluetooth.ClearAdvertisingData (GetHandle (), packetType);
957                 if (ret != (int)BluetoothError.None) {
958                     Log.Error (Globals.LogTag, "Failed to Clear Advertising Data- " + (BluetoothError)ret);
959                 }
960             }
961             else
962             {
963                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
964             }
965         }
966     }
967 }