airble.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. local airble = {}
  2. dnsproxy = require("dnsproxy")
  3. dhcpsrv = require("dhcpsrv")
  4. httpplus = require("httpplus")
  5. local run_state = false -- 判断本UI DEMO 是否运行
  6. local ble_state = "未初始"
  7. local ble_flag = "false"
  8. local Characteristic1 = "EA01"
  9. local Characteristic1_read = ""
  10. local Characteristic1_write = ""
  11. local Characteristic2 = "EA02"
  12. local Characteristic2_write = ""
  13. local Characteristic3 = "EA03"
  14. local Characteristic3_read = ""
  15. local Characteristic4 = "EA04"
  16. local Characteristic4_read = ""
  17. local Characteristic4ind = ""
  18. local att_db = nil
  19. local function set_att_db()
  20. att_db = { -- Service
  21. string.fromHex("FA00"), -- Service UUID
  22. -- Characteristic
  23. { -- Characteristic 1
  24. string.fromHex(Characteristic1), -- Characteristic UUID Value
  25. ble.NOTIFY | ble.READ | ble.WRITE -- Properties
  26. }, { -- Characteristic 2
  27. string.fromHex(Characteristic2), ble.WRITE
  28. }, { -- Characteristic 3
  29. string.fromHex(Characteristic3), ble.READ
  30. }, { -- Characteristic 4
  31. string.fromHex(Characteristic4), ble.IND | ble.READ
  32. }
  33. }
  34. end
  35. local function ble_callback(dev, evt, param)
  36. if evt == ble.EVENT_CONN then
  37. ble_state = "蓝牙链接成功"
  38. log.info("ble", ble_state, param, param and param.addr and param.addr:toHex() or "unknow")
  39. ble_flag = true
  40. elseif evt == ble.EVENT_DISCONN then
  41. ble_state = "蓝牙已断开"
  42. log.info("ble", ble_state)
  43. ble_flag = false
  44. -- 1秒后重新开始广播
  45. sys.timerStart(function() dev:adv_start() end, 1000)
  46. elseif evt == ble.EVENT_WRITE then
  47. -- 收到写请求
  48. ble_state = "接收到写请求"
  49. log.info("ble", "接收到写请求", param.uuid_service:toHex(), param.data:toHex(),param.uuid_characteristic:toHex())
  50. if param.uuid_characteristic:toHex() == Characteristic1 then
  51. Characteristic1_write = param.data:toHex()
  52. elseif param.uuid_characteristic:toHex() == Characteristic2 then
  53. Characteristic2_write = param.data:toHex()
  54. end
  55. end
  56. end
  57. local function write_read()
  58. local wr = {
  59. uuid_service = string.fromHex("FA00"),
  60. uuid_characteristic = string.fromHex("EA01"),
  61. }
  62. ble_device:write_value(wr, "FA00EA01 HELLO" .. os.date())
  63. wr = {
  64. uuid_service = string.fromHex("FA00"),
  65. uuid_characteristic = string.fromHex("EA03"),
  66. }
  67. ble_device:write_value(wr, "FA00EA03 HELLO" .. os.date())
  68. wr = {
  69. uuid_service = string.fromHex("FA00"),
  70. uuid_characteristic = string.fromHex("EA04"),
  71. }
  72. ble_device:write_value(wr, "FA00EA04 HELLO" .. os.date())
  73. end
  74. local function ble_peripheral_setup()
  75. local ret = 0
  76. set_att_db()
  77. sys.wait(500)
  78. ble_state = "开始初始化蓝牙核心"
  79. log.info(ble_state)
  80. bluetooth_device = bluetooth.init()
  81. sys.wait(100)
  82. ble_state = "初始化BLE功能"
  83. log.info(ble_state)
  84. ble_device = bluetooth_device:ble(ble_callback)
  85. if ble_device == nil then
  86. ble_state = "当前固件不支持完整的BLE"
  87. log.error(ble_state)
  88. return
  89. end
  90. sys.wait(100)
  91. ble_state = '开始创建GATT'
  92. log.info(ble_state)
  93. ret = ble_device:gatt_create(att_db)
  94. log.info("创建的GATT", ret)
  95. sys.wait(100)
  96. ble_state = "开始设置广播内容"
  97. log.info(ble_state)
  98. ble_device:adv_create({
  99. addr_mode = ble.PUBLIC,
  100. channel_map = ble.CHNLS_ALL,
  101. intv_min = 120,
  102. intv_max = 120,
  103. adv_data = {
  104. {ble.FLAGS, string.char(0x06)},
  105. {ble.COMPLETE_LOCAL_NAME, "LuatOS_Air8000"},
  106. {ble.SERVICE_DATA, string.fromHex("FE01")},
  107. {ble.MANUFACTURER_SPECIFIC_DATA, string.fromHex("05F0")}
  108. }
  109. })
  110. sys.wait(100)
  111. ble_state = "开始广播"
  112. log.info(ble_state)
  113. ble_device:adv_start()
  114. write_read()
  115. end
  116. local function start_notify_and_ind()
  117. if ble_flag then
  118. local wt = {
  119. uuid_service = string.fromHex("FA00"),
  120. uuid_characteristic = string.fromHex("EA01"),
  121. }
  122. local result = ble_device:write_notify(wt, "123456" .. os.date())
  123. log.info("ble", "发送通知数据", result)
  124. local wi = {
  125. uuid_service = string.fromHex("FA00"),
  126. uuid_characteristic = string.fromHex("EA04"),
  127. }
  128. local result = ble_device:write_indicate(wi, "please read" .. os.date())
  129. log.info("ble", "发送指示数据", result)
  130. ble_state = "通知指示数据已经发送完毕"
  131. end
  132. end
  133. function airble.run()
  134. log.info("airble.run")
  135. lcd.setFont(lcd.font_opposansm12_chinese) -- 设置中文字体
  136. run_state = true
  137. sysplus.taskInitEx(ble_peripheral_setup,"airble")
  138. while true do
  139. sys.wait(10)
  140. lcd.clear(_G.bkcolor)
  141. lcd.drawStr(0,80,"当前蓝牙状态:" .. ble_state )
  142. lcd.drawStr(0,100,"服务:FA00,特征:" .. Characteristic1 .. ",可读数据为:" .. Characteristic1_read.. "被写入数据为:" .. Characteristic1_write)
  143. lcd.drawStr(0,120,"服务:FA00,特征:" .. Characteristic2 .. "被写入数据为:" .. Characteristic2_write)
  144. lcd.drawStr(0,140,"服务:FA00,特征:" .. Characteristic3 .. ",可读数据为:" .. Characteristic3_read)
  145. lcd.drawStr(0,160,"服务:FA00,特征:" .. Characteristic4 .. "可读数据为:" .. Characteristic4_read)
  146. lcd.showImage(130,350,"/luadb/start.jpg") -- EA01 发送数据
  147. lcd.flush()
  148. if not run_state then -- 等待结束,返回主界面
  149. return true
  150. end
  151. end
  152. end
  153. function airble.tp_handal(x,y,event)
  154. log.info("airble.tp_handal",x,y)
  155. if x > 20 and x < 100 and y > 360 and y < 440 then
  156. run_state = false
  157. elseif x > 130 and x < 239 and y > 350 and y < 393 then
  158. sysplus.taskInitEx(start_notify_and_ind, "start_notify_and_ind")
  159. end
  160. end
  161. return airble