deviceConnect2.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // pages/deviceConnect0/deviceConnect0.ts
  2. const app = getApp();
  3. let xBlufi = require("../../utils/blufi/xBlufi.js");
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. nvabarData: {
  10. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  11. title: '连接配网', //导航栏 中间的标题
  12. },
  13. devicesList: [],
  14. searching: false,
  15. version: '2.0',
  16. name: '',
  17. connectedDeviceId: '',
  18. connected: true,
  19. deviceInfo: null,
  20. isInitOK: false,
  21. customData: '',
  22. isOne: true,
  23. isSubmit: false,
  24. islanya: false,
  25. isOnBluetooth: false,
  26. },
  27. onShow() {
  28. if (!this.data.isOne) {
  29. this.Search();
  30. }
  31. },
  32. /**
  33. * 生命周期函数--监听页面加载
  34. */
  35. onLoad() {
  36. const _this = this;
  37. const isOne = setTimeout(() => {
  38. clearTimeout(isOne);
  39. if (_this.data.isOne) {
  40. _this.setData({
  41. isOne: false,
  42. });
  43. }
  44. }, 2000);
  45. if (app.globalData.oneInitBluetooth) {
  46. app.globalData.oneInitBluetooth = false;
  47. xBlufi.initXBlufi(1);
  48. };
  49. xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
  50. this.Search();
  51. },
  52. funListenDeviceMsgEvent: function (options) {
  53. const _this = this;
  54. switch (options.type) {
  55. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
  56. // 搜索到蓝牙
  57. // console.log("gadsfadsfadsfa==xxx===" + JSON.stringify(options));
  58. if (options.result) {
  59. if (options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1).length > 0) {
  60. _this.setData({
  61. islanya: true,
  62. devicesList: options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1)
  63. });
  64. }
  65. }
  66. break;
  67. case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
  68. // console.log("连接回调:" + JSON.stringify(options))
  69. if (options.result) {
  70. wx.hideLoading();
  71. if (_this.data.isOnBluetooth) {
  72. return;
  73. };
  74. _this.setData({
  75. isOnBluetooth: true,
  76. });
  77. setTimeout(() => {
  78. _this.setData({
  79. isSubmit: false,
  80. islanya: false,
  81. devicesList: [],
  82. isOnBluetooth: false,
  83. });
  84. wx.navigateTo({
  85. url: '../deviceConnect3/deviceConnect3?deviceId=' + options.data.deviceId + '&name=' + options.data.name,
  86. });
  87. }, 500);
  88. } else {
  89. wx.hideLoading()
  90. wx.showModal({
  91. title: '提示',
  92. content: '连接失败',
  93. showCancel: false
  94. });
  95. }
  96. break;
  97. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
  98. if (!options.result) {
  99. console.log("蓝牙未开启 fail =》", options)
  100. wx.showToast({
  101. title: '蓝牙未开启',
  102. icon: 'none'
  103. })
  104. } else {
  105. //蓝牙搜索开始
  106. _this.setData({
  107. searching: true
  108. });
  109. }
  110. break;
  111. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
  112. if (options.result) {
  113. //蓝牙停止搜索ok
  114. console.log('蓝牙停止搜索ok')
  115. } else {
  116. //蓝牙停止搜索失败
  117. console.log('蓝牙停止搜索失败')
  118. }
  119. _this.setData({
  120. searching: false
  121. });
  122. break;
  123. }
  124. },
  125. Search: function () {
  126. xBlufi.notifyStartDiscoverBle({
  127. 'isStart': true,
  128. });
  129. },
  130. Connect: function (e) {
  131. // if(!this.data.islanya) {
  132. // return;
  133. // };
  134. const deviceIdData = this.data.devicesList;
  135. if (deviceIdData.length < 1) {
  136. wx.showToast({
  137. title: '请短按设备配对键',
  138. icon: 'none',
  139. duration: 2000
  140. });
  141. return;
  142. };
  143. if (this.data.isSubmit) {
  144. return;
  145. };
  146. this.setData({
  147. isSubmit: true
  148. });
  149. // 停止搜索
  150. xBlufi.notifyStartDiscoverBle({
  151. 'isStart': false,
  152. });
  153. //开始连接
  154. let name = deviceIdData[0].name;
  155. // console.log('点击了,蓝牙准备连接的deviceId:' + deviceIdData[0].deviceId)
  156. xBlufi.notifyConnectBle({
  157. isStart: true,
  158. deviceId: deviceIdData[0].deviceId,
  159. name
  160. });
  161. wx.showLoading({
  162. title: '请稍后',
  163. });
  164. },
  165. onUnload: function () {
  166. xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
  167. },
  168. getBluetoothStatus() {
  169. app.getBluetoothStatus();
  170. },
  171. notRoter() {
  172. wx.navigateBack({
  173. delta: 1
  174. });
  175. },
  176. })