deviceConnect4.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. },
  30. onLoad: function (options) {
  31. var that = this;
  32. var param = options.param;
  33. if (!strings.isEmpty(param)) {
  34. param = JSON.parse(param);
  35. that.setData({
  36. imageUrl: param.img,
  37. });
  38. }
  39. // AIrSMArT_861210052356337===D0:31:10:86:AC:9A
  40. /// 纯蓝牙:MW_BLE CC:90:DD:88:C7:E8
  41. var name = options.name;
  42. var deviceId = options.deviceId;
  43. that.setData({
  44. name: name,
  45. connectedDeviceId: deviceId,
  46. });
  47. var device = {
  48. "mac": name,
  49. "deviceId": deviceId
  50. };
  51. // var device = {
  52. // "mac": "MW_BLE",
  53. // "deviceId": "CC:90:DD:88:C7:E8"
  54. // };
  55. percentIn = setInterval(() => {
  56. if (that.data.percent === 100) {
  57. clearInterval(percentIn);
  58. ///失败
  59. that.setData({
  60. ruterStatus: 2
  61. });
  62. return;
  63. };
  64. that.data.percent++;
  65. that.setData({
  66. percent: that.data.percent
  67. });
  68. }, 600);
  69. BleUtil.getInstance().startConnect(device, function (changed) {
  70. if (changed) {
  71. BleUtil.getInstance().sendData(device, app.globalData.ssid, app.globalData.pwdData);
  72. } else {
  73. that.setData({
  74. ruterStatus: 2
  75. });
  76. clearInterval(percentIn);
  77. }
  78. }, function (connected) {
  79. var deviceId = that.data.name;
  80. var device = {
  81. connectType: 3,
  82. devName: "",
  83. state: "online",
  84. name: deviceId,
  85. deviceId: deviceId,
  86. mac: deviceId,
  87. imageUrl: that.data.imageUrl,
  88. }
  89. getCurrentPages()[0].addWifiDevice(device);
  90. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  91. // 记住密码
  92. wx.setStorage({
  93. key: "wifiInfo",
  94. data: JSON.stringify({
  95. "password": app.globalData.pwdData,
  96. "ssid": app.globalData.ssid
  97. }),
  98. });
  99. setTimeout(() => {
  100. if (percentIn != null) {
  101. clearInterval(percentIn);
  102. that.setData({
  103. percent: 100,
  104. ruterStatus: 1,
  105. });
  106. }
  107. }, 1000);
  108. });
  109. },
  110. egen() {
  111. wx.navigateBack({
  112. delta: 1
  113. })
  114. },
  115. goIndex() {
  116. routeUtil.goBackRoute(routeRoot.index);
  117. },
  118. })