/
BluetoothGatt.cs
1346 lines (1221 loc) · 54.3 KB
/
BluetoothGatt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Tizen.Network.Bluetooth
{
/// <summary>
/// The Bluetooth GATT server.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class BluetoothGattServer : IDisposable
{
private static BluetoothGattServer _instance;
private BluetoothGattServerImpl _impl;
private BluetoothGattServer()
{
_impl = new BluetoothGattServerImpl();
_impl._notificationSent += (s, e) =>
{
e.Server = this;
NotificationSent?.Invoke(this, e);
};
_impl.AttMtuChanged += OnAttMtuChanged;
}
private void OnAttMtuChanged(object s, AttMtuChangedEventArgs e)
{
AttMtuChanged?.Invoke(this, e);
}
/// <summary>
/// (event) This event is called when the indication acknowledgement is received for each notified client.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public event EventHandler<NotificationSentEventArg> NotificationSent;
/// <summary>
/// Creates the Bluetooth GATT server.
/// </summary>
/// <returns>The BluetoothGattServer instance.</returns>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the create GATT server fails.</exception>
/// <since_tizen> 3 </since_tizen>
public static BluetoothGattServer CreateServer()
{
if (_instance == null)
{
BluetoothGattServer server = new BluetoothGattServer();
if (server.IsValid())
{
_instance = server;
}
}
return _instance;
}
/// <summary>
/// Registers the server along with the GATT services of the application it is hosting.
/// </summary>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the register server application fails.</exception>
/// <since_tizen> 3 </since_tizen>
public void Start()
{
_impl.Start();
}
/// <summary>
/// Registers a specified service to this server.
/// </summary>
/// <param name="service">The service, which needs to be registered with this server.</param>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the register service fails.</exception>
/// <since_tizen> 3 </since_tizen>
public void RegisterGattService(BluetoothGattService service)
{
if (service.IsRegistered())
{
BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
}
_impl.RegisterGattService(this, service);
}
/// <summary>
/// Unregisters a specified service from this server.
/// </summary>
/// <param name="service">The service, which needs to be unregistered from this server.</param>
/// <remarks>
/// Once unregistered, the service object will become invalid and should not be used to access sevices or any children attribute's methods/members.
/// </remarks>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the unregister service fails.</exception>
/// <since_tizen> 3 </since_tizen>
public void UnregisterGattService(BluetoothGattService service)
{
if (service.GetGattServer() != this)
{
BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
}
_impl.UnregisterGattService(service);
}
/// <summary>
/// Unregisters all services from this server.
/// </summary>
/// <remarks>
/// Once unregistered, servicees will become invalid and should not be used to access sevices or any children attribute's methods/members.
/// </remarks>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the unregister all services fail.</exception>
/// <since_tizen> 3 </since_tizen>
public void UnregisterGattServices()
{
_impl.UnregisterAllGattServices(this);
}
/// <summary>
/// Gets service with given UUID that belongs to this server.
/// </summary>
/// <param name="uuid">The UUID for the service to get.</param>
/// <returns>The Service with the given UUID if it exists, null otherwise.</returns>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the service is not registered.</exception>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattService GetService(string uuid)
{
return _impl.GetService(this, uuid);
}
/// <summary>
/// Gets the list of services that belongs to this server.
/// </summary>
/// <returns>The list of services that belongs to this server.</returns>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the service is not registered.</exception>
/// <since_tizen> 3 </since_tizen>
public IEnumerable<BluetoothGattService> GetServices()
{
return _impl.GetServices(this);
}
/// <summary>
/// Sends indication for the value change of the characteristic to the remote devices.
/// </summary>
/// <param name="characteristic">The characteristic whose the value is changed.</param>
/// <param name="clientAddress">The remote device address to send, notify, or indicate and if set to NULL, then notify/indicate all is enabled.</param>
/// <returns>true on success, false otherwise.</returns>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected, or when service is not registered, or when the CCCD is not enabled.</exception>
/// <since_tizen> 3 </since_tizen>
public async Task<bool> SendIndicationAsync(BluetoothGattCharacteristic characteristic, string clientAddress)
{
return await _impl.SendIndicationAsync(this, characteristic, clientAddress);
}
/// <summary>
/// Sends the notification for the value change of the characteristic to the remote devices.
/// </summary>
/// <param name="characteristic">The characteristic, which has a changed value.</param>
/// <param name="clientAddress">The remote device address to send, notify, or indicate and if set to NULL, then notify/indicate all is enabled.</param>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected, or when service is not registered, or when the CCCD is not enabled.</exception>
/// <since_tizen> 3 </since_tizen>
public void SendNotification(BluetoothGattCharacteristic characteristic, string clientAddress)
{
_ = _impl.SendIndicationAsync(this, characteristic, clientAddress);
}
/// <summary>
/// Sends a response to the remote device as a result of a read/write request.
/// </summary>
/// <param name="requestId">The identification of a read/write request.</param>
/// <param name="type">The request type for read/write.</param>
/// <param name="status">The error value in case of failure, 0 for success.</param>
/// <param name="value">The value to be sent.</param>
/// <param name="offset">The offset from where the value is read.</param>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected, or the send response procedure fails.</exception>
/// <since_tizen> 3 </since_tizen>
public void SendResponse(int requestId, BluetoothGattRequestType type, int status, byte[] value, int offset)
{
_impl.SendResponse(requestId, (int)type, status, value, offset);
}
/// <summary>
/// Gets the value of the ATT MTU(Maximum Transmission Unit) for the connection.
/// </summary>
/// <param name="clientAddress">The remote device address.</param>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <returns>The MTU value</returns>
/// <exception cref="NotSupportedException">Thrown when the BT/BLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the BT/BLE is not enabled
/// or when the remote device is disconnected, or when other specific error occurs.</exception>
/// <since_tizen> 9 </since_tizen>
public int GetAttMtu(string clientAddress)
{
return _impl.GetAttMtu(clientAddress);
}
/// <summary>
/// The AttMtuChanged event is raised when the MTU value changed.
/// </summary>
/// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler<AttMtuChangedEventArgs> AttMtuChanged;
internal bool IsValid()
{
return _impl?.GetHandle()?.IsInvalid == false;
}
/// <summary>
/// Destroys the current object.
/// </summary>
~BluetoothGattServer()
{
Dispose(false);
}
/// <summary>
/// Destroys the current object.
/// </summary>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
/// <since_tizen> 6 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases all the resources currently used by this instance.
/// </summary>
/// <param name="disposing">true if the managed resources should be disposed, otherwise false.</param>
/// <since_tizen> 6 </since_tizen>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_impl?.GetHandle()?.Dispose();
_instance = null;
}
}
}
/// <summary>
/// The Bluetooth GATT client.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class BluetoothGattClient : IDisposable
{
private BluetoothGattClientImpl _impl;
private string _remoteAddress = string.Empty;
private TaskCompletionSource<bool> _taskForConnection;
private TaskCompletionSource<bool> _taskForDisconnection;
private static event EventHandler<GattConnectionStateChangedEventArgs> s_connectionStateChanged;
private static Interop.Bluetooth.GattConnectionStateChangedCallBack s_connectionStateChangeCallback;
internal BluetoothGattClient(string remoteAddress)
{
_impl = new BluetoothGattClientImpl(remoteAddress);
_remoteAddress = remoteAddress;
StaticConnectionStateChanged += OnConnectionStateChanged;
_impl.AttMtuChanged += OnAttMtuChanged;
_impl.ServiceChanged += OnServiceChanged;
}
private void OnAttMtuChanged(object s, AttMtuChangedEventArgs e)
{
AttMtuChanged?.Invoke(this, e);
}
private void OnServiceChanged(object s, ServiceChangedEventArgs e)
{
ServiceChanged?.Invoke(this, e);
}
/// <summary>
/// Creates the Bluetooth GATT client.
/// </summary>
/// <param name="remoteAddress">The remote device address.</param>
/// <returns>The BluetoothGattClient instance.</returns>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.client</feature>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the create GATT client fails.</exception>
/// <since_tizen> 6 </since_tizen>
public static BluetoothGattClient CreateClient(string remoteAddress)
{
BluetoothGattClient client = new BluetoothGattClient(remoteAddress);
return client.Isvalid() ? client : null;
}
/// <summary>
/// The ConnectionStateChanged event is raised when the gatt connection state is changed.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public event EventHandler<GattConnectionStateChangedEventArgs> ConnectionStateChanged;
private void OnConnectionStateChanged(Object s, GattConnectionStateChangedEventArgs e)
{
if (e.RemoteAddress == _remoteAddress)
{
if (_taskForConnection != null && !_taskForConnection.Task.IsCompleted)
{
if (e.Result == (int)BluetoothError.None)
{
_taskForConnection.SetResult(true);
}
else
{
_taskForConnection.SetException(BluetoothErrorFactory.CreateBluetoothException((int)e.Result));
}
_taskForConnection = null;
}
if (_taskForDisconnection != null && !_taskForDisconnection.Task.IsCompleted)
{
if (e.Result == (int)BluetoothError.None)
{
_taskForDisconnection.SetResult(true);
}
else
{
_taskForDisconnection.SetException(BluetoothErrorFactory.CreateBluetoothException(e.Result));
}
_taskForDisconnection = null;
}
ConnectionStateChanged?.Invoke(this, e);
}
}
internal static event EventHandler<GattConnectionStateChangedEventArgs> StaticConnectionStateChanged
{
add
{
if (s_connectionStateChanged == null)
{
RegisterConnectionStateChangedEvent();
}
s_connectionStateChanged += value;
}
remove
{
s_connectionStateChanged -= value;
if (s_connectionStateChanged == null)
{
UnregisterConnectionStateChangedEvent();
}
}
}
private static void RegisterConnectionStateChangedEvent()
{
s_connectionStateChangeCallback = (int result, bool connected, string remoteDeviceAddress, IntPtr userData) =>
{
Log.Info(Globals.LogTag, "Setting gatt connection state changed callback");
GattConnectionStateChangedEventArgs e = new GattConnectionStateChangedEventArgs(result, connected, remoteDeviceAddress);
s_connectionStateChanged?.Invoke(null, e);
};
int ret = Interop.Bluetooth.SetGattConnectionStateChangedCallback(s_connectionStateChangeCallback, IntPtr.Zero);
if (ret != (int)BluetoothError.None)
{
Log.Error(Globals.LogTag, "Failed to set gatt connection state changed callback, Error - " + (BluetoothError)ret);
}
}
private static void UnregisterConnectionStateChangedEvent()
{
int ret = Interop.Bluetooth.UnsetGattConnectionStateChangedCallback();
if (ret != (int)BluetoothError.None)
{
Log.Error(Globals.LogTag, "Failed to unset gatt connection state changed callback, Error - " + (BluetoothError)ret);
}
}
/// <summary>
/// Connects to the remote GATT server asynchronously.
/// </summary>
/// <param name="autoConnect">The flag for reconnecting when the connection is disconnceted.</param>
/// <returns> A task indicating whether the method is done or not.</returns>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.client</feature>
/// <privilege>http://tizen.org/privilege/bluetooth</privilege>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the create GATT client fails.</exception>
/// <since_tizen> 6 </since_tizen>
public Task ConnectAsync(bool autoConnect)
{
if (_taskForConnection != null && !_taskForConnection.Task.IsCompleted)
{
BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NowInProgress);
}
_taskForConnection = new TaskCompletionSource<bool>();
_impl.Connect(_remoteAddress, autoConnect);
return _taskForConnection.Task;
}
/// <summary>
/// Disconnects to the remote GATT server asynchronously.
/// </summary>
/// <returns> A task indicating whether the method is done or not.</returns>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.client</feature>
/// <privilege>http://tizen.org/privilege/bluetooth</privilege>
/// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the create GATT client fails.</exception>
/// <since_tizen> 6 </since_tizen>
public Task DisconnectAsync()
{
if (_taskForDisconnection != null && !_taskForDisconnection.Task.IsCompleted)
{
BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NowInProgress);
}
_taskForDisconnection = new TaskCompletionSource<bool>();
_impl.Disconnect(_remoteAddress);
return _taskForDisconnection.Task;
}
/// <summary>
/// Destroy Bluetooth GATT client
/// </summary>
/// <since_tizen> 3 </since_tizen>
[Obsolete("Deprecated since API level 6. Please use Dispose() method on BluetoothGattClient.")]
public void DestroyClient()
{
_impl?.GetHandle()?.Dispose();
}
/// <summary>
/// The address of the remote device.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected.</exception>
/// <since_tizen> 3 </since_tizen>
public string RemoteAddress
{
get
{
if (string.IsNullOrEmpty(_remoteAddress))
{
_remoteAddress = _impl.GetRemoteAddress();
}
return _remoteAddress;
}
}
/// <summary>
/// Gets the service with the given UUID that belongs to the remote device.
/// </summary>
/// <param name="uuid">The UUID for the service to get.</param>
/// <returns>The service with the given UUID if it exists, null otherwise.</returns>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected, or when the get service fails.</exception>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattService GetService(string uuid)
{
return _impl.GetService(this, uuid);
}
/// <summary>
/// Gets list of services that belongs to the remote device.
/// </summary>
/// <returns>The list of services that belongs to the remote device.</returns>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected, or when the get service fails.</exception>
/// <since_tizen> 3 </since_tizen>
public IEnumerable<BluetoothGattService> GetServices()
{
return _impl.GetServices(this);
}
/// <summary>
/// Reads the value of a given characteristic from the remote device asynchronously.
/// </summary>
/// <param name="characteristic">The characteristic to be read.</param>
/// <returns>true on success, false otherwise.</returns>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected, or when the read attribute value fails.</exception>
/// <since_tizen> 3 </since_tizen>
public async Task<bool> ReadValueAsync(BluetoothGattCharacteristic characteristic)
{
return await _impl.ReadValueAsyncTask(characteristic.GetHandle());
}
/// <summary>
/// Reads the value of the given descriptor from the remote device asynchronously.
/// </summary>
/// <param name="descriptor">The descriptor to be read.</param>
/// <returns>true on success, false otherwise.</returns>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected, or when the read attribute value fails.</exception>
/// <since_tizen> 3 </since_tizen>
public async Task<bool> ReadValueAsync(BluetoothGattDescriptor descriptor)
{
return await _impl.ReadValueAsyncTask(descriptor.GetHandle());
}
/// <summary>
/// Writes the value of a given characteristic to the remote device asynchronously.
/// </summary>
/// <param name="characteristic">The characteristic to be written.</param>
/// <returns>true on success, false otherwise.</returns>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected or when the write attribute value fails.</exception>
/// <since_tizen> 3 </since_tizen>
public async Task<bool> WriteValueAsync(BluetoothGattCharacteristic characteristic)
{
return await _impl.WriteValueAsyncTask(characteristic.GetHandle());
}
/// <summary>
/// Writes the value of the given descriptor to the remote device asynchronously.
/// </summary>
/// <param name="descriptor">The descriptor to be written.</param>
/// <returns>true on success, false otherwise.</returns>
/// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled
/// or when the remote device is disconnected, or when the write attribute value fails.</exception>
/// <since_tizen> 3 </since_tizen>
public async Task<bool> WriteValueAsync(BluetoothGattDescriptor descriptor)
{
return await _impl.WriteValueAsyncTask(descriptor.GetHandle());
}
/// <summary>
/// Gets the value of the ATT MTU(Maximum Transmission Unit) for the connection.
/// </summary>
/// <returns>The MTU value</returns>
/// <exception cref="NotSupportedException">Thrown when the BT/BLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the BT/BLE is not enabled
/// or when the remote device is disconnected, or when other specific error occurs.</exception>
/// <since_tizen> 8 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public int GetAttMtu()
{
return _impl.GetAttMtu();
}
/// <summary>
/// Sets the value of the ATT MTU(Maximum Transmission Unit) for the connection.
/// </summary>
/// <param name="mtu">The MTU value</param>
/// <exception cref="NotSupportedException">Thrown when the BT/BLE is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the BT/BLE is not enabled
/// or when the remote device is disconnected, or when other specific error occurs.</exception>
/// <since_tizen> 8 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetAttMtu(int mtu)
{
_impl.SetAttMtu(mtu);
}
/// <summary>
/// The AttMtuChanged event is raised when the MTU value changed.
/// </summary>
/// <since_tizen> 8 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler<AttMtuChangedEventArgs> AttMtuChanged;
/// <summary>
/// The ServiceChanged event is raised when the service is changed from the remote device(GATT server).
/// </summary>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.client</feature>
/// <since_tizen> 9 </since_tizen>
public event EventHandler<ServiceChangedEventArgs> ServiceChanged;
internal bool Isvalid()
{
return _impl?.GetHandle()?.IsInvalid == false;
}
/// <summary>
/// Destroys the current object.
/// </summary>
~BluetoothGattClient()
{
Dispose(false);
}
/// <summary>
/// Destroys the current object.
/// </summary>
/// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.client</feature>
/// <since_tizen> 6 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases all the resources currently used by this instance.
/// </summary>
/// <param name="disposing">true if the managed resources should be disposed, otherwise false.</param>
/// <since_tizen> 6 </since_tizen>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_impl?.GetHandle()?.Dispose();
_impl = null;
StaticConnectionStateChanged -= OnConnectionStateChanged;
}
}
}
/// <summary>
/// The Bluetooth GATT service.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class BluetoothGattService
{
private BluetoothGattServiceImpl _impl;
private BluetoothGattClient _parentClient = null;
private BluetoothGattServer _parentServer = null;
private BluetoothGattService _parentService = null;
/// <summary>
/// The constructor.
/// </summary>
/// <param name="uuid">The UUID of the service.</param>
/// <param name="type">The type of service.</param>
/// <exception cref="InvalidOperationException">Thrown when the create GATT service procedure fails.</exception>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattService(string uuid, BluetoothGattServiceType type)
{
Uuid = uuid;
_impl = new BluetoothGattServiceImpl(uuid, type);
}
internal BluetoothGattService(BluetoothGattServiceImpl impl, string uuid)
{
Uuid = uuid;
_impl = impl;
}
/// <summary>
/// Specification name from the UUID.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public string Uuid { get; }
/// <summary>
/// Adds a characteristic to this service.
/// </summary>
/// <param name="characteristic">The characteristic to be added.</param>
/// <returns>true on success, false otherwise.</returns>
/// <exception cref="InvalidOperationException">Thrown when the add GATT characteristic procedure fails.</exception>
/// <since_tizen> 3 </since_tizen>
public void AddCharacteristic(BluetoothGattCharacteristic characteristic)
{
if (GetGattClient() != null)
{
BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotSupported);
}
if (characteristic.GetService() != null)
{
BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
}
_impl.AddCharacteristic(characteristic);
characteristic.SetParent(this);
}
/// <summary>
/// Gets the characteristic with the given UUID that belongs to this service.
/// </summary>
/// <param name="uuid">The UUID for the characteristic to get.</param>
/// <returns>The characteristic with a given UUID if it exists, null otherwise.</returns>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattCharacteristic GetCharacteristic(string uuid)
{
return _impl.GetCharacteristic(this, uuid);
}
/// <summary>
/// Gets list of the characteristic that belongs to this service.
/// </summary>
/// <returns>The list of the characteristic that belongs to this service.</returns>
/// <since_tizen> 3 </since_tizen>
public IEnumerable<BluetoothGattCharacteristic> GetCharacteristics()
{
return _impl.GetCharacteristics(this);
}
/// <summary>
/// Includes a service to this service.
/// </summary>
/// <param name="service">The service to be included.</param>
/// <returns>true on success, false otherwise</returns>
/// <exception cref="InvalidOperationException">Thrown when the add GATT service procedure fails.</exception>///
/// <since_tizen> 3 </since_tizen>
public void AddService(BluetoothGattService service)
{
if (GetGattClient() != null)
{
BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotSupported);
}
if (service.IsRegistered())
{
BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter);
}
_impl.AddIncludeService(service);
service.SetParent(this);
}
/// <summary>
/// Gets the included service.
/// </summary>
/// <param name="uuid">The UUID for the service to get.</param>
/// <returns>The service with a given UUID if it exists, null otherwise.</returns>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattService GetIncludeService(string uuid)
{
return _impl.GetIncludeService(this, uuid);
}
/// <summary>
/// Gets the included service list of this service.
/// </summary>
/// <returns>The included service list of this service.</returns>
/// <since_tizen> 3 </since_tizen>
public IEnumerable<BluetoothGattService> GetIncludeServices()
{
return _impl.GetIncludeServices(this);
}
/// <summary>
/// Gets the server instance which the specified service belongs to.
/// </summary>
/// <returns>The server instance which the specified service belongs to.</returns>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattServer GetGattServer()
{
return _parentServer;
}
/// <summary>
/// Gets the client instance which the specified service belongs to.
/// </summary>
/// <returns>The client instance which the specified service belongs to.</returns>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattClient GetGattClient()
{
return _parentClient;
}
internal BluetoothGattAttributeHandle GetHandle()
{
return _impl.GetHandle();
}
internal void SetParent(BluetoothGattService parent)
{
if (!IsRegistered())
{
_parentService = parent;
_impl.ReleaseHandleOwnership();
}
}
internal void SetParent(BluetoothGattClient parent)
{
if (!IsRegistered())
{
_parentClient = parent;
_impl.ReleaseHandleOwnership();
}
}
internal void SetParent(BluetoothGattServer parent)
{
if (!IsRegistered())
{
_parentServer = parent;
_impl.ReleaseHandleOwnership();
}
}
internal void UnregisterService()
{
_parentServer = null;
_parentClient = null;
_parentService = null;
}
internal bool IsRegistered()
{
return _parentClient != null || _parentServer != null || _parentService != null;
}
}
/// <summary>
/// The Bluetooth GATT characteristic.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class BluetoothGattCharacteristic : BluetoothGattAttribute
{
private BluetoothGattCharacteristicImpl _impl;
private BluetoothGattService _parent = null;
private Interop.Bluetooth.BtClientCharacteristicValueChangedCallback _characteristicValueChangedCallback;
private Interop.Bluetooth.BtGattServerNotificationStateChangeCallback _notificationStateChangedCallback;
private EventHandler<ValueChangedEventArgs> _characteristicValueChanged;
internal EventHandler<NotificationStateChangedEventArg> _notificationStateChanged;
/// <summary>
/// The constructor.
/// </summary>
/// <param name="uuid">The UUID of the characterstic.</param>
/// <param name="permissions">Permissions for the characterstic.</param>
/// <param name="properties">Properties set for the characterstic.</param>
/// <param name="value">The value associated with the characterstic.</param>
/// <remarks>throws in case of internal error.</remarks>
/// <exception cref="InvalidOperationException">Thrown when the create GATT characteristics procedure fails.</exception>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattCharacteristic(string uuid, BluetoothGattPermission permissions, BluetoothGattProperty properties, byte[] value) : base(uuid, permissions)
{
_impl = new BluetoothGattCharacteristicImpl(uuid, permissions, properties, value);
}
internal BluetoothGattCharacteristic(BluetoothGattCharacteristicImpl impl, string uuid, BluetoothGattPermission permission) : base(uuid, permission)
{
_impl = impl;
}
/// <summary>
/// The CharacteristicValueChanged event is raised when the server notifies for change in this characteristic value.
/// </summary>
/// <remarks>
/// Adding the event handle on characteristic on the server side will not have any effect.
/// </remarks>
/// <since_tizen> 3 </since_tizen>
public event EventHandler<ValueChangedEventArgs> ValueChanged
{
add
{
if (Client != null)
{
if (_characteristicValueChanged == null)
{
_characteristicValueChangedCallback = (gattHandle, characteristicValue, len, userData) =>
{
_characteristicValueChanged?.Invoke(this, new ValueChangedEventArgs(characteristicValue, len));
};
_impl.SetCharacteristicValueChangedEvent(_characteristicValueChangedCallback);
}
_characteristicValueChanged = value;
}
}
remove
{
if (Client != null)
{
_characteristicValueChanged = null;
if (_characteristicValueChanged == null)
{
_impl.UnsetCharacteristicValueChangedEvent();
}
}
}
}
/// <summary>
/// The NotificationStateChanged event is called when the client enables or disables the Notification/Indication for particular characteristics.
/// </summary>
/// <remarks>
/// BluetoothGattServer.RegisterGattService() should be called before adding/removing this EventHandler.
/// Adding event handle on the characteristic on the client side will not have any effect.
/// </remarks>
/// <since_tizen> 3 </since_tizen>
public event EventHandler<NotificationStateChangedEventArg> NotificationStateChanged
{
add
{
if (Server != null)
{
if (_notificationStateChangedCallback == null)
{
_notificationStateChangedCallback = (notify, serverHandle, characteristicHandle, userData) =>
{
_notificationStateChanged?.Invoke(this, new NotificationStateChangedEventArg(Server, notify));
};
_impl.SetNotificationStateChangedEvent(_notificationStateChangedCallback);
}
_notificationStateChanged = value;
}
}
remove
{
if (Server != null)
{
_notificationStateChanged = null;
// CAPI does not allow unsetting ReadValueRequestedEventCallback.
}
}
}
/// <summary>
/// The property for this characteristic.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattProperty Properties
{
get
{
return _impl.GetProperties();
}
set
{
if (Server != null)
{
_impl.SetProperties(value);
}
}
}
/// <summary>
/// The write type to be used for write operations.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public BluetoothGattWriteType WriteType
{
get
{
return _impl.GetWriteType();
}
set
{
_impl.SetWriteType(value);
}
}
internal override BluetoothGattClient Client
{
get
{
return _parent?.GetGattClient();
}
}
internal override BluetoothGattServer Server
{
get
{
return _parent?.GetGattServer();
}
}
internal override BluetoothGattAttributeImpl Impl
{
get
{
return _impl;
}
}
/// <summary>
/// Adds a descriptor to this characteristic.