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.InteropServices;
21 using System.Threading.Tasks;
23 namespace Tizen.Network.Bluetooth
26 /// The Bluetooth GATT server.
28 /// <since_tizen> 3 </since_tizen>
29 public class BluetoothGattServer : IDisposable
31 private static BluetoothGattServer _instance;
32 private BluetoothGattServerImpl _impl;
34 private BluetoothGattServer()
36 _impl = new BluetoothGattServerImpl();
40 /// (event) This event is called when the indication acknowledgement is received for each notified client.
42 /// <since_tizen> 3 </since_tizen>
43 public event EventHandler<NotificationSentEventArg> NotificationSent
47 _impl._notificationSent += value;
51 _impl._notificationSent -= value;
56 /// Creates the Bluetooth GATT server.
58 /// <returns>The BluetoothGattServer instance.</returns>
59 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
60 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
61 /// <exception cref="InvalidOperationException">Thrown when the create GATT server fails.</exception>
62 /// <since_tizen> 3 </since_tizen>
63 public static BluetoothGattServer CreateServer()
65 if (_instance == null)
67 BluetoothGattServer server = new BluetoothGattServer();
77 /// Registers the server along with the GATT services of the application it is hosting.
79 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
80 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
81 /// <exception cref="InvalidOperationException">Thrown when the register server application fails.</exception>
82 /// <since_tizen> 3 </since_tizen>
89 /// Registers a specified service to this server.
91 /// <param name="service">The service, which needs to be registered with this server.</param>
92 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
93 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
94 /// <exception cref="InvalidOperationException">Thrown when the register service fails.</exception>
95 /// <since_tizen> 3 </since_tizen>
96 public void RegisterGattService(BluetoothGattService service)
98 if (service.IsRegistered())
100 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
102 _impl.RegisterGattService(this, service);
106 /// Unregisters a specified service from this server.
108 /// <param name="service">The service, which needs to be unregistered from this server.</param>
110 /// Once unregistered, the service object will become invalid and should not be used to access sevices or any children attribute's methods/members.
112 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
113 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
114 /// <exception cref="InvalidOperationException">Thrown when the unregister service fails.</exception>
115 /// <since_tizen> 3 </since_tizen>
116 public void UnregisterGattService(BluetoothGattService service)
118 if (service.GetGattServer() != this)
120 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
123 _impl.UnregisterGattService(service);
127 /// Unregisters all services from this server.
130 /// Once unregistered, servicees will become invalid and should not be used to access sevices or any children attribute's methods/members.
132 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
133 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
134 /// <exception cref="InvalidOperationException">Thrown when the unregister all services fail.</exception>
135 /// <since_tizen> 3 </since_tizen>
136 public void UnregisterGattServices()
138 _impl.UnregisterAllGattServices(this);
142 /// Gets service with given UUID that belongs to this server.
144 /// <param name="uuid">The UUID for the service to get.</param>
145 /// <returns>The Service with the given UUID if it exists, null otherwise.</returns>
146 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
147 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
148 /// <exception cref="InvalidOperationException">Thrown when the service is not registered.</exception>
149 /// <since_tizen> 3 </since_tizen>
150 public BluetoothGattService GetService(string uuid)
152 return _impl.GetService(this, uuid);
156 /// Gets the list of services that belongs to this server.
158 /// <returns>The list of services that belongs to this server.</returns>
159 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
160 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
161 /// <exception cref="InvalidOperationException">Thrown when the service is not registered.</exception>
162 /// <since_tizen> 3 </since_tizen>
163 public IEnumerable<BluetoothGattService> GetServices()
165 return _impl.GetServices(this);
169 /// Sends indication for the value change of the characteristic to the remote devices.
171 /// <param name="characteristic">The characteristic whose the value is changed.</param>
172 /// <param name="clientAddress">The remote device address to send, notify, or indicate and if set to NULL, then notify/indicate all is enabled.</param>
173 /// <returns>true on success, false otherwise.</returns>
174 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
175 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
176 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
177 /// or when the remote device is disconnected, or when service is not registered, or when the CCCD is not enabled.</exception>
178 /// <since_tizen> 3 </since_tizen>
179 public async Task<bool> SendIndicationAsync(BluetoothGattCharacteristic characteristic, string clientAddress)
181 return await _impl.SendIndicationAsync(this, characteristic, clientAddress);
185 /// Sends the notification for the value change of the characteristic to the remote devices.
187 /// <param name="characteristic">The characteristic, which has a changed value.</param>
188 /// <param name="clientAddress">The remote device address to send, notify, or indicate and if set to NULL, then notify/indicate all is enabled.</param>
189 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
190 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
191 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
192 /// or when the remote device is disconnected, or when service is not registered, or when the CCCD is not enabled.</exception>
193 /// <since_tizen> 3 </since_tizen>
194 public void SendNotification(BluetoothGattCharacteristic characteristic, string clientAddress)
196 _impl.SendNotification(characteristic, clientAddress);
200 /// Sends a response to the remote device as a result of a read/write request.
202 /// <param name="requestId">The identification of a read/write request.</param>
203 /// <param name="type">The request type for read/write.</param>
204 /// <param name="status">The error value in case of failure, 0 for success.</param>
205 /// <param name="value">The value to be sent.</param>
206 /// <param name="offset">The offset from where the value is read.</param>
207 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
208 /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
209 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
210 /// or when the remote device is disconnected, or the send response procedure fails.</exception>
211 /// <since_tizen> 3 </since_tizen>
212 public void SendResponse(int requestId, BluetoothGattRequestType type, int status, byte[] value, int offset)
214 _impl.SendResponse(requestId, (int)type, status, value, offset);
217 internal bool IsValid()
219 return _impl.GetHandle().IsInvalid == false;
223 /// Destroys the current object.
225 ~BluetoothGattServer()
231 /// Destroys the current object.
233 /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
234 /// <since_tizen> 6 </since_tizen>
235 public void Dispose()
238 GC.SuppressFinalize(this);
242 /// Releases all the resources currently used by this instance.
244 /// <param name="disposing">true if the managed resources should be disposed, otherwise false.</param>
245 /// <since_tizen> 6 </since_tizen>
246 protected virtual void Dispose(bool disposing)
250 _impl?.GetHandle()?.Dispose();
257 /// The Bluetooth GATT client.
259 /// <since_tizen> 3 </since_tizen>
260 public class BluetoothGattClient
262 private BluetoothGattClientImpl _impl;
263 private string _remoteAddress = string.Empty;
265 internal BluetoothGattClient(string remoteAddress)
267 _impl = new BluetoothGattClientImpl(remoteAddress);
268 _remoteAddress = remoteAddress;
271 internal static BluetoothGattClient CreateClient(string remoteAddress)
273 BluetoothGattClient client = new BluetoothGattClient(remoteAddress);
274 return client.Isvalid() ? client : null;
278 /// Destroy Bluetooth GATT client
280 /// <since_tizen> 3 </since_tizen>
281 public void DestroyClient()
283 _impl.GetHandle().Dispose();
287 /// The address of the remote device.
289 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
290 /// or when the remote device is disconnected.</exception>
291 /// <since_tizen> 3 </since_tizen>
292 public string RemoteAddress
296 if (string.IsNullOrEmpty(_remoteAddress))
298 _remoteAddress = _impl.GetRemoteAddress();
300 return _remoteAddress;
305 /// Gets the service with the given UUID that belongs to the remote device.
307 /// <param name="uuid">The UUID for the service to get.</param>
308 /// <returns>The service with the given UUID if it exists, null otherwise.</returns>
309 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
310 /// or when the remote device is disconnected, or when the get service fails.</exception>
311 /// <since_tizen> 3 </since_tizen>
312 public BluetoothGattService GetService(string uuid)
314 return _impl.GetService(this, uuid);
318 /// Gets list of services that belongs to the remote device.
320 /// <returns>The list of services that belongs to the remote device.</returns>
321 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
322 /// or when the remote device is disconnected, or when the get service fails.</exception>
323 /// <since_tizen> 3 </since_tizen>
324 public IEnumerable<BluetoothGattService> GetServices()
326 return _impl.GetServices(this);
330 /// Reads the value of a given characteristic from the remote device asynchronously.
332 /// <param name="characteristic">The characteristic to be read.</param>
333 /// <returns>true on success, false otherwise.</returns>
334 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
335 /// or when the remote device is disconnected, or when the read attribute value fails.</exception>
336 /// <since_tizen> 3 </since_tizen>
337 public async Task<bool> ReadValueAsync(BluetoothGattCharacteristic characteristic)
339 return await _impl.ReadValueAsyncTask(characteristic.GetHandle());
343 /// Reads the value of the given descriptor from the remote device asynchronously.
345 /// <param name="descriptor">The descriptor to be read.</param>
346 /// <returns>true on success, false otherwise.</returns>
347 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
348 /// or when the remote device is disconnected, or when the read attribute value fails.</exception>
349 /// <since_tizen> 3 </since_tizen>
350 public async Task<bool> ReadValueAsync(BluetoothGattDescriptor descriptor)
352 return await _impl.ReadValueAsyncTask(descriptor.GetHandle());
356 /// Writes the value of a given characteristic to the remote device asynchronously.
358 /// <param name="characteristic">The characteristic to be written.</param>
359 /// <returns>true on success, false otherwise.</returns>
360 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
361 /// or when the remote device is disconnected or when the write attribute value fails.</exception>
362 /// <since_tizen> 3 </since_tizen>
363 public async Task<bool> WriteValueAsync(BluetoothGattCharacteristic characteristic)
365 return await _impl.WriteValueAsyncTask(characteristic.GetHandle());
369 /// Writes the value of the given descriptor to the remote device asynchronously.
371 /// <param name="descriptor">The descriptor to be written.</param>
372 /// <returns>true on success, false otherwise.</returns>
373 /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
374 /// or when the remote device is disconnected, or when the write attribute value fails.</exception>
375 /// <since_tizen> 3 </since_tizen>
376 public async Task<bool> WriteValueAsync(BluetoothGattDescriptor descriptor)
378 return await _impl.WriteValueAsyncTask(descriptor.GetHandle());
381 internal bool Isvalid()
383 return _impl.GetHandle().IsInvalid == false;
388 /// The Bluetooth GATT service.
390 /// <since_tizen> 3 </since_tizen>
391 public class BluetoothGattService
393 private BluetoothGattServiceImpl _impl;
394 private BluetoothGattClient _parentClient = null;
395 private BluetoothGattServer _parentServer = null;
396 private BluetoothGattService _parentService = null;
401 /// <param name="uuid">The UUID of the service.</param>
402 /// <param name="type">The type of service.</param>
403 /// <exception cref="InvalidOperationException">Thrown when the create GATT service procedure fails.</exception>
404 /// <since_tizen> 3 </since_tizen>
405 public BluetoothGattService(string uuid, BluetoothGattServiceType type)
408 _impl = new BluetoothGattServiceImpl(uuid, type);
411 internal BluetoothGattService(BluetoothGattServiceImpl impl, string uuid)
418 /// Specification name from the UUID.
420 /// <since_tizen> 3 </since_tizen>
421 public string Uuid { get; }
424 /// Adds a characteristic to this service.
426 /// <param name="characteristic">The characteristic to be added.</param>
427 /// <returns>true on success, false otherwise.</returns>
428 /// <exception cref="InvalidOperationException">Thrown when the add GATT characteristic procedure fails.</exception>
429 /// <since_tizen> 3 </since_tizen>
430 public void AddCharacteristic(BluetoothGattCharacteristic characteristic)
432 if (GetGattClient() != null)
434 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotSupported);
437 if (characteristic.GetService() != null)
439 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
442 _impl.AddCharacteristic(characteristic);
443 characteristic.SetParent(this);
447 /// Gets the characteristic with the given UUID that belongs to this service.
449 /// <param name="uuid">The UUID for the characteristic to get.</param>
450 /// <returns>The characteristic with a given UUID if it exists, null otherwise.</returns>
451 /// <since_tizen> 3 </since_tizen>
452 public BluetoothGattCharacteristic GetCharacteristic(string uuid)
454 return _impl.GetCharacteristic(this, uuid);
458 /// Gets list of the characteristic that belongs to this service.
460 /// <returns>The list of the characteristic that belongs to this service.</returns>
461 /// <since_tizen> 3 </since_tizen>
462 public IEnumerable<BluetoothGattCharacteristic> GetCharacteristics()
464 return _impl.GetCharacteristics(this);
468 /// Includes a service to this service.
470 /// <param name="service">The service to be included.</param>
471 /// <returns>true on success, false otherwise</returns>
472 /// <exception cref="InvalidOperationException">Thrown when the add GATT service procedure fails.</exception>///
473 /// <since_tizen> 3 </since_tizen>
474 public void AddService(BluetoothGattService service)
476 if (GetGattClient() != null)
478 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotSupported);
481 if (service.IsRegistered())
483 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
486 _impl.AddIncludeService(service);
487 service.SetParent(this);
491 /// Gets the included service.
493 /// <param name="uuid">The UUID for the service to get.</param>
494 /// <returns>The service with a given UUID if it exists, null otherwise.</returns>
495 /// <since_tizen> 3 </since_tizen>
496 public BluetoothGattService GetIncludeService(string uuid)
498 return _impl.GetIncludeService(this, uuid);
502 /// Gets the included service list of this service.
504 /// <returns>The included service list of this service.</returns>
505 /// <since_tizen> 3 </since_tizen>
506 public IEnumerable<BluetoothGattService> GetIncludeServices()
508 return _impl.GetIncludeServices(this);
512 /// Gets the server instance which the specified service belongs to.
514 /// <returns>The server instance which the specified service belongs to.</returns>
515 /// <since_tizen> 3 </since_tizen>
516 public BluetoothGattServer GetGattServer()
518 return _parentServer;
522 /// Gets the client instance which the specified service belongs to.
524 /// <returns>The client instance which the specified service belongs to.</returns>
525 /// <since_tizen> 3 </since_tizen>
526 public BluetoothGattClient GetGattClient()
528 return _parentClient;
531 internal BluetoothGattAttributeHandle GetHandle()
533 return _impl.GetHandle();
536 internal void SetParent(BluetoothGattService parent)
540 _parentService = parent;
541 _impl.ReleaseHandleOwnership();
545 internal void SetParent(BluetoothGattClient parent)
549 _parentClient = parent;
550 _impl.ReleaseHandleOwnership();
554 internal void SetParent(BluetoothGattServer parent)
558 _parentServer = parent;
559 _impl.ReleaseHandleOwnership();
563 internal void UnregisterService()
565 _parentServer = null;
566 _parentClient = null;
567 _parentService = null;
570 internal bool IsRegistered()
572 return _parentClient != null || _parentServer != null || _parentService != null;
577 /// The Bluetooth GATT characteristic.
579 /// <since_tizen> 3 </since_tizen>
580 public class BluetoothGattCharacteristic : BluetoothGattAttribute
582 private BluetoothGattCharacteristicImpl _impl;
583 private BluetoothGattService _parent = null;
585 private Interop.Bluetooth.BtClientCharacteristicValueChangedCallback _characteristicValueChangedCallback;
586 private Interop.Bluetooth.BtGattServerNotificationStateChangeCallback _notificationStateChangedCallback;
588 private EventHandler<ValueChangedEventArgs> _characteristicValueChanged;
589 internal EventHandler<NotificationStateChangedEventArg> _notificationStateChanged;
594 /// <param name="uuid">The UUID of the characterstic.</param>
595 /// <param name="permissions">Permissions for the characterstic.</param>
596 /// <param name="properties">Properties set for the characterstic.</param>
597 /// <param name="value">The value associated with the characterstic.</param>
598 /// <remarks>throws in case of internal error.</remarks>
599 /// <exception cref="InvalidOperationException">Thrown when the create GATT characteristics procedure fails.</exception>
600 /// <since_tizen> 3 </since_tizen>
601 public BluetoothGattCharacteristic(string uuid, BluetoothGattPermission permissions, BluetoothGattProperty properties, byte[] value) : base(uuid, permissions)
603 _impl = new BluetoothGattCharacteristicImpl(uuid, permissions, properties, value);
606 internal BluetoothGattCharacteristic(BluetoothGattCharacteristicImpl impl, string uuid, BluetoothGattPermission permission) : base(uuid, permission)
612 /// The CharacteristicValueChanged event is raised when the server notifies for change in this characteristic value.
615 /// Adding the event handle on characteristic on the server side will not have any effect.
617 /// <since_tizen> 3 </since_tizen>
618 public event EventHandler<ValueChangedEventArgs> ValueChanged
624 if (_characteristicValueChanged == null)
626 _characteristicValueChangedCallback = (gattHandle, characteristicValue, len, userData) =>
628 _characteristicValueChanged?.Invoke(this, new ValueChangedEventArgs(characteristicValue, len));
631 _impl.SetCharacteristicValueChangedEvent(_characteristicValueChangedCallback);
633 _characteristicValueChanged = value;
640 _characteristicValueChanged = null;
641 if (_characteristicValueChanged == null)
643 _impl.UnsetCharacteristicValueChangedEvent();
651 /// The NotificationStateChanged event is called when the client enables or disables the Notification/Indication for particular characteristics.
654 /// Adding event handle on the characteristic on the client side will not have any effect.
656 /// <since_tizen> 3 </since_tizen>
657 public event EventHandler<NotificationStateChangedEventArg> NotificationStateChanged
663 if (_notificationStateChangedCallback == null)
665 _notificationStateChangedCallback = (notify, serverHandle, characteristicHandle, userData) =>
667 _notificationStateChanged?.Invoke(this, new NotificationStateChangedEventArg(Server, notify));
670 _impl.SetNotificationStateChangedEvent(_notificationStateChangedCallback);
673 _notificationStateChanged = value;
680 _notificationStateChanged = null;
681 // CAPI does not allow unsetting ReadValueRequestedEventCallback.
687 /// The property for this characteristic.
689 /// <since_tizen> 3 </since_tizen>
690 public BluetoothGattProperty Properties
694 return _impl.GetProperties();
700 _impl.SetProperties(value);
706 /// The write type to be used for write operations.
708 /// <since_tizen> 3 </since_tizen>
709 public BluetoothGattWriteType WriteType
713 return _impl.GetWriteType();
717 _impl.SetWriteType(value);
721 internal override BluetoothGattClient Client
725 return _parent?.GetGattClient();
729 internal override BluetoothGattServer Server
733 return _parent?.GetGattServer();
737 internal override BluetoothGattAttributeImpl Impl
746 /// Adds a descriptor to this characteristic.
748 /// <param name="descriptor">The descriptor to be added.</param>
749 /// <returns>true on success, false otherwise.</returns>
750 /// <exception cref="InvalidOperationException">Thrown when the add GATT descriptor procedure fails.</exception>
751 /// <since_tizen> 3 </since_tizen>
752 public void AddDescriptor(BluetoothGattDescriptor descriptor)
756 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotSupported);
759 if (descriptor.GetCharacteristic() != null)
761 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
764 _impl.AddDescriptor(descriptor);
765 descriptor.SetParent(this);
769 /// Gets the descriptor with the given UUID that belongs to this characteristic.
771 /// <param name="uuid">The UUID for the descriptor to get.</param>
772 /// <returns>The descriptor with a given UUID if it exists, null otherwise.</returns>
773 /// <since_tizen> 3 </since_tizen>
774 public BluetoothGattDescriptor GetDescriptor(string uuid)
776 return _impl.GetDescriptor(this, uuid);
780 /// Gets the list of descriptors that belongs to this characteristic.
782 /// <returns>The list of descriptors that belongs to this characteristic.</returns>
783 /// <since_tizen> 3 </since_tizen>
784 public IEnumerable<BluetoothGattDescriptor> GetDescriptors()
786 return _impl.GetDescriptors(this);
790 /// Gets the service instance, which the specified characterstic belongs to.
792 /// <returns>The characteristic instance, the specified characterstic belongs to.</returns>
793 /// <since_tizen> 3 </since_tizen>
794 public BluetoothGattService GetService()
799 internal void SetParent(BluetoothGattService parent)
804 ReleaseHandleOwnership();
810 /// The Bluetooth GATT descriptor.
812 /// <since_tizen> 3 </since_tizen>
813 public class BluetoothGattDescriptor : BluetoothGattAttribute
815 private BluetoothGattCharacteristic _parent = null;
816 private BluetoothGattDescriptorImpl _impl;
821 /// <param name="uuid">The UUID of the descriptor.</param>
822 /// <param name="permisions">Permissions for the descriptor.</param>
823 /// <param name="value">The value associated with the descriptor.</param>
824 /// <remarks>throws in case of internal error.</remarks>
825 /// <exception cref="InvalidOperationException">Thrown when the create GATT descriptor procedure fails.</exception>
826 /// <since_tizen> 3 </since_tizen>
827 public BluetoothGattDescriptor(string uuid, BluetoothGattPermission permisions, byte[] value) : base (uuid, permisions)
829 _impl = new BluetoothGattDescriptorImpl(uuid, permisions, value);
832 internal BluetoothGattDescriptor(BluetoothGattDescriptorImpl impl, string uuid, BluetoothGattPermission permission) : base(uuid, permission)
837 internal override BluetoothGattClient Client
841 return _parent?.Client;
845 internal override BluetoothGattServer Server
849 return _parent?.Server;
853 internal override BluetoothGattAttributeImpl Impl
862 /// Gets the characteristic instance, which the specified descriptor belongs to.
864 /// <returns>The characteristic instance, the specified descriptor belongs to.</returns>
865 /// <since_tizen> 3 </since_tizen>
866 public BluetoothGattCharacteristic GetCharacteristic()
871 internal void SetParent(BluetoothGattCharacteristic parent)
876 ReleaseHandleOwnership();
882 /// The Bluetooth GATT attribute.
884 /// <since_tizen> 3 </since_tizen>
885 public abstract class BluetoothGattAttribute
887 private Interop.Bluetooth.BtGattServerReadValueRequestedCallback _readValueRequestedCallback;
888 private Interop.Bluetooth.BtGattServerWriteValueRequestedCallback _writeValueRequestedCallback;
890 private EventHandler<ReadRequestedEventArgs> _readValueRequested;
891 private EventHandler<WriteRequestedEventArgs> _writeValueRequested;
896 /// <param name="uuid">The UUID of the GATT attribute.</param>
897 /// <param name="permission">Permission for the GATT attribute.</param>
898 /// <since_tizen> 3 </since_tizen>
899 public BluetoothGattAttribute(string uuid, BluetoothGattPermission permission)
902 Permissions = permission;
908 /// This event is called when the client request to read the value of a characteristic or a descriptor.
910 /// <exception cref="InvalidOperationException">Thrown when the set read value requested callback procedure fails.</exception>
911 /// <since_tizen> 3 </since_tizen>
912 public event EventHandler<ReadRequestedEventArgs> ReadRequested
916 if (Server == null) return;
917 if (_readValueRequestedCallback == null)
919 _readValueRequestedCallback = (clientAddress, requestId, serverHandle, gattHandle, offset, userData) =>
921 _readValueRequested?.Invoke(this, new ReadRequestedEventArgs(Server, clientAddress, requestId, offset));
923 Impl.SetReadValueRequestedEventCallback(_readValueRequestedCallback);
925 _readValueRequested = value;
929 if (Server == null) return;
930 _readValueRequested = null;
931 // CAPI does not allow unsetting ReadValueRequestedEventCallback.
936 /// This event is called when a value of a characteristic or a descriptor has been changed by a client.
938 /// <exception cref="InvalidOperationException">Thrown when the set write value requested callback procedure fails.</exception>
939 /// <since_tizen> 3 </since_tizen>
940 public event EventHandler<WriteRequestedEventArgs> WriteRequested
944 if (Server == null) return;
945 if (_writeValueRequested == null)
947 _writeValueRequestedCallback = (clientAddress, requestId, serverHandle, gattHandle, response_needed, offset, valueToWrite, len, userData) =>
949 _writeValueRequested?.Invoke(this, new WriteRequestedEventArgs(Server, clientAddress, requestId, valueToWrite, offset, response_needed));
951 Impl.SetWriteValueRequestedEventCallback(_writeValueRequestedCallback);
953 _writeValueRequested = value;
957 if (Server == null) return;
958 _writeValueRequested = null;
959 // CAPI does not allow unsetting ReadValueRequestedEventCallback.
964 /// The attribute's UUID.
966 /// <since_tizen> 3 </since_tizen>
967 public string Uuid { get; }
970 /// Permissions for this attribute.
972 /// <since_tizen> 3 </since_tizen>
973 public BluetoothGattPermission Permissions { get; }
976 /// The value of this descriptor.
978 /// <since_tizen> 3 </since_tizen>
983 return Impl.GetValue();
987 Impl.SetValue(value);
991 internal abstract BluetoothGattClient Client { get; }
992 internal abstract BluetoothGattServer Server { get; }
993 internal abstract BluetoothGattAttributeImpl Impl { get; }
996 /// Returns a string value at the specified offset.
998 /// <param name="offset">An offset in the attribute value buffer.</param>
999 /// <returns>The string value at specified offset.</returns>
1000 /// <since_tizen> 3 </since_tizen>
1001 public string GetValue(int offset)
1003 return Impl.GetValue(offset);
1007 /// Sets the string value as a specified offset.
1009 /// <param name="value">value to set</param>
1010 /// <exception cref="InvalidOperationException">Throws exception if the value is null.</exception>
1011 /// <since_tizen> 3 </since_tizen>
1012 public void SetValue(string value)
1014 if (string.IsNullOrEmpty(value))
1015 GattUtil.ThrowForError((int)BluetoothError.InvalidParameter, "value should not be null");
1017 byte[] val = Encoding.UTF8.GetBytes(value);
1022 /// Returns a value at specified offset as the int value of the specified type.
1024 /// <param name="type">The type of the int value.</param>
1025 /// <param name="offset">An offset in the attribute value buffer.</param>
1026 /// <returns>The int value at given offset.</returns>
1027 /// <exception cref="InvalidOperationException">Throws exception if (offset + size of int value) is greater than the length of the value buffer.</exception>
1028 /// <since_tizen> 3 </since_tizen>
1029 public int GetValue(IntDataType type, int offset)
1031 return Impl.GetValue(type, offset);
1035 /// Updates a value at the specified offset by the int value of the specified type.
1037 /// <param name="type">The type of the int value.</param>
1038 /// <param name="value">The value to set.</param>
1039 /// <param name="offset">An offset in the attribute value buffer.</param>
1040 /// <exception cref="InvalidOperationException">Throws exception if (offset + size of int value) is greater than the length of the value buffer.</exception>
1041 /// <since_tizen> 3 </since_tizen>
1042 public void SetValue(IntDataType type, int value, int offset)
1044 Impl.SetValue(type, value, offset);
1048 /// Returns a value at the specified offset as the float value of the specified type.
1050 /// <param name="type">The type of the float value.</param>
1051 /// <param name="offset">An offset in the attribute value buffer.</param>
1052 /// <returns>The float value at given offset.</returns>
1053 /// <exception cref="InvalidOperationException">Throws exception if (offset + size of float value) is greater than the length of the value buffer.</exception>
1054 /// <since_tizen> 3 </since_tizen>
1055 public float GetValue(FloatDataType type, int offset)
1057 return Impl.GetValue(type, offset);
1061 /// Updates the value at the specified offset by the float value of the specified type.
1063 /// <param name="type">The type of the float value.</param>
1064 /// <param name="mantissa">The mantissa of the float value.</param>
1065 /// <param name="exponent">An exponent of the float value.</param>
1066 /// <param name="offset">An offset in the attribute value buffer.</param>
1067 /// <exception cref="InvalidOperationException">Throws exception if (offset + size of float value) is greater than the length of the value buffer.</exception>
1068 /// <since_tizen> 3 </since_tizen>
1069 public void SetValue(FloatDataType type, int mantissa, int exponent, int offset)
1071 Impl.SetValue(type, mantissa, exponent, offset);
1074 internal void ReleaseHandleOwnership()
1076 Impl.ReleaseHandleOwnership();
1079 internal BluetoothGattAttributeHandle GetHandle()
1081 return Impl.GetHandle();