deviceConnect4.js 3.0 KB

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