Skip to content

Commit 9452304

Browse files
Mr-FJCjayceon.fu
and
jayceon.fu
authored
继续完善ble client端smp例程 (#13)
* code (cellular): 提交蜂窝无线网卡应用文档中的示例代码 固件版本: N/A 是否需要文案翻译: 是 * 增加拨号应用文档异常处理章节中apn配置例程 * 根据最新要求将示例代码中的注释都换成英文 * 增加ble smp client/server端相关示例 * 继续完善ble client smp例程。 --------- Co-authored-by: jayceon.fu <jayceon.fu@quectel.com>
1 parent 1187cdd commit 9452304

File tree

1 file changed

+57
-9
lines changed

1 file changed

+57
-9
lines changed

bt/ble_smp/ble_smp_client_demo.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ble
44
import utime
5+
import osTimer
56
import _thread
67
import checkNet
78
from queue import Queue
@@ -385,8 +386,23 @@ def ble_client_callback(args):
385386
msg_queue.put(args)
386387

387388

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+
388402
def ble_gatt_client_event_handler():
389403
global msg_queue
404+
global pair_timer
405+
global pair_timer_status
390406
old_time = 0
391407

392408
while True:
@@ -463,10 +479,33 @@ def ble_gatt_client_event_handler():
463479
addr_str = '{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(addr[0], addr[1], addr[2], addr[3], addr[4], addr[5])
464480
print('connect_id : {:#x}, connect_addr : {}'.format(ble_client.connect_id, addr_str))
465481

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+
470509
else:
471510
print('ble connect failed.')
472511
break
@@ -577,7 +616,15 @@ def ble_gatt_client_event_handler():
577616
ble_client.chara_descriptor_count = 0
578617
ble_client.characteristic_index = 0
579618
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+
'''
581628
# if ble_client.discover_service_mode == 0:
582629
# print('execute the function discover_all_service.')
583630
# ret = ble_client.discover_all_service()
@@ -587,7 +634,6 @@ def ble_gatt_client_event_handler():
587634
# if ret != 0:
588635
# print('Execution result: Failed.')
589636
# ble_client.gatt_close()
590-
# ble_client.release()
591637
# break
592638
elif event_id == event.BLE_GATT_DISCOVER_SERVICE_IND:
593639
print('')
@@ -829,6 +875,11 @@ def ble_gatt_client_event_handler():
829875
print('event_id : BLE_GATT_SMP_COMPLETE_IND, status = {}'.format(status))
830876
if status == 0:
831877
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()
832883
if ble_client.gatt_statue == gatt_status.BLE_GATT_DISCOVER_SERVICE:
833884
if ble_client.discover_service_mode == 0:
834885
print('execute the function discover_all_service.')
@@ -839,7 +890,6 @@ def ble_gatt_client_event_handler():
839890
if ret != 0:
840891
print('Execution result: Failed.')
841892
ble_client.gatt_close()
842-
ble_client.release()
843893
break
844894
else:
845895
print('[callback] ble smp pairing fail.')
@@ -908,13 +958,11 @@ def main():
908958
# ble_status = ble_client.gatt_get_status()
909959
# if ble_status == 1:
910960
# ble_client.gatt_close()
911-
# ble_client.release()
912961
# break
913962
# else:
914963
# ble_status = ble_client.gatt_get_status()
915964
# if ble_status == 0: # stopped
916965
# print('BLE connection has been disconnected.')
917-
# ble_client.release()
918966
# break
919967

920968
if __name__ == '__main__':

0 commit comments

Comments
 (0)