main.lua 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "ble"
  3. VERSION = "1.0.0"
  4. log.info("main", "project name is ", PROJECT, "version is ", VERSION)
  5. -- 通过boot按键方便刷Air8000S
  6. function PWR8000S(val) gpio.set(23, val) end
  7. gpio.debounce(0, 1000)
  8. gpio.setup(0, function()
  9. sys.taskInit(function()
  10. log.info("复位Air8000S")
  11. PWR8000S(0)
  12. sys.wait(20)
  13. PWR8000S(1)
  14. end)
  15. end, gpio.PULLDOWN)
  16. local scan_count = 0
  17. local function ble_callback(ble_device, ble_event, ble_param)
  18. if ble_event == ble.EVENT_CONN then
  19. log.info("ble", "connect 成功")
  20. elseif ble_event == ble.EVENT_DISCONN then
  21. log.info("ble", "disconnect", ble_param.reason)
  22. sys.timerStart(function() ble_device:scan_start() end, 1000)
  23. elseif ble_event == ble.EVENT_WRITE then
  24. log.info("ble", "write", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex())
  25. log.info("ble", "data", ble_param.data:toHex())
  26. elseif ble_event == ble.EVENT_READ_VALUE then
  27. log.info("ble", "read", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex(),ble_param.data:toHex())
  28. elseif ble_event == ble.EVENT_SCAN_REPORT then
  29. print("ble scan report",ble_param.addr_type,ble_param.rssi,ble_param.adv_addr:toHex(),ble_param.data:toHex())
  30. scan_count = scan_count + 1
  31. if scan_count > 100 then
  32. log.info("ble", "扫描次数超过100次, 停止扫描, 15秒后重新开始")
  33. scan_count = 0
  34. ble_device:scan_stop()
  35. sys.timerStart(function() ble_device:scan_start() end, 15000)
  36. end
  37. -- 注意, 这里是连接到另外一个设备, 设备名称带LuatOS字样
  38. if ble_param.addr_type == 0 and ble_param.data:find("LuatOS") then
  39. log.info("ble", "停止扫描, 连接设备", ble_param.adv_addr:toHex(), ble_param.addr_type)
  40. ble_device:scan_stop()
  41. ble_device:connect(ble_param.adv_addr,ble_param.addr_type)
  42. end
  43. elseif ble_event == ble.EVENT_GATT_ITEM then
  44. -- 读取GATT完成, 打印出来
  45. log.info("ble", "gatt item", ble_param)
  46. elseif ble_event == ble.EVENT_GATT_DONE then
  47. log.info("ble", "gatt done", ble_param.service_num)
  48. local wt = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA01")}
  49. ble_device:notify_enable(wt, true) -- 开启通知
  50. -- 主动写入数据, 但不带通知, 带通知是 write_notify
  51. local wt = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA02")}
  52. ble_device:write_value(wt,string.fromHex("1234"))
  53. local wt = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA03")}
  54. ble_device:read_value(wt)
  55. end
  56. end
  57. sys.taskInit(function()
  58. log.info("开始初始化蓝牙核心")
  59. bluetooth_device = bluetooth.init()
  60. log.info("初始化BLE功能")
  61. ble_device = bluetooth_device:ble(ble_callback)
  62. -- master
  63. ble_device:scan_create({})
  64. ble_device:scan_start()
  65. -- ble_device:scan_stop()
  66. end)
  67. -- 用户代码已结束---------------------------------------------
  68. -- 结尾总是这一句
  69. sys.run()
  70. -- sys.run()之后后面不要加任何语句!!!!!