deviceConnect3.js 5.9 KB

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