deviceConnect3.js 6.1 KB

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