Select one of the symbols to view example projects that use it.
 
Outline
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
#include "esp_gatt_common_api.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#define GATTC_TAG
#define REMOTE_SERVICE_UUID
#define REMOTE_NOTIFY_CHAR_UUID
#define PROFILE_NUM
#define PROFILE_A_APP_ID
#define PROFILE_B_APP_ID
#define PROFILE_C_APP_ID
#define INVALID_HANDLE
remote_filter_service_uuid
remote_filter_char_uuid
notify_descr_uuid
conn_device_a
conn_device_b
conn_device_c
get_service_a
get_service_b
get_service_c
Isconnecting
stop_scan_done
char_elem_result_a
descr_elem_result_a
char_elem_result_b
descr_elem_result_b
char_elem_result_c
descr_elem_result_c
remote_device_name
ble_scan_params
gattc_profile_inst
gl_profile_tab
start_scan()
gattc_profile_a_event_handler(esp_gattc_cb_event_t, esp_gatt_if_t, esp_ble_gattc_cb_param_t *)
gattc_profile_b_event_handler(esp_gattc_cb_event_t, esp_gatt_if_t, esp_ble_gattc_cb_param_t *)
gattc_profile_c_event_handler(esp_gattc_cb_event_t, esp_gatt_if_t, esp_ble_gattc_cb_param_t *)
esp_gap_cb(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t *)
esp_gattc_cb(esp_gattc_cb_event_t, esp_gatt_if_t, esp_ble_gattc_cb_param_t *)
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplesgattc_multi_connect samplemain/gattc_multi_connect.c
 
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
1001
1002
1003
1004
1005
1006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ /**************************************************************************** * * This file is for gatt client. It can scan ble device, connect multiple devices, * The gattc_multi_connect demo can connect three ble slaves at the same time. * Modify the name of gatt_server demo named ESP_GATTS_DEMO_a, * ESP_GATTS_DEMO_b and ESP_GATTS_DEMO_c,then run three demos,the gattc_multi_connect demo will connect * the three gatt_server demos, and then exchange data. * Of course you can also modify the code to connect more devices, we default to connect * up to 4 devices, more than 4 you need to modify menuconfig. * ****************************************************************************//* ... */ #include <stdint.h> #include <string.h> #include <stdbool.h> #include <stdio.h> #include "nvs.h" #include "nvs_flash.h" #include "esp_bt.h" #include "esp_gap_ble_api.h" #include "esp_gattc_api.h" #include "esp_gatt_defs.h" #include "esp_bt_main.h" #include "esp_gatt_common_api.h" #include "esp_log.h" #include "freertos/FreeRTOS.h"14 includes #define GATTC_TAG "GATTC_MULTIPLE_DEMO" #define REMOTE_SERVICE_UUID 0x00FF #define REMOTE_NOTIFY_CHAR_UUID 0xFF01 /* register three profiles, each profile corresponds to one connection, which makes it easy to handle each connection event *//* ... */ #define PROFILE_NUM 3 #define PROFILE_A_APP_ID 0 #define PROFILE_B_APP_ID 1 #define PROFILE_C_APP_ID 2 #define INVALID_HANDLE 08 defines /* Declare static functions */ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param); static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param); static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param); static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param); static esp_bt_uuid_t remote_filter_service_uuid = { .len = ESP_UUID_LEN_16, .uuid = {.uuid16 = REMOTE_SERVICE_UUID,}, }{...}; static esp_bt_uuid_t remote_filter_char_uuid = { .len = ESP_UUID_LEN_16, .uuid = {.uuid16 = REMOTE_NOTIFY_CHAR_UUID,}, }{...}; static esp_bt_uuid_t notify_descr_uuid = { .len = ESP_UUID_LEN_16, .uuid = {.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG,}, }{...}; static bool conn_device_a = false; static bool conn_device_b = false; static bool conn_device_c = false; static bool get_service_a = false; static bool get_service_b = false; static bool get_service_c = false; static bool Isconnecting = false; static bool stop_scan_done = false; static esp_gattc_char_elem_t *char_elem_result_a = NULL; static esp_gattc_descr_elem_t *descr_elem_result_a = NULL; static esp_gattc_char_elem_t *char_elem_result_b = NULL; static esp_gattc_descr_elem_t *descr_elem_result_b = NULL; static esp_gattc_char_elem_t *char_elem_result_c = NULL; static esp_gattc_descr_elem_t *descr_elem_result_c = NULL; static const char remote_device_name[3][20] = {"ESP_GATTS_DEMO_a", "ESP_GATTS_DEMO_b", "ESP_GATTS_DEMO_c"}; static esp_ble_scan_params_t ble_scan_params = { .scan_type = BLE_SCAN_TYPE_ACTIVE, .own_addr_type = BLE_ADDR_TYPE_PUBLIC, .scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL, .scan_interval = 0x50, .scan_window = 0x30, .scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE }{...}; struct gattc_profile_inst { esp_gattc_cb_t gattc_cb; uint16_t gattc_if; uint16_t app_id; uint16_t conn_id; uint16_t service_start_handle; uint16_t service_end_handle; uint16_t char_handle; esp_bd_addr_t remote_bda; }{ ... }; /* One gatt-based profile one app_id and one gattc_if, this array will store the gattc_if returned by ESP_GATTS_REG_EVT */ static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = { [PROFILE_A_APP_ID] = { .gattc_cb = gattc_profile_a_event_handler, .gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */ }{...}, [PROFILE_B_APP_ID] = { .gattc_cb = gattc_profile_b_event_handler, .gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */ }{...}, [PROFILE_C_APP_ID] = { .gattc_cb = gattc_profile_c_event_handler, .gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */ }{...}, }{...}; static void start_scan(void) { stop_scan_done = false; Isconnecting = false; uint32_t duration = 30; esp_ble_gap_start_scanning(duration); }{ ... } static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) { esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param; switch (event) { case ESP_GATTC_REG_EVT: ESP_LOGI(GATTC_TAG, "REG_EVT"); esp_err_t scan_ret = esp_ble_gap_set_scan_params(&ble_scan_params); if (scan_ret){ ESP_LOGE(GATTC_TAG, "set scan params error, error code = %x", scan_ret); }{...} break; /* one device connect successfully, all profiles callback function will get the ESP_GATTC_CONNECT_EVT, so must compare the mac address to check which device is connected, so it is a good choice to use ESP_GATTC_OPEN_EVT. *//* ... */ ... case ESP_GATTC_CONNECT_EVT: break;... case ESP_GATTC_OPEN_EVT: if (p_data->open.status != ESP_GATT_OK){ //open failed, ignore the first device, connect the second device ESP_LOGE(GATTC_TAG, "connect device failed, status %d", p_data->open.status); conn_device_a = false; //start_scan(); break; }{...} memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->open.remote_bda, 6); gl_profile_tab[PROFILE_A_APP_ID].conn_id = p_data->open.conn_id; ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu); ESP_LOGI(GATTC_TAG, "REMOTE BDA:"); ESP_LOG_BUFFER_HEX(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t)); esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id); if (mtu_ret){ ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret); }{...} break;... case ESP_GATTC_CFG_MTU_EVT: if (param->cfg_mtu.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG,"Config mtu failed"); }{...} ESP_LOGI(GATTC_TAG, "Status %d, MTU %d, conn_id %d", param->cfg_mtu.status, param->cfg_mtu.mtu, param->cfg_mtu.conn_id); esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, &remote_filter_service_uuid); break;... case ESP_GATTC_SEARCH_RES_EVT: { ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x is primary service %d", p_data->search_res.conn_id, p_data->search_res.is_primary); ESP_LOGI(GATTC_TAG, "start handle %d end handle %d current handle value %d", p_data->search_res.start_handle, p_data->search_res.end_handle, p_data->search_res.srvc_id.inst_id); if (p_data->search_res.srvc_id.uuid.len == ESP_UUID_LEN_16 && p_data->search_res.srvc_id.uuid.uuid.uuid16 == REMOTE_SERVICE_UUID) { ESP_LOGI(GATTC_TAG, "UUID16: %x", p_data->search_res.srvc_id.uuid.uuid.uuid16); get_service_a = true; gl_profile_tab[PROFILE_A_APP_ID].service_start_handle = p_data->search_res.start_handle; gl_profile_tab[PROFILE_A_APP_ID].service_end_handle = p_data->search_res.end_handle; }{...} break; }{...} ... case ESP_GATTC_SEARCH_CMPL_EVT: if (p_data->search_cmpl.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "search service failed, error status = %x", p_data->search_cmpl.status); break; }{...} if (get_service_a){ uint16_t count = 0; esp_gatt_status_t status = esp_ble_gattc_get_attr_count( gattc_if, p_data->search_cmpl.conn_id, ESP_GATT_DB_CHARACTERISTIC, gl_profile_tab[PROFILE_A_APP_ID].service_start_handle, gl_profile_tab[PROFILE_A_APP_ID].service_end_handle, INVALID_HANDLE, &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); break; }{...} if (count > 0) { char_elem_result_a = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result_a){ ESP_LOGE(GATTC_TAG, "gattc no mem"); break; }{...}else { status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, gl_profile_tab[PROFILE_A_APP_ID].service_start_handle, gl_profile_tab[PROFILE_A_APP_ID].service_end_handle, remote_filter_char_uuid, char_elem_result_a, &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error"); free(char_elem_result_a); char_elem_result_a = NULL; break; }{...} /* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */ if (count > 0 && (char_elem_result_a[0].properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)){ gl_profile_tab[PROFILE_A_APP_ID].char_handle = char_elem_result_a[0].char_handle; esp_ble_gattc_register_for_notify (gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, char_elem_result_a[0].char_handle); }{...} } /* free char_elem_result */ free(char_elem_result_a); char_elem_result_a = NULL; }{...}else { ESP_LOGE(GATTC_TAG, "no char found"); } }{...} break;... case ESP_GATTC_REG_FOR_NOTIFY_EVT: { if (p_data->reg_for_notify.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "reg notify failed, error status =%x", p_data->reg_for_notify.status); break; }{...} uint16_t count = 0; uint16_t notify_en = 1; esp_gatt_status_t ret_status = esp_ble_gattc_get_attr_count( gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, ESP_GATT_DB_DESCRIPTOR, gl_profile_tab[PROFILE_A_APP_ID].service_start_handle, gl_profile_tab[PROFILE_A_APP_ID].service_end_handle, gl_profile_tab[PROFILE_A_APP_ID].char_handle, &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); }{...} if (count > 0){ descr_elem_result_a = (esp_gattc_descr_elem_t *)malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result_a){ ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem"); }{...}else{ ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, p_data->reg_for_notify.handle, notify_descr_uuid, descr_elem_result_a, &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error"); }{...} /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */ if (count > 0 && descr_elem_result_a[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result_a[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){ ret_status = esp_ble_gattc_write_char_descr( gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, descr_elem_result_a[0].handle, sizeof(notify_en), (uint8_t *)&notify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); }{...} if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_write_char_descr error"); }{...} /* free descr_elem_result */ free(descr_elem_result_a); } }{...} else{ ESP_LOGE(GATTC_TAG, "decsr not found"); }{...} break; }{...} ... case ESP_GATTC_NOTIFY_EVT: ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, Receive notify value:"); ESP_LOG_BUFFER_HEX(GATTC_TAG, p_data->notify.value, p_data->notify.value_len); break;... case ESP_GATTC_WRITE_DESCR_EVT: if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "write descr failed, error status = %x", p_data->write.status); break; }{...} ESP_LOGI(GATTC_TAG, "write descr success"); uint8_t write_char_data[35]; for (int i = 0; i < sizeof(write_char_data); ++i) { write_char_data[i] = i % 256; }{...} esp_ble_gattc_write_char( gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, gl_profile_tab[PROFILE_A_APP_ID].char_handle, sizeof(write_char_data), write_char_data, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); break;... case ESP_GATTC_WRITE_CHAR_EVT: if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "write char failed, error status = %x", p_data->write.status); }{...}else{ ESP_LOGI(GATTC_TAG, "write char success"); } start_scan(); break;... case ESP_GATTC_SRVC_CHG_EVT: { esp_bd_addr_t bda; memcpy(bda, p_data->srvc_chg.remote_bda, sizeof(esp_bd_addr_t)); ESP_LOGI(GATTC_TAG, "ESP_GATTC_SRVC_CHG_EVT, bd_addr:%08x%04x",(bda[0] << 24) + (bda[1] << 16) + (bda[2] << 8) + bda[3], (bda[4] << 8) + bda[5]); break; }{...} ... case ESP_GATTC_DISCONNECT_EVT: //Start scanning again start_scan(); if (memcmp(p_data->disconnect.remote_bda, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, 6) == 0){ ESP_LOGI(GATTC_TAG, "device a disconnect"); conn_device_a = false; get_service_a = false; }{...} break;... default: break;... }{...} }{ ... } static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) { esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param; switch (event) { case ESP_GATTC_REG_EVT: ESP_LOGI(GATTC_TAG, "REG_EVT"); break;... case ESP_GATTC_CONNECT_EVT: break;... case ESP_GATTC_OPEN_EVT: if (p_data->open.status != ESP_GATT_OK){ //open failed, ignore the second device, connect the third device ESP_LOGE(GATTC_TAG, "connect device failed, status %d", p_data->open.status); conn_device_b = false; //start_scan(); break; }{...} memcpy(gl_profile_tab[PROFILE_B_APP_ID].remote_bda, p_data->open.remote_bda, 6); gl_profile_tab[PROFILE_B_APP_ID].conn_id = p_data->open.conn_id; ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu); ESP_LOGI(GATTC_TAG, "REMOTE BDA:"); ESP_LOG_BUFFER_HEX(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t)); esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id); if (mtu_ret){ ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret); }{...} break;... case ESP_GATTC_CFG_MTU_EVT: if (param->cfg_mtu.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG,"Config mtu failed"); }{...} ESP_LOGI(GATTC_TAG, "Status %d, MTU %d, conn_id %d", param->cfg_mtu.status, param->cfg_mtu.mtu, param->cfg_mtu.conn_id); esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, &remote_filter_service_uuid); break;... case ESP_GATTC_SEARCH_RES_EVT: { ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x is primary service %d", p_data->search_res.conn_id, p_data->search_res.is_primary); ESP_LOGI(GATTC_TAG, "start handle %d end handle %d current handle value %d", p_data->search_res.start_handle, p_data->search_res.end_handle, p_data->search_res.srvc_id.inst_id); if (p_data->search_res.srvc_id.uuid.len == ESP_UUID_LEN_16 && p_data->search_res.srvc_id.uuid.uuid.uuid16 == REMOTE_SERVICE_UUID) { ESP_LOGI(GATTC_TAG, "UUID16: %x", p_data->search_res.srvc_id.uuid.uuid.uuid16); get_service_b = true; gl_profile_tab[PROFILE_B_APP_ID].service_start_handle = p_data->search_res.start_handle; gl_profile_tab[PROFILE_B_APP_ID].service_end_handle = p_data->search_res.end_handle; }{...} break; }{...} ... case ESP_GATTC_SEARCH_CMPL_EVT: if (p_data->search_cmpl.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "search service failed, error status = %x", p_data->search_cmpl.status); break; }{...} if (get_service_b){ uint16_t count = 0; esp_gatt_status_t status = esp_ble_gattc_get_attr_count( gattc_if, p_data->search_cmpl.conn_id, ESP_GATT_DB_CHARACTERISTIC, gl_profile_tab[PROFILE_B_APP_ID].service_start_handle, gl_profile_tab[PROFILE_B_APP_ID].service_end_handle, INVALID_HANDLE, &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); }{...} if (count > 0){ char_elem_result_b = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result_b){ ESP_LOGE(GATTC_TAG, "gattc no mem"); break; }{...}else{ status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, gl_profile_tab[PROFILE_B_APP_ID].service_start_handle, gl_profile_tab[PROFILE_B_APP_ID].service_end_handle, remote_filter_char_uuid, char_elem_result_b, &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error"); free(char_elem_result_b); char_elem_result_b = NULL; break; }{...} /* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */ if (count > 0 && (char_elem_result_b[0].properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)){ gl_profile_tab[PROFILE_B_APP_ID].char_handle = char_elem_result_b[0].char_handle; esp_ble_gattc_register_for_notify (gattc_if, gl_profile_tab[PROFILE_B_APP_ID].remote_bda, char_elem_result_b[0].char_handle); }{...} } /* free char_elem_result */ free(char_elem_result_b); char_elem_result_b = NULL; }{...}else{ ESP_LOGE(GATTC_TAG, "no char found"); } }{...} break;... case ESP_GATTC_REG_FOR_NOTIFY_EVT: { if (p_data->reg_for_notify.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "reg notify failed, error status =%x", p_data->reg_for_notify.status); break; }{...} uint16_t count = 0; uint16_t notify_en = 1; esp_gatt_status_t ret_status = esp_ble_gattc_get_attr_count( gattc_if, gl_profile_tab[PROFILE_B_APP_ID].conn_id, ESP_GATT_DB_DESCRIPTOR, gl_profile_tab[PROFILE_B_APP_ID].service_start_handle, gl_profile_tab[PROFILE_B_APP_ID].service_end_handle, gl_profile_tab[PROFILE_B_APP_ID].char_handle, &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); }{...} if (count > 0){ descr_elem_result_b = (esp_gattc_descr_elem_t *)malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result_b){ ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem"); }{...}else{ ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if, gl_profile_tab[PROFILE_B_APP_ID].conn_id, p_data->reg_for_notify.handle, notify_descr_uuid, descr_elem_result_b, &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error"); free(descr_elem_result_b); descr_elem_result_b = NULL; break; }{...} /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */ if (count > 0 && descr_elem_result_b[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result_b[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){ ret_status = esp_ble_gattc_write_char_descr( gattc_if, gl_profile_tab[PROFILE_B_APP_ID].conn_id, descr_elem_result_b[0].handle, sizeof(notify_en), (uint8_t *)&notify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); }{...} if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_write_char_descr error"); }{...} /* free descr_elem_result */ free(descr_elem_result_b); descr_elem_result_b = NULL; } }{...} else{ ESP_LOGE(GATTC_TAG, "decsr not found"); }{...} break; }{...} ... case ESP_GATTC_NOTIFY_EVT: ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, Receive notify value:"); ESP_LOG_BUFFER_HEX(GATTC_TAG, p_data->notify.value, p_data->notify.value_len); break;... case ESP_GATTC_WRITE_DESCR_EVT: if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "write descr failed, error status = %x", p_data->write.status); break; }{...} ESP_LOGI(GATTC_TAG, "write descr success"); uint8_t write_char_data[35]; for (int i = 0; i < sizeof(write_char_data); ++i) { write_char_data[i] = i % 256; }{...} esp_ble_gattc_write_char( gattc_if, gl_profile_tab[PROFILE_B_APP_ID].conn_id, gl_profile_tab[PROFILE_B_APP_ID].char_handle, sizeof(write_char_data), write_char_data, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); break;... case ESP_GATTC_WRITE_CHAR_EVT: if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "Write char failed, error status = %x", p_data->write.status); }{...}else{ ESP_LOGI(GATTC_TAG, "Write char success"); } start_scan(); break;... case ESP_GATTC_SRVC_CHG_EVT: { esp_bd_addr_t bda; memcpy(bda, p_data->srvc_chg.remote_bda, sizeof(esp_bd_addr_t)); ESP_LOGI(GATTC_TAG, "ESP_GATTC_SRVC_CHG_EVT, bd_addr:%08x%04x",(bda[0] << 24) + (bda[1] << 16) + (bda[2] << 8) + bda[3], (bda[4] << 8) + bda[5]); break; }{...} ... case ESP_GATTC_DISCONNECT_EVT: if (memcmp(p_data->disconnect.remote_bda, gl_profile_tab[PROFILE_B_APP_ID].remote_bda, 6) == 0){ ESP_LOGI(GATTC_TAG, "device b disconnect"); conn_device_b = false; get_service_b = false; }{...} break;... default: break;... }{...} }{ ... } static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) { esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param; switch (event) { case ESP_GATTC_REG_EVT: ESP_LOGI(GATTC_TAG, "REG_EVT"); break;... case ESP_GATTC_CONNECT_EVT: break;... case ESP_GATTC_OPEN_EVT: if (p_data->open.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "connect device failed, status %d", p_data->open.status); conn_device_c = false; //start_scan(); break; }{...} memcpy(gl_profile_tab[PROFILE_C_APP_ID].remote_bda, p_data->open.remote_bda, 6); gl_profile_tab[PROFILE_C_APP_ID].conn_id = p_data->open.conn_id; ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu); ESP_LOGI(GATTC_TAG, "REMOTE BDA:"); ESP_LOG_BUFFER_HEX(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t)); esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id); if (mtu_ret){ ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret); }{...} break;... case ESP_GATTC_CFG_MTU_EVT: if (param->cfg_mtu.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG,"Config mtu failed"); }{...} ESP_LOGI(GATTC_TAG, "Status %d, MTU %d, conn_id %d", param->cfg_mtu.status, param->cfg_mtu.mtu, param->cfg_mtu.conn_id); esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, &remote_filter_service_uuid); break;... case ESP_GATTC_SEARCH_RES_EVT: { ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x is primary service %d", p_data->search_res.conn_id, p_data->search_res.is_primary); ESP_LOGI(GATTC_TAG, "start handle %d end handle %d current handle value %d", p_data->search_res.start_handle, p_data->search_res.end_handle, p_data->search_res.srvc_id.inst_id); if (p_data->search_res.srvc_id.uuid.len == ESP_UUID_LEN_16 && p_data->search_res.srvc_id.uuid.uuid.uuid16 == REMOTE_SERVICE_UUID) { ESP_LOGI(GATTC_TAG, "UUID16: %x", p_data->search_res.srvc_id.uuid.uuid.uuid16); get_service_c = true; gl_profile_tab[PROFILE_C_APP_ID].service_start_handle = p_data->search_res.start_handle; gl_profile_tab[PROFILE_C_APP_ID].service_end_handle = p_data->search_res.end_handle; }{...} break; }{...} ... case ESP_GATTC_SEARCH_CMPL_EVT: if (p_data->search_cmpl.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "search service failed, error status = %x", p_data->search_cmpl.status); break; }{...} if (get_service_c){ uint16_t count = 0; esp_gatt_status_t status = esp_ble_gattc_get_attr_count( gattc_if, p_data->search_cmpl.conn_id, ESP_GATT_DB_CHARACTERISTIC, gl_profile_tab[PROFILE_C_APP_ID].service_start_handle, gl_profile_tab[PROFILE_C_APP_ID].service_end_handle, INVALID_HANDLE, &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); }{...} if (count > 0){ char_elem_result_c = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result_c){ ESP_LOGE(GATTC_TAG, "gattc no mem"); break; }{...}else{ status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, gl_profile_tab[PROFILE_C_APP_ID].service_start_handle, gl_profile_tab[PROFILE_C_APP_ID].service_end_handle, remote_filter_char_uuid, char_elem_result_c, &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error"); free(char_elem_result_c); char_elem_result_c = NULL; break; }{...} /* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */ if (count > 0 && (char_elem_result_c[0].properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)){ gl_profile_tab[PROFILE_C_APP_ID].char_handle = char_elem_result_c[0].char_handle; esp_ble_gattc_register_for_notify (gattc_if, gl_profile_tab[PROFILE_C_APP_ID].remote_bda, char_elem_result_c[0].char_handle); }{...} } /* free char_elem_result */ free(char_elem_result_c); char_elem_result_c = NULL; }{...}else{ ESP_LOGE(GATTC_TAG, "no char found"); } }{...} break;... case ESP_GATTC_REG_FOR_NOTIFY_EVT: { if (p_data->reg_for_notify.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "reg notify failed, error status =%x", p_data->reg_for_notify.status); break; }{...} uint16_t count = 0; uint16_t notify_en = 1; esp_gatt_status_t ret_status = esp_ble_gattc_get_attr_count( gattc_if, gl_profile_tab[PROFILE_C_APP_ID].conn_id, ESP_GATT_DB_DESCRIPTOR, gl_profile_tab[PROFILE_C_APP_ID].service_start_handle, gl_profile_tab[PROFILE_C_APP_ID].service_end_handle, gl_profile_tab[PROFILE_C_APP_ID].char_handle, &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); }{...} if (count > 0){ descr_elem_result_c = (esp_gattc_descr_elem_t *)malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result_c){ ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem"); break; }{...}else{ ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if, gl_profile_tab[PROFILE_C_APP_ID].conn_id, p_data->reg_for_notify.handle, notify_descr_uuid, descr_elem_result_c, &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error"); free(descr_elem_result_c); descr_elem_result_c = NULL; break; }{...} /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */ if (count > 0 && descr_elem_result_c[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result_c[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){ ret_status = esp_ble_gattc_write_char_descr( gattc_if, gl_profile_tab[PROFILE_C_APP_ID].conn_id, descr_elem_result_c[0].handle, sizeof(notify_en), (uint8_t *)&notify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); }{...} if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_write_char_descr error"); }{...} /* free descr_elem_result */ free(descr_elem_result_c); descr_elem_result_c = NULL; } }{...} else{ ESP_LOGE(GATTC_TAG, "decsr not found"); }{...} break; }{...} ... case ESP_GATTC_NOTIFY_EVT: ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, Receive notify value:"); ESP_LOG_BUFFER_HEX(GATTC_TAG, p_data->notify.value, p_data->notify.value_len); break;... case ESP_GATTC_WRITE_DESCR_EVT: if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "write descr failed, error status = %x", p_data->write.status); break; }{...} ESP_LOGI(GATTC_TAG, "write descr success"); uint8_t write_char_data[35]; for (int i = 0; i < sizeof(write_char_data); ++i) { write_char_data[i] = i % 256; }{...} esp_ble_gattc_write_char( gattc_if, gl_profile_tab[PROFILE_C_APP_ID].conn_id, gl_profile_tab[PROFILE_C_APP_ID].char_handle, sizeof(write_char_data), write_char_data, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); break;... case ESP_GATTC_WRITE_CHAR_EVT: if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "Write char failed, error status = %x", p_data->write.status); break; }{...} ESP_LOGI(GATTC_TAG, "Write char success"); start_scan(); break;... case ESP_GATTC_SRVC_CHG_EVT: { esp_bd_addr_t bda; memcpy(bda, p_data->srvc_chg.remote_bda, sizeof(esp_bd_addr_t)); ESP_LOGI(GATTC_TAG, "ESP_GATTC_SRVC_CHG_EVT, bd_addr:%08x%04x",(bda[0] << 24) + (bda[1] << 16) + (bda[2] << 8) + bda[3], (bda[4] << 8) + bda[5]); break; }{...} ... case ESP_GATTC_DISCONNECT_EVT: if (memcmp(p_data->disconnect.remote_bda, gl_profile_tab[PROFILE_C_APP_ID].remote_bda, 6) == 0){ ESP_LOGI(GATTC_TAG, "device c disconnect"); conn_device_c = false; get_service_c = false; }{...} break;... default: break;... }{...} }{ ... } static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { uint8_t *adv_name = NULL; uint8_t adv_name_len = 0; switch (event) { case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: ESP_LOGI(GATTC_TAG, "update connection params status = %d, conn_int = %d, latency = %d, timeout = %d", param->update_conn_params.status, param->update_conn_params.conn_int, param->update_conn_params.latency, param->update_conn_params.timeout); break;... case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: { //the unit of the duration is second uint32_t duration = 30; esp_ble_gap_start_scanning(duration); break; }{...} ... case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT: //scan start complete event to indicate scan start successfully or failed if (param->scan_start_cmpl.status == ESP_BT_STATUS_SUCCESS) { ESP_LOGI(GATTC_TAG, "Scan start success"); }{...}else{ ESP_LOGE(GATTC_TAG, "Scan start failed"); } break;... case ESP_GAP_BLE_SCAN_RESULT_EVT: { esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param; switch (scan_result->scan_rst.search_evt) { case ESP_GAP_SEARCH_INQ_RES_EVT: ESP_LOG_BUFFER_HEX(GATTC_TAG, scan_result->scan_rst.bda, 6); ESP_LOGI(GATTC_TAG, "Searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len); adv_name = esp_ble_resolve_adv_data_by_type(scan_result->scan_rst.ble_adv, scan_result->scan_rst.adv_data_len + scan_result->scan_rst.scan_rsp_len, ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len); ESP_LOGI(GATTC_TAG, "Searched Device Name Len %d", adv_name_len); ESP_LOG_BUFFER_CHAR(GATTC_TAG, adv_name, adv_name_len); ESP_LOGI(GATTC_TAG, " "); if (Isconnecting){ break; }{...} if (conn_device_a && conn_device_b && conn_device_c && !stop_scan_done){ stop_scan_done = true; esp_ble_gap_stop_scanning(); ESP_LOGI(GATTC_TAG, "all devices are connected"); break; }{...} if (adv_name != NULL) { if (strlen(remote_device_name[0]) == adv_name_len && strncmp((char *)adv_name, remote_device_name[0], adv_name_len) == 0) { if (conn_device_a == false) { conn_device_a = true; ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[0]); esp_ble_gap_stop_scanning(); esp_ble_gatt_creat_conn_params_t creat_conn_params = {0}; memcpy(&creat_conn_params.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); creat_conn_params.remote_addr_type = scan_result->scan_rst.ble_addr_type; creat_conn_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC; creat_conn_params.is_direct = true; creat_conn_params.is_aux = false; creat_conn_params.phy_mask = 0x0; esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, &creat_conn_params); Isconnecting = true; }{...} break; }{...} else if (strlen(remote_device_name[1]) == adv_name_len && strncmp((char *)adv_name, remote_device_name[1], adv_name_len) == 0) { if (conn_device_b == false) { conn_device_b = true; ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[1]); esp_ble_gap_stop_scanning(); esp_ble_gatt_creat_conn_params_t creat_conn_params = {0}; memcpy(&creat_conn_params.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); creat_conn_params.remote_addr_type = scan_result->scan_rst.ble_addr_type; creat_conn_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC; creat_conn_params.is_direct = true; creat_conn_params.is_aux = false; creat_conn_params.phy_mask = 0x0; esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, &creat_conn_params); Isconnecting = true; }{...} }{...} else if (strlen(remote_device_name[2]) == adv_name_len && strncmp((char *)adv_name, remote_device_name[2], adv_name_len) == 0) { if (conn_device_c == false) { conn_device_c = true; ESP_LOGI(GATTC_TAG, "Searched device %s", remote_device_name[2]); esp_ble_gap_stop_scanning(); esp_ble_gatt_creat_conn_params_t creat_conn_params = {0}; memcpy(&creat_conn_params.remote_bda, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); creat_conn_params.remote_addr_type = scan_result->scan_rst.ble_addr_type; creat_conn_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC; creat_conn_params.is_direct = true; creat_conn_params.is_aux = false; creat_conn_params.phy_mask = 0x0; esp_ble_gattc_enh_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, &creat_conn_params); Isconnecting = true; }{...} break; }{...} }{...} break;... case ESP_GAP_SEARCH_INQ_CMPL_EVT: break;... default: break;... }{...} break; }{...} ... case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT: if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){ ESP_LOGE(GATTC_TAG, "Scan stop failed"); break; }{...} ESP_LOGI(GATTC_TAG, "Stop scan successfully"); break; ... case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){ ESP_LOGE(GATTC_TAG, "Adv stop failed"); break; }{...} ESP_LOGI(GATTC_TAG, "Stop adv successfully"); break; ... default: break;... }{...} }{ ... } static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) { //ESP_LOGI(GATTC_TAG, "EVT %d, gattc if %d, app_id %d", event, gattc_if, param->reg.app_id); /* If event is register event, store the gattc_if for each profile */ if (event == ESP_GATTC_REG_EVT) { if (param->reg.status == ESP_GATT_OK) { gl_profile_tab[param->reg.app_id].gattc_if = gattc_if; }{...} else { ESP_LOGI(GATTC_TAG, "Reg app failed, app_id %04x, status %d", param->reg.app_id, param->reg.status); return; }{...} }{...} /* If the gattc_if equal to profile A, call profile A cb handler, * so here call each profile's callback *//* ... */ do { int idx; for (idx = 0; idx < PROFILE_NUM; idx++) { if (gattc_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */ gattc_if == gl_profile_tab[idx].gattc_if) { if (gl_profile_tab[idx].gattc_cb) { gl_profile_tab[idx].gattc_cb(event, gattc_if, param); }{...} }{...} }{...} }{...} while (0); }{ ... } void app_main(void) { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); }{...} ESP_ERROR_CHECK( ret ); ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); ret = esp_bt_controller_init(&bt_cfg); if (ret) { ESP_LOGE(GATTC_TAG, "%s initialize controller failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); if (ret) { ESP_LOGE(GATTC_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bluedroid_init(); if (ret) { ESP_LOGE(GATTC_TAG, "%s init bluetooth failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bluedroid_enable(); if (ret) { ESP_LOGE(GATTC_TAG, "%s enable bluetooth failed: %s", __func__, esp_err_to_name(ret)); return; }{...} //register the callback function to the gap module ret = esp_ble_gap_register_callback(esp_gap_cb); if (ret){ ESP_LOGE(GATTC_TAG, "gap register error, error code = %x", ret); return; }{...} //register the callback function to the gattc module ret = esp_ble_gattc_register_callback(esp_gattc_cb); if(ret){ ESP_LOGE(GATTC_TAG, "gattc register error, error code = %x", ret); return; }{...} ret = esp_ble_gattc_app_register(PROFILE_A_APP_ID); if (ret){ ESP_LOGE(GATTC_TAG, "gattc app register error, error code = %x", ret); return; }{...} ret = esp_ble_gattc_app_register(PROFILE_B_APP_ID); if (ret){ ESP_LOGE(GATTC_TAG, "gattc app register error, error code = %x", ret); return; }{...} ret = esp_ble_gattc_app_register(PROFILE_C_APP_ID); if (ret){ ESP_LOGE(GATTC_TAG, "gattc app register error, error code = %x", ret); return; }{...} ret = esp_ble_gatt_set_local_mtu(200); if (ret){ ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", ret); }{...} }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.