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