bluetooth.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import _bluetooth
  2. # 需要额外注意类型转换
  3. class BLE(_bluetooth.BLE):
  4. # last_adv_data = "" #广播内容
  5. # last_resp_data = "" #回应扫描内容
  6. # addr_mode = "" #地址类型
  7. # def __init__(self):
  8. # super().__init__()
  9. # pass
  10. def test(self):
  11. pass
  12. def active(self):
  13. # _bluetooth.BLE.active(self)
  14. # a = _bluetooth.BLE()
  15. # a.active()
  16. _ = super().active()
  17. # self.active()
  18. return print("bluetooth active")
  19. # def config(self,param_name, /):
  20. # if(param_name=="mac"):
  21. # pass
  22. # elif(param_name=="addr_mode"):
  23. # pass
  24. # elif(param_name=="gap_name"):
  25. # pass
  26. # elif(param_name=="rxbuf"):
  27. # pass
  28. # elif(param_name=="mtu"):
  29. # pass
  30. # elif(param_name=="bond"):
  31. # pass
  32. # elif(param_name=="mitm"):
  33. # pass
  34. # elif(param_name=="io"):
  35. # pass
  36. # elif(param_name=="le_secire"):
  37. # pass
  38. # else:
  39. # print("there is not params")
  40. # def config(self,*, mac,addr_mode,gap_name,rxbuf,mtu,bond,mitm,io,le_secire=False):
  41. # if gap_name != None:
  42. # super.config_name_update(gap_name)
  43. # # TODO:如何进行事件处理函数
  44. # def irq():
  45. # pass
  46. # """
  47. # interval_us:广告间隔时间, 为none则停止广播
  48. # adv_data: 包含在所有广播中, 是任何实现缓冲协议的类型(例如bytes, bytearray, str)
  49. # resp_data: 被发送以响应主动扫描, 是任何实现缓冲协议的类型(例如bytes, bytearray, str)
  50. # """
  51. # def gap_advertise(self, interval_us, adv_data=None, *, resp_data=None, connectable=True):
  52. # # 停止广播
  53. # if interval_us is None:
  54. # return super.stop_advertise()
  55. # if adv_data is None:
  56. # adv_data = self.last_adv_data
  57. # else :
  58. # self.last_adv_data = adv_data
  59. # if resp_data is None:
  60. # resp_data = self.last_resp_data
  61. # else :
  62. # self.last_resp_data = resp_data
  63. # #TODO:active的作用是什么意思
  64. # """
  65. # 使用interval_us和window_us可选择配置占空比。扫描器将每interval_us微秒运行window_us 微秒,
  66. # 总共持续duration_ms毫秒。默认间隔和窗口分别为 1.28 秒和 11.25 毫秒(后台扫描)。
  67. # TODO:无法完全实现和micropython相同API
  68. # """
  69. # # def gap_scan(self, duration_ms, interval_us=1280000, window_us=11250, active=False, /):
  70. # # if duration_ms is None :
  71. # # super.gap_stop_scan()
  72. # # elif (duration_ms == 0):
  73. # # super.gap_scan_forever()
  74. # # else:
  75. # # super.gap_scan_forever()
  76. # """
  77. # micropython:直接输入时间
  78. # nimble: 按照单位算时间
  79. # duration:
  80. # """
  81. # def gap_scan(self, duration_ms, interval_us=1280000, window_us=11250, active=False, /):
  82. # if duration_ms is None :
  83. # super.gap_stop_scan()
  84. # else:
  85. # duration = duration_ms / 10
  86. # super.gap_scan(duration_ms, interval_us, window_us, active)
  87. # def gap_connect(self, addr_type, addr, scan_duration_ms=2000, /):
  88. # super.gap_connect(addr_type, addr,scan_duration_ms)
  89. # pass
  90. # def gap_disconnect(self, conn_handle, /):
  91. # super.gap_disconnect()
  92. # pass
  93. # def gatts_register_services(self, services_definition, /):
  94. # for service in services_definition:
  95. # service_uuid, characteristics = service
  96. # print(f"Service UUID: {service_uuid}")
  97. # # 遍历特征
  98. # for characteristic in characteristics:
  99. # char_uuid, flags = characteristic
  100. # print(f"Characteristic UUID: {char_uuid}")
  101. # print(f"Flags: {flags}")
  102. # super.register_a_service()
  103. # def gatts_read(value_handle, /):
  104. # pass
  105. # def gatts_write(value_handle, data, send_update=False, /):
  106. # pass
  107. # def gatts_notify(conn_handle, value_handle, data=None, /):
  108. # pass
  109. # def gatts_indicate(conn_handle, value_handle, /):
  110. # pass
  111. # def gatts_set_buffer(value_handle, len, append=False, /):
  112. # pass
  113. # def gattc_discover_services(conn_handle, uuid=None, /):
  114. # pass
  115. # def gattc_discover_characteristics(conn_handle, start_handle, end_handle, uuid=None, /):
  116. # pass
  117. # def gattc_discover_descriptors(conn_handle, start_handle, end_handle, /):
  118. # pass
  119. # def gattc_read(conn_handle, value_handle, /):
  120. # pass
  121. # def gattc_write(conn_handle, value_handle, data, mode=0, /):
  122. # pass
  123. # def gattc_exchange_mtu(conn_handle, /):
  124. # pass