2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.Runtime.CompilerServices;
20 using System.Runtime.InteropServices;
22 using System.Threading.Tasks;
24 namespace Tizen.Network.Bluetooth
26 internal class BluetoothGattServerImpl
28 private BluetoothGattServerHandle _handle;
29 internal event EventHandler<NotificationSentEventArg> _notificationSent;
31 Dictionary<int, TaskCompletionSource<bool>> _sendIndicationTaskSource = new Dictionary<int, TaskCompletionSource<bool>>();
32 private Interop.Bluetooth.BtGattServerNotificationSentCallback _sendIndicationCallback;
33 private Interop.Bluetooth.BtGattForeachCallback _serviceForeachCallback;
35 internal BluetoothGattServerImpl()
37 _sendIndicationCallback = SendIndicationCallback;
39 int err = Interop.Bluetooth.BtGattServerInitialize();
40 GattUtil.ThrowForError(err, "Failed to initialize server");
42 err = Interop.Bluetooth.BtGattServerCreate(out _handle);
43 GattUtil.ThrowForError(err, "Failed to create server");
48 int err = Interop.Bluetooth.BtGattServerStart();
49 GattUtil.ThrowForError(err, "Failed to start server");
52 internal void RegisterGattService(BluetoothGattServer server, BluetoothGattService service)
54 int err = Interop.Bluetooth.BtGattServerRegisterService(_handle, service.GetHandle());
55 GattUtil.ThrowForError(err, "Failed to Register service");
57 service.SetParent(server);
60 internal void UnregisterGattService(BluetoothGattService service)
62 int err = Interop.Bluetooth.BtGattServerUnregisterService(_handle, service.GetHandle());
63 GattUtil.ThrowForError(err, "Failed to Unregister service");
65 service.UnregisterService();
68 internal void UnregisterAllGattServices(BluetoothGattServer server)
70 int err = Interop.Bluetooth.BtGattServerUnregisterAllServices(_handle);
71 GattUtil.ThrowForError(err, "Failed to Unregister all services");
74 internal BluetoothGattService GetService(BluetoothGattServer server, string uuid)
76 BluetoothGattAttributeHandle serviceHandle;
77 int err = Interop.Bluetooth.BtGattServerGetService(_handle, uuid, out serviceHandle);
80 GattUtil.Error(err, string.Format("Failed to get service with UUID ({0})", uuid));
84 BluetoothGattService service = new BluetoothGattService(new BluetoothGattServiceImpl(serviceHandle), uuid); ;
85 service.SetParent(server);
89 internal IEnumerable<BluetoothGattService> GetServices(BluetoothGattServer server)
91 List<BluetoothGattService> attribututeList = new List<BluetoothGattService>();
92 _serviceForeachCallback = (total, index, attributeHandle, userData) =>
94 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
95 BluetoothGattService service = BluetoothGattServiceImpl.CreateBluetoothGattService(handle, ""); ;
98 service.SetParent(server);
99 attribututeList.Add(service);
104 int err = Interop.Bluetooth.BtGattServerForeachServices(_handle, _serviceForeachCallback, IntPtr.Zero);
105 GattUtil.Error(err, "Failed to get all services");
107 return attribututeList;
110 internal void SendResponse(int requestId, int request_type, int status, byte[] value, int offset)
112 int err = Interop.Bluetooth.BtGattServerSendResponse(requestId, request_type, offset, status, value, value.Length);
113 GattUtil.ThrowForError(err, string.Format("Failed to send response for request Id {0}", requestId));
116 void SendIndicationCallback(int result, string clientAddress, IntPtr serverHandle, IntPtr characteristicHandle, bool completed, IntPtr userData)
118 int requestId = (int)userData;
119 if (_sendIndicationTaskSource.ContainsKey(requestId))
121 _notificationSent?.Invoke(this, new NotificationSentEventArg(null, clientAddress, result, completed));
124 _sendIndicationTaskSource[requestId].SetResult(true);
128 _sendIndicationTaskSource[requestId].SetResult(false);
130 _sendIndicationTaskSource.Remove(requestId);
134 internal Task<bool> SendIndicationAsync(BluetoothGattServer server, BluetoothGattCharacteristic characteristic, string clientAddress)
136 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
141 requestId = _requestId++;
142 _sendIndicationTaskSource[requestId] = task;
145 int err = Interop.Bluetooth.BtGattServerNotify(characteristic.GetHandle(), _sendIndicationCallback, clientAddress, (IntPtr)requestId);
148 GattUtil.Error(err, string.Format("Failed to send value changed indication for characteristic uuid {0}", characteristic.Uuid));
149 task.SetResult(false);
150 _sendIndicationTaskSource.Remove(requestId);
151 BluetoothErrorFactory.ThrowBluetoothException(err);
156 internal BluetoothGattServerHandle GetHandle()
162 internal class BluetoothGattClientImpl
164 private BluetoothGattClientHandle _handle;
166 Dictionary<int, TaskCompletionSource<bool>> _readValueTaskSource = new Dictionary<int, TaskCompletionSource<bool>>();
167 private Interop.Bluetooth.BtGattClientRequestCompletedCallback _readValueCallback;
168 Dictionary<int, TaskCompletionSource<bool>> _writeValueTaskSource = new Dictionary<int, TaskCompletionSource<bool>>();
169 private Interop.Bluetooth.BtGattClientRequestCompletedCallback _writeValueCallback;
170 private Interop.Bluetooth.BtGattForeachCallback _serviceForeachCallback;
172 internal BluetoothGattClientImpl(string remoteAddress)
174 _readValueCallback = ReadValueCallback;
175 _writeValueCallback = WriteValueCallback;
177 if (BluetoothAdapter.IsBluetoothEnabled)
179 int err = Interop.Bluetooth.BtGattClientCreate(remoteAddress, out _handle);
180 GattUtil.ThrowForError(err, "Failed to get native client handle");
184 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
188 internal void Connect(string remoteAddress, bool autoConnect)
190 int err = Interop.Bluetooth.GattConnect(remoteAddress, autoConnect);
191 GattUtil.ThrowForError(err, "Failed to connect to remote address");
194 internal void Disconnect(string remoteAddress)
196 int err = Interop.Bluetooth.GattDisconnect(remoteAddress);
197 GattUtil.ThrowForError(err, "Failed to disconnect to remote address");
200 internal string GetRemoteAddress()
202 string remoteAddress;
203 int err = Interop.Bluetooth.BtGattClientGetRemoteAddress(_handle, out remoteAddress);
204 GattUtil.ThrowForError(err, "Failed to get remote address for this client");
206 return remoteAddress;
209 internal BluetoothGattService GetService(BluetoothGattClient client, string uuid)
211 BluetoothGattAttributeHandle serviceHandle;
212 int err = Interop.Bluetooth.BtGattClientGetService(_handle, uuid, out serviceHandle);
215 GattUtil.Error(err, string.Format("Failed to get service with UUID ({0})", uuid));
219 BluetoothGattService service = new BluetoothGattService(new BluetoothGattServiceImpl(serviceHandle), uuid); ;
220 service.SetParent(client);
224 internal IEnumerable<BluetoothGattService> GetServices(BluetoothGattClient client)
226 List<BluetoothGattService> attribututeList = new List<BluetoothGattService>();
227 _serviceForeachCallback = (total, index, attributeHandle, userData) =>
229 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
230 BluetoothGattService service = BluetoothGattServiceImpl.CreateBluetoothGattService(handle, "");
233 service.SetParent(client);
234 attribututeList.Add(service);
239 int err = Interop.Bluetooth.BtGattClientForeachServices(_handle, _serviceForeachCallback, IntPtr.Zero);
240 GattUtil.Error(err, "Failed to get all services");
242 return attribututeList;
245 void ReadValueCallback(int result, IntPtr requestHandle, IntPtr userData)
247 int requestId = (int)userData;
248 if (_readValueTaskSource.ContainsKey(requestId))
250 if (result == (int)BluetoothError.None)
252 _readValueTaskSource[requestId].SetResult(true);
256 _readValueTaskSource[requestId].SetResult(false);
259 _readValueTaskSource.Remove(requestId);
262 internal Task<bool> ReadValueAsyncTask(BluetoothGattAttributeHandle handle)
264 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
269 requestId = _requestId++;
270 _readValueTaskSource[requestId] = task;
273 int err = Interop.Bluetooth.BtGattClientReadValue(handle, _readValueCallback, (IntPtr)requestId);
276 GattUtil.Error(err, "Failed to read value from remote device");
277 task.SetResult(false);
278 _readValueTaskSource.Remove(requestId);
279 BluetoothErrorFactory.ThrowBluetoothException(err);
284 void WriteValueCallback(int result, IntPtr requestHandle, IntPtr userData)
286 int requestId = (int)userData;
287 if (_writeValueTaskSource.ContainsKey(requestId))
289 if (result == (int)BluetoothError.None)
291 _writeValueTaskSource[requestId].SetResult(true);
295 _writeValueTaskSource[requestId].SetResult(false);
298 _writeValueTaskSource.Remove(requestId);
301 internal Task<bool> WriteValueAsyncTask(BluetoothGattAttributeHandle handle)
303 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
308 requestId = _requestId++;
309 _writeValueTaskSource[requestId] = task;
312 int err = Interop.Bluetooth.BtGattClientWriteValue(handle, _writeValueCallback, (IntPtr)requestId);
315 GattUtil.Error(err, "Failed to write value to remote device");
316 task.SetResult(false);
317 _writeValueTaskSource.Remove(requestId);
318 BluetoothErrorFactory.ThrowBluetoothException(err);
323 internal BluetoothGattClientHandle GetHandle()
329 internal class BluetoothGattServiceImpl : BluetoothGattAttributeImpl
331 private Interop.Bluetooth.BtGattForeachCallback _characteristicForeachCallback;
332 private Interop.Bluetooth.BtGattForeachCallback _includedServiceForeachCallback;
334 internal BluetoothGattServiceImpl(string uuid, BluetoothGattServiceType type)
336 int err = Interop.Bluetooth.BtGattServiceCreate(uuid, (int)type, out _handle);
337 GattUtil.ThrowForError(err, "Failed to get native service handle");
340 internal BluetoothGattServiceImpl(BluetoothGattAttributeHandle handle)
345 internal static BluetoothGattService CreateBluetoothGattService(BluetoothGattAttributeHandle handle, string uuid)
349 int err = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
350 GattUtil.ThrowForError(err, "Failed to get UUID");
353 BluetoothGattServiceImpl impl = new BluetoothGattServiceImpl(handle);
354 return new BluetoothGattService(impl, uuid);
357 internal void AddCharacteristic(BluetoothGattCharacteristic characteristic)
359 int err = Interop.Bluetooth.BtGattServiceAddCharacteristic(_handle, characteristic.GetHandle());
360 GattUtil.ThrowForError(err, string.Format("Failed to add characteristic with UUID ({0})", characteristic.Uuid));
363 internal BluetoothGattCharacteristic GetCharacteristic(BluetoothGattService service, string uuid)
365 BluetoothGattAttributeHandle attributeHandle;
366 int err = Interop.Bluetooth.BtGattServiceGetCharacteristic(_handle, uuid, out attributeHandle);
369 GattUtil.Error(err, string.Format("Failed to get Characteristic with UUID ({0})", uuid));
373 BluetoothGattCharacteristic Characteristic = BluetoothGattCharacteristicImpl.CreateBluetoothGattGattCharacteristic(attributeHandle, uuid);
374 if (Characteristic != null)
376 Characteristic.SetParent(service);
378 return Characteristic;
381 internal IEnumerable<BluetoothGattCharacteristic> GetCharacteristics(BluetoothGattService service)
383 List<BluetoothGattCharacteristic> attribututeList = new List<BluetoothGattCharacteristic>();
384 _characteristicForeachCallback = (total, index, attributeHandle, userData) =>
386 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
387 BluetoothGattCharacteristic Characteristic = BluetoothGattCharacteristicImpl.CreateBluetoothGattGattCharacteristic(handle, "");
388 if (Characteristic != null)
390 Characteristic.SetParent(service);
391 attribututeList.Add(Characteristic);
396 int err = Interop.Bluetooth.BtGattServiceForeachCharacteristics(service.GetHandle(), _characteristicForeachCallback, IntPtr.Zero);
397 GattUtil.Error(err, "Failed to get all Characteristic");
399 return attribututeList;
402 internal void AddIncludeService(BluetoothGattService includedService)
404 int err = Interop.Bluetooth.BtGattServiceAddIncludedService(_handle, includedService.GetHandle());
405 GattUtil.ThrowForError(err, string.Format("Failed to add service with UUID ({0})", includedService.Uuid));
408 internal BluetoothGattService GetIncludeService(BluetoothGattService parentService, string uuid)
410 BluetoothGattAttributeHandle attributeHandle;
411 int err = Interop.Bluetooth.BtGattServiceGetIncludedService(_handle, uuid, out attributeHandle);
414 GattUtil.Error(err, string.Format("Failed to get included service with UUID ({0})", uuid));
418 BluetoothGattService service = new BluetoothGattService(new BluetoothGattServiceImpl(attributeHandle), uuid);
419 service.SetParent(parentService);
423 internal IEnumerable<BluetoothGattService> GetIncludeServices(BluetoothGattService parentService)
425 List<BluetoothGattService> attribututeList = new List<BluetoothGattService>();
426 _includedServiceForeachCallback = (total, index, attributeHandle, userData) =>
428 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
429 BluetoothGattService service = BluetoothGattServiceImpl.CreateBluetoothGattService(handle, "");
432 service.SetParent(parentService);
433 attribututeList.Add(service);
438 int err = Interop.Bluetooth.BtGattServiceForeachIncludedServices(parentService.GetHandle(), _includedServiceForeachCallback, IntPtr.Zero);
439 GattUtil.Error(err, "Failed to get all services");
441 return attribututeList;
445 internal class BluetoothGattCharacteristicImpl : BluetoothGattAttributeImpl
447 private Interop.Bluetooth.BtGattForeachCallback _descriptorForeachCallback;
449 internal BluetoothGattCharacteristicImpl(string uuid, BluetoothGattPermission permission, BluetoothGattProperty property, byte[] value)
451 int err = Interop.Bluetooth.BtGattCharacteristicCreate(uuid, (int)permission, (int)property, value, value.Length, out _handle);
452 GattUtil.ThrowForError(err, "Failed to get native characteristic handle");
455 internal BluetoothGattCharacteristicImpl(BluetoothGattAttributeHandle handle)
460 internal static BluetoothGattCharacteristic CreateBluetoothGattGattCharacteristic(BluetoothGattAttributeHandle handle, string uuid)
463 int err = Interop.Bluetooth.BtGattCharacteristicGetPermissions(handle, out permission);
464 GattUtil.ThrowForError(err, "Failed to get permissions");
468 err = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
469 GattUtil.ThrowForError(err, "Failed to get UUID");
472 BluetoothGattCharacteristicImpl impl = new BluetoothGattCharacteristicImpl(handle);
473 return new BluetoothGattCharacteristic(impl, uuid, (BluetoothGattPermission)permission);
476 internal void SetCharacteristicValueChangedEvent(Interop.Bluetooth.BtClientCharacteristicValueChangedCallback callback)
478 int err = Interop.Bluetooth.BtGattClientSetCharacteristicValueChangedCallback(_handle, callback, IntPtr.Zero);
479 GattUtil.Error(err, "Failed to set client characteristic value changed callback");
482 internal void UnsetCharacteristicValueChangedEvent()
484 int err = Interop.Bluetooth.BtGattClientUnsetCharacteristicValueChangedCallback(_handle);
485 GattUtil.Error(err, "Failed to unset client characteristic value changed callback");
488 internal void SetNotificationStateChangedEvent(Interop.Bluetooth.BtGattServerNotificationStateChangeCallback callback)
490 int err = Interop.Bluetooth.BtGattServeSetNotificationStateChangeCallback(_handle, callback, IntPtr.Zero);
491 GattUtil.Error(err, "Failed to set characteristic notification state changed callback");
494 internal BluetoothGattProperty GetProperties()
497 int err = Interop.Bluetooth.BtGattCharacteristicGetProperties(_handle, out properties);
498 GattUtil.Error(err, "Failed to get characteristic properties");
499 return (BluetoothGattProperty)properties;
502 internal void SetProperties(BluetoothGattProperty perperties)
504 int err = Interop.Bluetooth.BtGattCharacteristicSetProperties(_handle, (int)perperties);
505 GattUtil.Error(err, "Failed to set characteristic properties");
508 internal BluetoothGattWriteType GetWriteType()
511 int err = Interop.Bluetooth.BtGattCharacteristicGetWriteType(_handle, out writeType);
512 GattUtil.Error(err, "Failed to get characteristic writetype");
513 return (BluetoothGattWriteType) writeType;
516 internal void SetWriteType(BluetoothGattWriteType writeType)
518 int err = Interop.Bluetooth.BtGattCharacteristicSetWriteType(_handle, (int)writeType);
519 GattUtil.Error(err, "Failed to get characteristic writetype");
522 internal void AddDescriptor(BluetoothGattDescriptor descriptor)
524 int err = Interop.Bluetooth.BtGattCharacteristicAddDescriptor(_handle, descriptor.GetHandle());
525 GattUtil.ThrowForError(err, string.Format("Failed to add descriptor with UUID ({0})", descriptor.Uuid));
528 internal BluetoothGattDescriptor GetDescriptor(BluetoothGattCharacteristic characteristic, string uuid)
530 BluetoothGattAttributeHandle handle;
531 int err = Interop.Bluetooth.BtGattCharacteristicGetDescriptor(_handle, uuid, out handle);
534 GattUtil.Error(err, string.Format("Failed to get descriptor with UUID ({0})", uuid));
537 BluetoothGattDescriptor descriptor = BluetoothGattDescriptorImpl.CreateBluetoothGattDescriptor(handle, uuid);
538 if (descriptor != null)
540 descriptor.SetParent(characteristic);
545 internal IEnumerable<BluetoothGattDescriptor> GetDescriptors(BluetoothGattCharacteristic characteristic)
547 List<BluetoothGattDescriptor> attribututeList = new List<BluetoothGattDescriptor>();
548 _descriptorForeachCallback = (total, index, attributeHandle, userData) =>
550 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
551 BluetoothGattDescriptor descriptor = BluetoothGattDescriptorImpl.CreateBluetoothGattDescriptor(handle, "");
552 if (descriptor != null)
554 descriptor.SetParent(characteristic);
555 attribututeList.Add(descriptor);
560 int err = Interop.Bluetooth.BtGattCharacteristicForeachDescriptors(characteristic.GetHandle(), _descriptorForeachCallback, IntPtr.Zero);
561 GattUtil.Error(err, "Failed to get all descriptor");
563 return attribututeList;
567 internal class BluetoothGattDescriptorImpl : BluetoothGattAttributeImpl
569 internal BluetoothGattDescriptorImpl(string uuid, BluetoothGattPermission permission, byte[] value)
571 int err = Interop.Bluetooth.BtGattDescriptorCreate(uuid, (int)permission, value, value.Length, out _handle);
572 GattUtil.ThrowForError(err, "Failed to get native descriptor handle");
575 internal BluetoothGattDescriptorImpl(BluetoothGattAttributeHandle handle)
580 internal static BluetoothGattDescriptor CreateBluetoothGattDescriptor(BluetoothGattAttributeHandle handle, string uuid)
583 int err = Interop.Bluetooth.BtGattDescriptorGetPermissions(handle, out permission);
584 GattUtil.ThrowForError(err, string.Format("Failed to get permissions with UUID ({0})", uuid));
588 int ret = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
589 GattUtil.ThrowForError(ret, "Failed to get UUID");
592 BluetoothGattDescriptorImpl impl = new BluetoothGattDescriptorImpl(handle);
593 return new BluetoothGattDescriptor(impl, uuid, (BluetoothGattPermission)permission);
597 internal abstract class BluetoothGattAttributeImpl
599 protected BluetoothGattAttributeHandle _handle;
601 internal string GetUuid()
604 int err = Interop.Bluetooth.BtGattGetUuid(_handle, out uuid);
605 GattUtil.Error(err, "Failed to get attribute uuid");
610 internal byte[] GetValue()
613 int nativeValueLength;
614 int err = Interop.Bluetooth.BtGattGetValue(_handle, out nativeValue, out nativeValueLength);
615 GattUtil.Error(err, "Failed to get attribute value");
617 return GattUtil.IntPtrToByteArray(nativeValue, nativeValueLength);
620 internal void SetValue(byte[] value)
622 int err = Interop.Bluetooth.BtGattSetValue(_handle, value, value.Length);
623 GattUtil.ThrowForError(err, "Failed to set attribute value");
626 internal string GetValue(int offset)
628 byte[] value = GetValue();
630 int nullPos = value.Length - offset;
631 for (int i = offset; i < value.Length; ++i)
633 if (value[i] == '\0')
640 string strValue = "";
641 strValue = Encoding.UTF8.GetString(value, offset, nullPos - offset);
645 internal int GetValue(IntDataType type, int offset)
648 int err = Interop.Bluetooth.BtGattGetIntValue(_handle, (int)type, offset, out value);
649 GattUtil.Error(err, "Failed to get attribute int value at offset");
653 internal void SetValue(IntDataType type, int value, int offset)
655 int err = Interop.Bluetooth.BtGattSetIntValue(_handle, (int)type, value, offset);
656 GattUtil.ThrowForError(err, "Failed to set attribute int value at offset");
659 internal float GetValue(FloatDataType type, int offset)
662 int err = Interop.Bluetooth.BtGattGetFloatValue(_handle, (int)type, offset, out value);
663 GattUtil.Error(err, "Failed to get attribute float value at offset");
667 internal void SetValue(FloatDataType type, int mantissa, int exponent, int offset)
669 int err = Interop.Bluetooth.BtGattSetFloatValue(_handle, (int)type, mantissa, exponent, offset);
670 GattUtil.ThrowForError(err, "Failed to set attribute float value at offset");
673 internal void SetReadValueRequestedEventCallback(Interop.Bluetooth.BtGattServerReadValueRequestedCallback callback)
675 int err = Interop.Bluetooth.BtGattServerSetReadValueRequestedCallback(_handle, callback, IntPtr.Zero);
676 GattUtil.ThrowForError(err, "Failed to set attribute read value requested callback");
679 internal void SetWriteValueRequestedEventCallback(Interop.Bluetooth.BtGattServerWriteValueRequestedCallback callback)
681 int err = Interop.Bluetooth.BtGattServerSetWriteValueRequestedCallback(_handle, callback, IntPtr.Zero);
682 GattUtil.ThrowForError(err, "Failed to set attribute write value requested callback");
685 internal BluetoothGattAttributeHandle GetHandle()
690 internal void ReleaseHandleOwnership()
692 _handle.ReleaseOwnership();
697 internal class BluetoothGattAttributeHandle : BluetoothGattHandle
699 public BluetoothGattAttributeHandle(IntPtr nativeHandle, bool hasOwnership) : base(nativeHandle, hasOwnership)
703 public BluetoothGattAttributeHandle()
707 protected override bool ReleaseHandle()
709 if (_hasOwnership == true)
711 Interop.Bluetooth.BtGattDestroy(handle);
713 SetHandle(IntPtr.Zero);
718 internal class BluetoothGattClientHandle : BluetoothGattHandle
720 protected override bool ReleaseHandle()
722 if (_hasOwnership == true)
724 Interop.Bluetooth.BtGattClientDestroy(handle);
726 SetHandle(IntPtr.Zero);
731 internal class BluetoothGattServerHandle : BluetoothGattHandle
733 protected override bool ReleaseHandle()
735 if (_hasOwnership == true)
739 err = Interop.Bluetooth.BtGattServerDestroy(handle);
742 Log.Error(Globals.LogTag, "Failed to destroy the server instance");
746 err = Interop.Bluetooth.BtGattServerDeinitialize();
749 Log.Error(Globals.LogTag, "Failed to deinitialize");
750 SetHandle(IntPtr.Zero);
754 SetHandle(IntPtr.Zero);
759 internal abstract class BluetoothGattHandle : SafeHandle
761 protected bool _hasOwnership;
763 public BluetoothGattHandle() : base(IntPtr.Zero, true)
765 _hasOwnership = true;
768 public BluetoothGattHandle(IntPtr nativeHandle, bool hasOwnership) : base(nativeHandle, true)
770 _hasOwnership = hasOwnership;
773 public override bool IsInvalid
775 get { return handle == IntPtr.Zero; }
778 public void ReleaseOwnership()
780 _hasOwnership = false;
784 internal static class GattUtil
786 internal static byte[] IntPtrToByteArray(IntPtr nativeValue, int lenght)
788 byte[] value = new byte[lenght];
789 if (nativeValue != IntPtr.Zero)
791 Marshal.Copy(nativeValue, value, 0, lenght);
796 internal static void Error(int err, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
800 Log.Error(Globals.LogTag, string.Format("{0}, err: {1}", message, (BluetoothError)err), file, func, line);
804 internal static void ThrowForError(int err, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
808 Log.Error(Globals.LogTag, string.Format("{0}, err: {1}", message, (BluetoothError)err), file, func, line);
809 BluetoothErrorFactory.ThrowBluetoothException(err);
813 internal static bool IsFailed(this int err)
815 return err != (int)BluetoothError.None;