deviceConnect2.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. if (options.result) {
  58. if (options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1).length > 0) {
  59. _this.setData({
  60. islanya: true,
  61. devicesList: options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1)
  62. });
  63. }
  64. }
  65. break;
  66. case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
  67. // console.log("连接回调:" + JSON.stringify(options))
  68. if (options.result) {
  69. wx.hideLoading();
  70. if (_this.data.isOnBluetooth) {
  71. return;
  72. };
  73. _this.setData({
  74. isOnBluetooth: true,
  75. });
  76. setTimeout(() => {
  77. _this.setData({
  78. isSubmit: false,
  79. islanya: false,
  80. devicesList: [],
  81. isOnBluetooth: false,
  82. });
  83. wx.navigateTo({
  84. url: '../deviceConnect3/deviceConnect3?deviceId=' + options.data.deviceId + '&name=' + options.data.name,
  85. });
  86. }, 500);
  87. } else {
  88. wx.hideLoading()
  89. wx.showModal({
  90. title: '提示',
  91. content: '连接失败',
  92. showCancel: false
  93. });
  94. }
  95. break;
  96. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
  97. if (!options.result) {
  98. console.log("蓝牙未开启 fail =》", options)
  99. wx.showToast({
  100. title: '蓝牙未开启',
  101. icon: 'none'
  102. })
  103. } else {
  104. //蓝牙搜索开始
  105. _this.setData({
  106. searching: true
  107. });
  108. }
  109. break;
  110. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
  111. if (options.result) {
  112. //蓝牙停止搜索ok
  113. console.log('蓝牙停止搜索ok')
  114. } else {
  115. //蓝牙停止搜索失败
  116. console.log('蓝牙停止搜索失败')
  117. }
  118. _this.setData({
  119. searching: false
  120. });
  121. break;
  122. }
  123. },
  124. Search: function () {
  125. xBlufi.notifyStartDiscoverBle({
  126. 'isStart': true,
  127. });
  128. },
  129. Connect: async function (e) {
  130. // if(!this.data.islanya) {
  131. // return;
  132. // };
  133. // [{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24","RSSI":-51,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fcbb24","serviceData":{}}]
  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. await getCurrentPages()[0].cancelCurrent();
  150. // 停止搜索
  151. xBlufi.notifyStartDiscoverBle({
  152. 'isStart': false,
  153. });
  154. //开始连接
  155. let name = deviceIdData[0].name;
  156. // console.log('点击了,蓝牙准备连接的deviceId:' + deviceIdData[0].deviceId)
  157. xBlufi.notifyConnectBle({
  158. isStart: true,
  159. deviceId: deviceIdData[0].deviceId,
  160. name
  161. });
  162. wx.showLoading({
  163. title: '请稍后',
  164. });
  165. },
  166. onUnload: function () {
  167. xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
  168. },
  169. getBluetoothStatus() {
  170. app.getBluetoothStatus();
  171. },
  172. notRoter() {
  173. wx.navigateBack({
  174. delta: 1
  175. });
  176. },
  177. })