deviceConnect3.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const app = getApp();
  2. let xBlufi = require("../../../utils/blufi/xBlufi.js");
  3. import strings from '../../../utils/strings.js';
  4. import routeUtil from '../../../utils/routeUtil.js';
  5. import routeRoot from '../../../utils/routeRoot.js'
  6. let percentIn = null;
  7. let errTi = null;
  8. Page({
  9. data: {
  10. nvabarData: {
  11. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  12. title: '连接配网', //导航栏 中间的标题
  13. },
  14. scopeBluetooth: app.globalData.scopeBluetooth,
  15. ssid: app.globalData.ssid,
  16. password: app.globalData.pwdData,
  17. version: '2.0',
  18. name: '',
  19. connectedDeviceId: '',
  20. connected: true,
  21. deviceInfo: null,
  22. isInitOK: false,
  23. customData: '',
  24. percent: 0,
  25. ruterStatus: 0, // 0 连接中 // 1 成功 // 2失败
  26. imageUrl: "./../../../img/g.png",
  27. },
  28. onLoad: function (options) {
  29. var that = this;
  30. var param = options.param;
  31. if (!strings.isEmpty(param)) {
  32. param = JSON.parse(param);
  33. that.setData({
  34. imageUrl: param.img,
  35. });
  36. }
  37. // AIrSMArT_861210052356337===D0:31:10:86:AC:9A
  38. that.setData({
  39. name: options.name,
  40. connectedDeviceId: options.deviceId,
  41. })
  42. xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
  43. ///通知初始化获取设备的服务列表等信息
  44. xBlufi.notifyInitBleEsp32({
  45. deviceId: options.deviceId,
  46. });
  47. percentIn = setInterval(() => {
  48. if (that.data.percent === 100) {
  49. clearInterval(percentIn);
  50. ///失败
  51. that.setData({
  52. ruterStatus: 2
  53. });
  54. return;
  55. };
  56. that.data.percent++;
  57. that.setData({
  58. percent: that.data.percent
  59. });
  60. }, 600);
  61. },
  62. // {"type":"2","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","serviceId":"0000FFFF-0000-1000-8000-00805F9B34FB","characteristicId":"0000FF01-0000-1000-8000-00805F9B34FB"}}
  63. // {"type":"4","result":false,"data":{"progress":0,"ssid":""}}
  64. // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}}
  65. funListenDeviceMsgEvent: function (options) {
  66. var that = this;
  67. switch (options.type) {
  68. // {"type":"2","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","serviceId":"0000FFFF-0000-1000-8000-00805F9B34FB",
  69. // "characteristicId":"0000FF01-0000-1000-8000-00805F9B34FB"}}
  70. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  71. if (options.result) {
  72. console.log('初始化成功');
  73. that.OnClickStart();
  74. } else {
  75. console.log('初始化失败')
  76. that.setData({
  77. connected: false
  78. })
  79. that.setData({
  80. ruterStatus: 2
  81. });
  82. }
  83. break;
  84. // {"type":"4","result":false,"data":{"progress":0,"ssid":""}}
  85. // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}}
  86. ///连接结果
  87. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  88. if (!options.result) {
  89. errTi = setTimeout(() => {
  90. that.setData({
  91. ruterStatus: 2
  92. });
  93. clearTimeout(errTi);
  94. clearInterval(percentIn);
  95. }, 5000);
  96. } else {
  97. // 配网成功
  98. if (options.data.progress == 100) {
  99. var deviceId = that.data.name;
  100. var device = {
  101. connectType: 3,
  102. devName: "",
  103. state: "online",
  104. name: deviceId,
  105. deviceId: deviceId,
  106. mac: deviceId,
  107. imageUrl: that.data.imageUrl,
  108. }
  109. getCurrentPages()[0].addWifiDevice(device);
  110. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  111. // 记住密码
  112. wx.setStorage({
  113. key: "wifiInfo",
  114. data: JSON.stringify({
  115. "password": app.globalData.pwdData,
  116. "ssid": app.globalData.ssid
  117. }),
  118. });
  119. setTimeout(() => {
  120. if (percentIn != null) {
  121. clearTimeout(errTi);
  122. clearInterval(percentIn);
  123. that.setData({
  124. percent: 100,
  125. ruterStatus: 1,
  126. });
  127. }
  128. }, 500);
  129. }
  130. }
  131. break;
  132. ///设备连接状态回调
  133. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  134. that.setData({
  135. connected: options.result
  136. });
  137. if (!options.result) {
  138. wx.showModal({
  139. title: '很抱歉提醒你!',
  140. content: '小程序与设备异常断开',
  141. showCancel: false, //是否显示取消按钮
  142. success: function (res) {
  143. wx.navigateBack({
  144. delta: 1
  145. });
  146. },
  147. });
  148. }
  149. break;
  150. }
  151. },
  152. ///校验wifi账号密码
  153. OnClickStart: function () {
  154. if (!app.globalData.ssid) {
  155. wx.showToast({
  156. title: 'SSID不能为空',
  157. icon: 'none'
  158. })
  159. return
  160. }
  161. // if (!app.globalData.pwdData) {
  162. // wx.showToast({
  163. // title: '密码不能为空',
  164. // icon: 'none'
  165. // })
  166. // return;
  167. // }
  168. xBlufi.notifySendRouterSsidAndPassword({
  169. ssid: app.globalData.ssid,
  170. password: app.globalData.pwdData
  171. });
  172. },
  173. bindPasswordInput: function (e) {
  174. var that = this;
  175. that.setData({
  176. password: e.detail.value
  177. })
  178. },
  179. bindCustomDataInput: function (e) {
  180. var that = this;
  181. that.setData({
  182. customData: e.detail.value
  183. })
  184. },
  185. egen() {
  186. wx.navigateBack({
  187. delta: 1
  188. })
  189. },
  190. goIndex() {
  191. routeUtil.goBackRoute(routeRoot.index);
  192. },
  193. onUnload: function () {
  194. // 关闭蓝牙连接
  195. var that = this
  196. xBlufi.notifyConnectBle({
  197. isStart: false,
  198. deviceId: that.data.connectedDeviceId,
  199. name: that.data.name,
  200. });
  201. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  202. clearTimeout(errTi);
  203. clearInterval(percentIn);
  204. },
  205. })