deviceConnect4.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const app = getApp();
  2. import strings from '../../../utils/strings.js';
  3. import routeUtil from '../../../utils/routeUtil.js';
  4. import routeRoot from '../../../utils/routeRoot.js'
  5. const {
  6. BleUtil
  7. } = require('../../../utils/blue_ble/util');
  8. let percentIn = null;
  9. ///君正方案
  10. Page({
  11. data: {
  12. nvabarData: {
  13. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  14. title: '连接配网', //导航栏 中间的标题
  15. },
  16. scopeBluetooth: app.globalData.scopeBluetooth,
  17. ssid: app.globalData.ssid,
  18. password: app.globalData.pwdData,
  19. version: '2.0',
  20. name: '',
  21. connectedDeviceId: '',
  22. connected: true,
  23. deviceInfo: null,
  24. isInitOK: false,
  25. customData: '',
  26. percent: 0,
  27. ruterStatus: 0, // 0 连接中 // 1 成功 // 2失败
  28. imageUrl: "./../../../img/g.png",
  29. isAiDevice: false,
  30. },
  31. onLoad: function (options) {
  32. var that = this;
  33. var param = options.param;
  34. if (!strings.isEmpty(param)) {
  35. param = JSON.parse(param);
  36. that.setData({
  37. imageUrl: param.img,
  38. });
  39. }
  40. this.data.isAiDevice = param.clientType === "MW-2AXD";
  41. console.log("设备连接页面参数:", this.data.isAiDevice, ":", param);
  42. // console.log("设备连接页面参数:", param);
  43. // AIrSMArT_861210052356337===D0:31:10:86:AC:9A
  44. /// 纯蓝牙:MW_BLE CC:90:DD:88:C7:E8
  45. var name = options.name;
  46. var deviceId = options.deviceId;
  47. that.setData({
  48. name: name,
  49. connectedDeviceId: deviceId,
  50. });
  51. var device = {
  52. "mac": name,
  53. "deviceId": deviceId
  54. };
  55. // var device = {
  56. // "mac": "MW_BLE",
  57. // "deviceId": "CC:90:DD:88:C7:E8"
  58. // };
  59. percentIn = setInterval(() => {
  60. if (that.data.percent === 100) {
  61. clearInterval(percentIn);
  62. ///失败
  63. that.setData({
  64. ruterStatus: 2
  65. });
  66. return;
  67. };
  68. that.data.percent++;
  69. that.setData({
  70. percent: that.data.percent
  71. });
  72. }, 600);
  73. BleUtil.getInstance().startConnect(device, function (changed) {
  74. if (changed) {
  75. BleUtil.getInstance().sendData(device, app.globalData.ssid, app.globalData.pwdData);
  76. } else {
  77. that.setData({
  78. ruterStatus: 2
  79. });
  80. clearInterval(percentIn);
  81. }
  82. }, function (connected) {
  83. var deviceId = that.data.name;
  84. var device = {
  85. connectType: 3,
  86. devName: "",
  87. state: "online",
  88. name: deviceId,
  89. deviceId: deviceId,
  90. mac: deviceId,
  91. imageUrl: that.data.imageUrl,
  92. }
  93. getCurrentPages()[0].addWifiDevice(device);
  94. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  95. // 记住密码
  96. wx.setStorage({
  97. key: "wifiInfo",
  98. data: JSON.stringify({
  99. "password": app.globalData.pwdData,
  100. "ssid": app.globalData.ssid
  101. }),
  102. });
  103. setTimeout(() => {
  104. if (percentIn != null) {
  105. clearInterval(percentIn);
  106. that.setData({
  107. percent: 100,
  108. ruterStatus: 1,
  109. });
  110. }
  111. }, 1000);
  112. });
  113. },
  114. egen() {
  115. wx.navigateBack({
  116. delta: 1
  117. })
  118. },
  119. goIndex() {
  120. routeUtil.goBackRoute(routeRoot.index);
  121. },
  122. })