2
2
3
3
import ble
4
4
import utime
5
+ import osTimer
5
6
import _thread
6
7
import checkNet
7
8
from queue import Queue
@@ -385,8 +386,23 @@ def ble_client_callback(args):
385
386
msg_queue .put (args )
386
387
387
388
389
+ pair_timer = osTimer ()
390
+ pair_timer_status = 0 # 0-not running, 1-running
391
+
392
+
393
+ def pair_timer_callback (args ):
394
+ global pair_timer_status
395
+ print ('[pair timer cb]ble smp start pair...' )
396
+ pair_timer_status = 0
397
+ ret = ble .smpStartPair (ble_client .connect_id )
398
+ if ret != 0 :
399
+ print ('smpStartPair execution failed.' )
400
+
401
+
388
402
def ble_gatt_client_event_handler ():
389
403
global msg_queue
404
+ global pair_timer
405
+ global pair_timer_status
390
406
old_time = 0
391
407
392
408
while True :
@@ -463,10 +479,33 @@ def ble_gatt_client_event_handler():
463
479
addr_str = '{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}' .format (addr [0 ], addr [1 ], addr [2 ], addr [3 ], addr [4 ], addr [5 ])
464
480
print ('connect_id : {:#x}, connect_addr : {}' .format (ble_client .connect_id , addr_str ))
465
481
466
- print ('[callback] ble smp start pair...' )
467
- ret = ble .smpStartPair (ble_client .connect_id )
468
- if ret != 0 :
469
- print ('[callback] ble smp start pair failed!' )
482
+ pair_device_infos = ble .smpGetPairedDevInfo ()
483
+ pair_device_nums = 0
484
+ if pair_device_infos != - 1 :
485
+ pair_device_nums = len (pair_device_infos )
486
+ if pair_device_nums == 0 :
487
+ print ('[1]First pairing, ble smp start pair...' )
488
+ ret = ble .smpStartPair (ble_client .connect_id )
489
+ if ret != 0 :
490
+ print ('smpStartPair execution failed.' )
491
+ else :
492
+ print ('paired list({}):' .format (pair_device_nums ))
493
+ i = 0
494
+ for dev_addr in pair_device_infos :
495
+ i += 1
496
+ dev_addr_str = '{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}' .format (dev_addr [0 ], dev_addr [1 ], dev_addr [2 ], dev_addr [3 ], dev_addr [4 ], dev_addr [5 ])
497
+ print ("device{} mac:{}" .format (i , dev_addr_str ))
498
+
499
+ if addr in pair_device_infos :
500
+ print ('Wait for automatic pairing, timeout is 1200ms.' )
501
+ pair_timer .start (1200 , 0 , pair_timer_callback )
502
+ pair_timer_status = 1
503
+ else :
504
+ print ('[2]First pairing, ble smp start pair...' )
505
+ ret = ble .smpStartPair (ble_client .connect_id )
506
+ if ret != 0 :
507
+ print ('smpStartPair execution failed.' )
508
+
470
509
else :
471
510
print ('ble connect failed.' )
472
511
break
@@ -577,7 +616,15 @@ def ble_gatt_client_event_handler():
577
616
ble_client .chara_descriptor_count = 0
578
617
ble_client .characteristic_index = 0
579
618
ble_client .gatt_statue = gatt_status .BLE_GATT_DISCOVER_SERVICE
580
-
619
+ '''
620
+ For scenarios that do not require pairing function (or do not support pairing),
621
+ after receiving the BLE_GATT_START_DISCOVER_SERVICE_IND event, you can perform
622
+ operations such as discovering services; for scenarios that support pairing and
623
+ require pairing, since the pairing process needs to be performed first after
624
+ establishing a connection, you need Wait for the pairing process to end before
625
+ performing operations such as discovering the service. When the pairing is completed,
626
+ the BLE_GATT_SMP_COMPLETE_IND event will be received.
627
+ '''
581
628
# if ble_client.discover_service_mode == 0:
582
629
# print('execute the function discover_all_service.')
583
630
# ret = ble_client.discover_all_service()
@@ -587,7 +634,6 @@ def ble_gatt_client_event_handler():
587
634
# if ret != 0:
588
635
# print('Execution result: Failed.')
589
636
# ble_client.gatt_close()
590
- # ble_client.release()
591
637
# break
592
638
elif event_id == event .BLE_GATT_DISCOVER_SERVICE_IND :
593
639
print ('' )
@@ -829,6 +875,11 @@ def ble_gatt_client_event_handler():
829
875
print ('event_id : BLE_GATT_SMP_COMPLETE_IND, status = {}' .format (status ))
830
876
if status == 0 :
831
877
print ('[callback] ble smp pairing complete.' )
878
+ if pair_timer_status == 1 :
879
+ print ('stop pair timer.' )
880
+ pair_timer .stop ()
881
+ pair_timer_status = 0
882
+ pair_timer .delete_timer ()
832
883
if ble_client .gatt_statue == gatt_status .BLE_GATT_DISCOVER_SERVICE :
833
884
if ble_client .discover_service_mode == 0 :
834
885
print ('execute the function discover_all_service.' )
@@ -839,7 +890,6 @@ def ble_gatt_client_event_handler():
839
890
if ret != 0 :
840
891
print ('Execution result: Failed.' )
841
892
ble_client .gatt_close ()
842
- ble_client .release ()
843
893
break
844
894
else :
845
895
print ('[callback] ble smp pairing fail.' )
@@ -908,13 +958,11 @@ def main():
908
958
# ble_status = ble_client.gatt_get_status()
909
959
# if ble_status == 1:
910
960
# ble_client.gatt_close()
911
- # ble_client.release()
912
961
# break
913
962
# else:
914
963
# ble_status = ble_client.gatt_get_status()
915
964
# if ble_status == 0: # stopped
916
965
# print('BLE connection has been disconnected.')
917
- # ble_client.release()
918
966
# break
919
967
920
968
if __name__ == '__main__' :
0 commit comments