slave.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. --- 模块功能:蓝牙功能测试
  2. -- @author openLuat
  3. -- @module bluetooth.slave
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2020.09.27
  7. -- @注意 需要使用core(Luat_VXXXX_RDA8910_BT_FLOAT)版本
  8. module(..., package.seeall)
  9. --蓝牙/WIFI分时复用功能测试
  10. local btWifiTdmTest = false
  11. require "wifiScan"
  12. local function init()
  13. log.info("bt", "init")
  14. rtos.on(rtos.MSG_BLUETOOTH, function(msg)
  15. if msg.event == btcore.MSG_OPEN_CNF then
  16. log.info("bt", "ble open") --蓝牙打开成功
  17. sys.publish("BT_OPEN", msg.result) --蓝牙打开成功
  18. elseif msg.event == btcore.MSG_CLOSE_CNF then
  19. log.info("bt", "ble close") --蓝牙关闭成功
  20. elseif msg.event == btcore.MSG_BLE_CONNECT_IND then
  21. sys.publish("BT_CONNECT_IND", {["handle"] = msg.handle, ["result"] = msg.result, ["addr"] = msg.addr}) --蓝牙连接成功
  22. elseif msg.event == btcore.MSG_BLE_DISCONNECT_IND then
  23. log.info("bt", "ble disconnect") --蓝牙断开连接
  24. elseif msg.event == btcore.MSG_BLE_DATA_IND then
  25. sys.publish("BT_DATA_IND", {["result"] = msg.result})--接收到的数据内容
  26. end
  27. end)
  28. end
  29. local function unInit()
  30. btcore.close()
  31. end
  32. local function poweron()
  33. log.info("bt", "poweron")
  34. btcore.open(0) --打开蓝牙从模式
  35. _, result = sys.waitUntil("BT_OPEN", 5000) --等待蓝牙打开成功
  36. end
  37. --[[
  38. --自定义服务
  39. local function service(uuid,struct)
  40. btcore.addservice(uuid) --添加服务
  41. for i = 1, #struct do
  42. btcore.addcharacteristic(struct[i][1],struct[i][2],struct[i][3]) --添加特征
  43. if(type(struct[i][4]) == "table") then
  44. for j = 1,#struct[i][4] do
  45. btcore.adddescriptor(struct[i][4][j][1],struct[i][4][j][2]) --添加描述
  46. end
  47. end
  48. end
  49. end
  50. ]]--
  51. local function advertising()
  52. --local struct1 = {{0xfee1, 0x08, 0x0002},
  53. -- {0xfee2, 0x10,0x0001, {{0x2902,0x0001},{0x2901,"123456"}}}}--{特征uuid,特征属性,特征权限,{特征描述uuid,描述属性}}
  54. --local struct2 = {{"9ecadc240ee5a9e093f3a3b50200406e",0x10,0x0001,{{0x2902,0x0001}}},
  55. -- {"9ecadc240ee5a9e093f3a3b50300406e",0x0c, 0x0002}}
  56. log.info("bt", "advertising")
  57. btcore.setname("Luat_Air724UG")-- 设置广播名称
  58. btcore.setadvdata(string.fromHex("02010604ff000203"))-- 设置广播数据 根据蓝牙广播包协议
  59. --btcore.setscanrspdata(string.fromHex("04ff000203"))-- 设置广播数据 根据蓝牙广播包协议
  60. --service(0xfee0, struct1)--添加服务16bit uuid 自定义服务
  61. --service("9ecadc240ee5a9e093f3a3b50100406e",struct2)--添加服务128bit uuid 自定义服务
  62. --btcore.addwhitelist("11:22:33:44:55:66",0)
  63. --btcore.setadvparam(0x80,0xa0,0,0,0x07,2,0,"11:22:33:44:55:66") --广播参数设置 (最小广播间隔,最大广播间隔,广播类型,广播本地地址类型,广播channel map,广播过滤策略,定向地址类型,定向地址)
  64. btcore.advertising(1)-- 打开广播
  65. end
  66. local function data_trans()
  67. advertising()
  68. _, bt_connect = sys.waitUntil("BT_CONNECT_IND") --等待连接成功
  69. if bt_connect.result ~= 0 then
  70. return false
  71. end
  72. --链接成功
  73. log.info("bt.connect_handle",bt_connect.handle) --连接句柄
  74. log.info("bt.send", "Hello I'm Luat BLE")
  75. while true do
  76. _, bt_recv = sys.waitUntil("BT_DATA_IND") --等待接收到数据
  77. local data = ""
  78. local len = 0
  79. local uuid = ""
  80. while true do
  81. local recvuuid, recvdata, recvlen = btcore.recv(3)
  82. if recvlen == 0 then
  83. break
  84. end
  85. uuid = recvuuid
  86. len = len + recvlen
  87. data = data .. recvdata
  88. end
  89. if len ~= 0 then
  90. log.info("bt.recv_data", data)
  91. log.info("bt.recv_data len", len)
  92. log.info("bt.recv_uuid", string.toHex(uuid))
  93. if data == "close" then
  94. btcore.disconnect()--主动断开连接
  95. if btWifiTdmTest then return end
  96. end
  97. btcore.send(data, 0xfee2, bt_connect.handle)--发送数据(数据 对应特征uuid 连接句柄)
  98. --btcore.send(data, "9ecadc240ee5a9e093f3a3b50200406e", bt_connect.handle)--发送数据(数据 对应特征uuid 连接句柄)
  99. end
  100. end
  101. end
  102. local ble_test = {init, poweron,data_trans}
  103. if btWifiTdmTest then
  104. --蓝牙wifi分时复用测试方法:
  105. --1、测试wifi搜索热点功能
  106. --2、打开蓝牙,配置为从模式;手机上可以安装一个app,连接模块蓝牙,向模块发送数据测试;当手机app向模块发送5个字节的数据close时,模块会主动断开并且关闭蓝牙
  107. --3、延时5秒钟,继续从第1步开始测试
  108. sys.taskInit(function()
  109. while true do
  110. sys.wait(5000)
  111. log.info("wifiScan.request begin")
  112. wifiScan.request(function(result,cnt,tInfo)
  113. log.info("testWifi.scanCb",result,cnt)
  114. log.info("testLbsLoc.wifiScan.request result",result,cnt)
  115. sys.publish("WIFI_SCAN_IND",result,cnt,tInfo)
  116. end)
  117. sys.waitUntil("WIFI_SCAN_IND")
  118. log.info("wifiScan.request end")
  119. init()
  120. poweron()
  121. data_trans()
  122. --关闭蓝牙
  123. unInit()
  124. end
  125. end)
  126. else
  127. sys.taskInit(function()
  128. sys.wait(5000)
  129. for _, f in ipairs(ble_test) do
  130. f()
  131. end
  132. end)
  133. end