ble_master.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. --[[
  2. @module ble_ibeacon
  3. @summary Air8000演示ibeacon功能模块
  4. @version 1.0
  5. @date 2025.07.01
  6. @author wangshihao
  7. @usage
  8. 本文件为Air8000核心板演示master功能的代码示例,核心业务逻辑为:
  9. 1. 初始化蓝牙底层框架
  10. bluetooth_device = bluetooth.init()
  11. 2. 创建BLE对象实例
  12. local ble_device = bluetooth_device:ble(ble_event_cb)
  13. 3. 创建BLE扫描
  14. ble_device:scan_create({})
  15. 4. 开始BLE扫描
  16. ble_device:scan_start()
  17. 5. 在回调函数中处理事件
  18. a. 扫描报告事件(ble.EVENT_SCAN_REPORT)
  19. - 每收到一个扫描report,计数器'scan_count'加1。
  20. - 如果扫描次数超过100次,停止扫描,15秒后重新开始(防止过度扫描)。
  21. - 检查扫描到的设备广播数据中是否包含"LuatOS"字符串,并且地址类型为0(公开地址)。如果满足条件,停止扫描并尝试连接该设备。
  22. b. 连接事件(ble.EVENT_CONN)
  23. - 连接成功,打印日志。
  24. c. 断开连接事件(ble.EVENT_DISCONN)
  25. - 打印日志,重新开始扫描。
  26. d. 读写数据事件(ble.EVENT_READ_VALUE, ble.EVENT_WRITE)
  27. - 打印日志。
  28. e. 通知事件(ble.EVENT_NOTIFY)
  29. - 打印日志。
  30. f. GATT事件(ble.EVENT_GATT_ITEM, ble.EVENT_GATT_DONE)
  31. - 开启指定服务(UUID:FA00)和特征值(UUID:EA01)的通知功能。
  32. - 向特征值(UUID:EA02)写入数据(十六进制数据"1234")。
  33. - 读取特征值(UUID:EA03)的数据。
  34. ]]
  35. local scan_count = 0
  36. local function ble_callback(ble_device, ble_event, ble_param)
  37. if ble_event == ble.EVENT_CONN then
  38. log.info("ble", "connect 成功")
  39. elseif ble_event == ble.EVENT_DISCONN then
  40. log.info("ble", "disconnect", ble_param.reason)
  41. sys.timerStart(function() ble_device:scan_start() end, 1000)
  42. elseif ble_event == ble.EVENT_WRITE then
  43. log.info("ble", "write", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex())
  44. log.info("ble", "data", ble_param.data:toHex())
  45. elseif ble_event == ble.EVENT_READ_VALUE then
  46. log.info("ble", "read", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex(),ble_param.data:toHex())
  47. elseif ble_event == ble.EVENT_SCAN_REPORT then
  48. print("ble scan report",ble_param.addr_type,ble_param.rssi,ble_param.adv_addr:toHex(),ble_param.data:toHex())
  49. scan_count = scan_count + 1
  50. if scan_count > 100 then
  51. log.info("ble", "扫描次数超过100次, 停止扫描, 15秒后重新开始")
  52. scan_count = 0
  53. ble_device:scan_stop()
  54. sys.timerStart(function() ble_device:scan_start() end, 15000)
  55. end
  56. -- 注意, 这里是连接到另外一个设备, 设备名称带LuatOS字样
  57. if ble_param.addr_type == 0 and ble_param.data:find("LuatOS") then
  58. log.info("ble", "停止扫描, 连接设备", ble_param.adv_addr:toHex(), ble_param.addr_type)
  59. ble_device:scan_stop()
  60. ble_device:connect(ble_param.adv_addr,ble_param.addr_type)
  61. end
  62. elseif ble_event == ble.EVENT_GATT_ITEM then
  63. -- 读取GATT完成, 打印出来
  64. log.info("ble", "gatt item", ble_param)
  65. elseif ble_event == ble.EVENT_GATT_DONE then
  66. log.info("ble", "gatt done", ble_param.service_num)
  67. local wt = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA01")}
  68. ble_device:notify_enable(wt, true) -- 开启通知
  69. -- 主动写入数据, 但不带通知, 带通知是 write_notify
  70. local wt = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA02")}
  71. ble_device:write_value(wt,string.fromHex("1234"))
  72. local wt = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA03")}
  73. ble_device:read_value(wt)
  74. end
  75. end
  76. function ble_master()
  77. log.info("开始初始化蓝牙核心")
  78. bluetooth_device = bluetooth.init()
  79. log.info("初始化BLE功能")
  80. ble_device = bluetooth_device:ble(ble_callback)
  81. -- master
  82. ble_device:scan_create({})
  83. ble_device:scan_start()
  84. -- ble_device:scan_stop()
  85. end
  86. sys.taskInit(ble_master)