deviceConnect2.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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: function(e) {
  130. if(!this.data.islanya) {
  131. return;
  132. };
  133. const deviceIdData = this.data.devicesList;
  134. if(deviceIdData.length < 1) {
  135. wx.showToast({
  136. title: '请短按设备配对键',
  137. icon: 'none',
  138. duration: 2000
  139. });
  140. return;
  141. };
  142. if(this.data.isSubmit) {
  143. return;
  144. };
  145. this.setData({
  146. isSubmit: true
  147. });
  148. // 停止搜索
  149. xBlufi.notifyStartDiscoverBle({
  150. 'isStart': false,
  151. });
  152. //开始连接
  153. let name = deviceIdData[0].name;
  154. // console.log('点击了,蓝牙准备连接的deviceId:' + deviceIdData[0].deviceId)
  155. xBlufi.notifyConnectBle({
  156. isStart: true,
  157. deviceId: deviceIdData[0].deviceId,
  158. name
  159. });
  160. wx.showLoading({
  161. title: '请稍后',
  162. });
  163. },
  164. onUnload: function() {
  165. xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
  166. },
  167. getBluetoothStatus() {
  168. app.getBluetoothStatus();
  169. },
  170. notRoter(){
  171. wx.navigateBack({
  172. delta: 1
  173. });
  174. },
  175. })