search.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // pages/search/search.js
  2. const tqlSDK = getApp().globalData.tqlSDK
  3. const app = getApp()
  4. import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
  5. const event = require('../../tqlsdk/event')
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. blueToothList: [],
  12. refreshFlag: false,
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. this.searchBlueTooth();
  19. event.remove('foundDevices')
  20. event.on('foundDevices', this, data => {
  21. this.setData({
  22. blueToothList: data
  23. })
  24. })
  25. },
  26. /**
  27. * 生命周期函数--监听页面初次渲染完成
  28. */
  29. onReady: function () {
  30. },
  31. /**
  32. * 生命周期函数--监听页面显示
  33. */
  34. onShow: function () {
  35. },
  36. /**
  37. * 生命周期函数--监听页面隐藏
  38. */
  39. onHide: function () {
  40. },
  41. /**
  42. * 生命周期函数--监听页面卸载
  43. */
  44. onUnload: function () {
  45. },
  46. /**
  47. * 页面相关事件处理函数--监听用户下拉动作
  48. */
  49. onPullDownRefresh: function () {
  50. },
  51. /**
  52. * 页面上拉触底事件的处理函数
  53. */
  54. onReachBottom: function () {
  55. },
  56. /**
  57. * 用户点击右上角分享
  58. */
  59. onShareAppMessage: function () {
  60. },
  61. // 搜索附近的蓝牙
  62. searchBlueTooth() {
  63. tqlSDK.searchBlueTooth(this.blueToothList)
  64. },
  65. // 查找到的设备列表
  66. blueToothList() {
  67. tqlSDK.showBlueToothList(this)
  68. },
  69. // 连接蓝牙
  70. connectBlueTooth(e) {
  71. Toast.clear()
  72. Toast.loading({
  73. message: '正在连接',
  74. forbidClick: true,
  75. duration: 0
  76. })
  77. tqlSDK.stopBlueToothSearch(() => {})
  78. console.log(e)
  79. let deviceId = e.currentTarget.dataset.deviceid
  80. let penName = e.currentTarget.dataset.devicename
  81. let mac = e.currentTarget.dataset.mac
  82. setTimeout(() => {
  83. tqlSDK.createBlueToothConnection(deviceId, () => {
  84. app.globalData.deviceId = deviceId
  85. app.globalData.deviceName = penName
  86. app.globalData.deviceMac = mac
  87. // event.emit('blueToothConnect', true)
  88. this.getBlueToothService(deviceId)
  89. }, () => {
  90. Toast.clear()
  91. })
  92. }, 1000)
  93. },
  94. // 获取蓝牙设备服务
  95. getBlueToothService(deviceId) {
  96. tqlSDK.getBlueToothService(deviceId, () => {
  97. setTimeout(() => {
  98. Toast.clear();
  99. Toast.success({
  100. message: "连接成功",
  101. duration: 2500,
  102. onClose: () => {
  103. wx.navigateBack({
  104. delta: 0,
  105. success: () => {
  106. // setTimeout(() => {
  107. tqlSDK.getPenOfflineDataSize()
  108. // },100)
  109. }
  110. })
  111. }
  112. })
  113. }, 500);
  114. })
  115. },
  116. //刷新列表
  117. refreshList() {
  118. setTimeout(() => {
  119. this.setData({
  120. refreshFlag: false
  121. })
  122. tqlSDK.allDevices.foundDevices = [];
  123. this.searchBlueTooth();
  124. }, 1500)
  125. }
  126. })