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