deviceConnect4.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const app = getApp();
  2. import strings from '../../utils/strings.js';
  3. import routeUtil from '../../utils/routeUtil.js';
  4. import routePath from '../../utils/routePath.js'
  5. import routeRoot from '../../utils/routeRoot.js'
  6. const {
  7. BleUtil
  8. } = require('../../utils/blue_ble/util');
  9. let percentIn = null;
  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. // imageUrl: "./../../img/min.png",
  89. }
  90. getCurrentPages()[0].addWifiDevice(device);
  91. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  92. // 记住密码
  93. wx.setStorage({
  94. key: "wifiInfo",
  95. data: JSON.stringify({
  96. "password": app.globalData.pwdData,
  97. "ssid": app.globalData.ssid
  98. }),
  99. });
  100. setTimeout(() => {
  101. if (percentIn != null) {
  102. clearInterval(percentIn);
  103. that.setData({
  104. percent: 100,
  105. ruterStatus: 1,
  106. });
  107. }
  108. }, 1000);
  109. });
  110. },
  111. egen() {
  112. wx.navigateBack({
  113. delta: 1
  114. })
  115. },
  116. goIndex() {
  117. routeUtil.goBackRoute(routeRoot.indexRoot);
  118. },
  119. })