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