bt.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. --- 模块功能:蓝牙功能测试
  2. -- @author openLuat
  3. -- @module bluetooth.bt
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2020.09.27
  7. -- @注意 需要使用core(Luat_VXXXX_RDA8910_BT_FLOAT)版本
  8. module(..., package.seeall)
  9. local function init()
  10. log.info("bt", "init")
  11. call_state = 0
  12. rtos.on(rtos.MSG_BLUETOOTH, function(msg)
  13. if msg.event == btcore.MSG_OPEN_CNF then
  14. sys.publish("BT_OPEN", msg.result) --蓝牙打开成功
  15. elseif msg.event == btcore.MSG_CLOSE_CNF then
  16. log.info("bt", "ble close") --蓝牙关闭成功
  17. elseif msg.event == btcore.MSG_BT_HFP_CONNECT_IND then
  18. sys.publish("BT_HFP_CONNECT_IND", msg.result) --hfp连接成功
  19. elseif msg.event == btcore.MSG_BT_HFP_DISCONNECT_IND then
  20. log.info("bt", "bt hfp disconnect") --hfp断开连接
  21. elseif msg.event == btcore.MSG_BT_HFP_CALLSETUP_OUTGOING then
  22. log.info("bt", "bt call outgoing") --建立呼出电话
  23. elseif msg.event == btcore.MSG_BT_HFP_CALLSETUP_INCOMING then
  24. log.info("bt", "bt call incoming") --呼叫传入
  25. sys.publish("BT_CALLSETUP_INCOMING", msg.result)
  26. elseif msg.event == btcore.MSG_BT_HFP_RING_INDICATION then
  27. log.info("bt", "bt ring indication") --呼叫传入铃声
  28. elseif msg.event == btcore.MSG_BT_AVRCP_CONNECT_IND then
  29. sys.publish("BT_AVRCP_CONNECT_IND", msg.result) --avrcp连接成功
  30. elseif msg.event == btcore.MSG_BT_AVRCP_DISCONNECT_IND then
  31. log.info("bt", "bt avrcp disconnect") --avrcp断开连接
  32. end
  33. end)
  34. end
  35. local function unInit()
  36. btcore.close()
  37. end
  38. local function poweron()
  39. log.info("bt", "poweron")
  40. btcore.open(2) --打开蓝牙
  41. _, result = sys.waitUntil("BT_OPEN", 5000) --等待蓝牙打开成功
  42. end
  43. local function advertising()
  44. log.info("bt", "advertising")
  45. btcore.setname("Luat_Air724UG")-- 设置广播名称
  46. btcore.setvisibility(0x11)-- 设置蓝牙可见性
  47. log.info("bt", "bt visibility",btcore.getvisibility())
  48. end
  49. local function data_trans()
  50. advertising()
  51. _, result = sys.waitUntil("BT_AVRCP_CONNECT_IND") --等待连接成功
  52. if result ~= 0 then
  53. return false
  54. end
  55. --链接成功
  56. sys.wait(1000)
  57. log.info("bt.send", "Hello I'm Luat BT")
  58. while true do
  59. btcore.setavrcpvol(100)
  60. sys.wait(1000)
  61. log.info("bt", "bt avrcp vol",btcore.getavrcpvol())
  62. sys.wait(1000)
  63. btcore.setavrcpsongs(1)--播放
  64. sys.wait(10000)
  65. btcore.setavrcpsongs(0)--暂停
  66. sys.wait(10000)
  67. btcore.setavrcpsongs(2)--上一曲
  68. sys.wait(10000)
  69. btcore.setavrcpsongs(3)--下一曲
  70. sys.wait(10000)
  71. end
  72. --[[
  73. _, result = sys.waitUntil("BT_HFP_CONNECT_IND") --等待连接成功
  74. if result ~= 0 then
  75. return false
  76. end
  77. --链接成功
  78. sys.wait(1000)
  79. log.info("bt.send", "Hello I'm Luat BT")
  80. while true do
  81. _, result = sys.waitUntil("BT_CALLSETUP_INCOMING")--蓝牙呼叫传入
  82. if result ~= 0 then
  83. return false
  84. end
  85. btcore.hfpcallanswer()--接听
  86. --btcore.hfpcallreject()--拒接
  87. sys.wait(1000)
  88. btcore.sethfpvol(10)--设置音量
  89. sys.wait(15000)
  90. btcore.hfpcallhangup()--挂断
  91. sys.wait(10000)
  92. btcore.hfpcalldial("176******02")--拨号
  93. sys.wait(10000)
  94. btcore.hfpcallhangup()--挂断
  95. sys.wait(10000)
  96. btcore.hfpcallredial()--重拨
  97. sys.wait(10000)
  98. btcore.hfpcallhangup()--挂断
  99. sys.wait(10000)
  100. end
  101. ]]
  102. end
  103. local bt_test = {init, poweron,data_trans}
  104. sys.taskInit(function()
  105. for _, f in ipairs(bt_test) do
  106. f()
  107. end
  108. end)