deviceConnect3.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // pages/deviceConnect3/deviceConnect3.ts
  2. const app = getApp();
  3. let xBlufi = require("../../utils/blufi/xBlufi.js");
  4. import strings from '../../utils/strings.js';
  5. import route_util from '../../utils/route_util.js';
  6. import route_constant from '../../utils/route_constant.js'
  7. let percentIn = null;
  8. let errTi = null;
  9. Page({
  10. data: {
  11. nvabarData: {
  12. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  13. title: '连接配网', //导航栏 中间的标题
  14. },
  15. scopeBluetooth: app.globalData.scopeBluetooth,
  16. ssid: app.globalData.ssid,
  17. password: app.globalData.pwdData,
  18. version: '2.0',
  19. name: '',
  20. connectedDeviceId: '',
  21. connected: true,
  22. deviceInfo: null,
  23. isInitOK: false,
  24. customData: '',
  25. percent: 0,
  26. ruterStatus: 0, // 0 连接中 // 1 成功 // 2失败
  27. imageUrl: "./../../img/g.png",
  28. },
  29. onLoad: function (options) {
  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. console.log("gadfqwerqwerqr==222==" + JSON.stringify(options.result));
  74. if (options.result) {
  75. console.log('初始化成功');
  76. that.OnClickStart();
  77. } else {
  78. console.log('初始化失败')
  79. that.setData({
  80. connected: false
  81. })
  82. that.setData({
  83. ruterStatus: 2
  84. });
  85. }
  86. break;
  87. // {"type":"4","result":false,"data":{"progress":0,"ssid":""}}
  88. // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}}
  89. ///连接结果
  90. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  91. console.log("gadfqwerqwerqr==2222==" + JSON.stringify(options.result));
  92. if (!options.result) {
  93. errTi = setTimeout(() => {
  94. that.setData({
  95. ruterStatus: 2
  96. });
  97. clearTimeout(errTi);
  98. clearInterval(percentIn);
  99. }, 5000);
  100. } else {
  101. // 配网成功
  102. if (options.data.progress == 100) {
  103. var deviceId = that.data.name;
  104. var device = {
  105. connectType: 3,
  106. devName: "",
  107. state: "online",
  108. name: deviceId,
  109. deviceId: deviceId,
  110. mac: deviceId,
  111. imageUrl: that.data.imageUrl,
  112. // imageUrl: "./../../img/min.png",
  113. }
  114. getCurrentPages()[0].addWifiDevice(device);
  115. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  116. // 记住密码
  117. wx.setStorage({
  118. key: "wifiInfo",
  119. data: JSON.stringify({
  120. "password": app.globalData.pwdData,
  121. "ssid": app.globalData.ssid
  122. }),
  123. });
  124. setTimeout(() => {
  125. clearTimeout(errTi);
  126. clearInterval(percentIn);
  127. that.setData({
  128. percent: 100,
  129. ruterStatus: 1,
  130. });
  131. }, 500);
  132. }
  133. }
  134. break;
  135. ///设备连接状态回调
  136. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  137. console.log("gadfqwerqwerqr==4444==" + JSON.stringify(options.result));
  138. that.setData({
  139. connected: options.result
  140. });
  141. if (!options.result) {
  142. wx.showModal({
  143. title: '很抱歉提醒你!',
  144. content: '小程序与设备异常断开',
  145. showCancel: false, //是否显示取消按钮
  146. success: function (res) {
  147. wx.navigateBack({
  148. delta: 1
  149. });
  150. },
  151. });
  152. }
  153. break;
  154. }
  155. },
  156. ///校验wifi账号密码
  157. OnClickStart: function () {
  158. if (!app.globalData.ssid) {
  159. wx.showToast({
  160. title: 'SSID不能为空',
  161. icon: 'none'
  162. })
  163. return
  164. }
  165. if (!app.globalData.pwdData) {
  166. wx.showToast({
  167. title: '密码不能为空',
  168. icon: 'none'
  169. })
  170. return;
  171. }
  172. xBlufi.notifySendRouterSsidAndPassword({
  173. ssid: app.globalData.ssid,
  174. password: app.globalData.pwdData
  175. });
  176. },
  177. bindPasswordInput: function (e) {
  178. var that = this;
  179. that.setData({
  180. password: e.detail.value
  181. })
  182. },
  183. bindCustomDataInput: function (e) {
  184. var that = this;
  185. that.setData({
  186. customData: e.detail.value
  187. })
  188. },
  189. egen() {
  190. wx.navigateBack({
  191. delta: 1
  192. })
  193. },
  194. goIndex() {
  195. route_util.goBackRoute(route_constant.indexRoot);
  196. },
  197. onUnload: function () {
  198. // 关闭蓝牙连接
  199. var that = this
  200. xBlufi.notifyConnectBle({
  201. isStart: false,
  202. deviceId: that.data.connectedDeviceId,
  203. name: that.data.name,
  204. });
  205. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  206. clearTimeout(errTi);
  207. clearInterval(percentIn);
  208. },
  209. })