deviceConnect4.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const app = getApp();
  2. import strings from '../../utils/strings.js';
  3. const {
  4. BtHelper
  5. } = require('../../devices/bt_helper');
  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. console.log("gadsfasdfadfaf===00==");
  56. percentIn = setInterval(() => {
  57. if (that.data.percent === 100) {
  58. clearInterval(percentIn);
  59. ///失败
  60. that.setData({
  61. ruterStatus: 2
  62. });
  63. return;
  64. };
  65. that.data.percent++;
  66. that.setData({
  67. percent: that.data.percent
  68. });
  69. }, 600);
  70. BtHelper.getInstance().startScan(device,
  71. async function (b) {
  72. if (!b) {
  73. that.setData({
  74. connected: false
  75. })
  76. that.setData({
  77. ruterStatus: 2
  78. });
  79. }
  80. setTimeout(function () {
  81. BleUtil.getInstance().startConnect(device, function (changed) {
  82. if (changed) {
  83. BleUtil.getInstance().sendData(device, app.globalData.ssid, app.globalData.pwdData);
  84. }
  85. });
  86. }, 10 * 1000);
  87. },
  88. async function (res) {
  89. var deviceId = that.data.name;
  90. var device = {
  91. connectType: 3,
  92. devName: "",
  93. state: "online",
  94. name: deviceId,
  95. deviceId: deviceId,
  96. mac: deviceId,
  97. imageUrl: that.data.imageUrl,
  98. // imageUrl: "./../../img/min.png",
  99. }
  100. getCurrentPages()[0].addWifiDevice(device);
  101. // 记住密码
  102. wx.setStorage({
  103. key: "wifiInfo",
  104. data: JSON.stringify({
  105. "password": app.globalData.pwdData,
  106. "ssid": app.globalData.ssid
  107. }),
  108. });
  109. setTimeout(() => {
  110. clearInterval(percentIn);
  111. that.setData({
  112. percent: 100,
  113. ruterStatus: 1,
  114. });
  115. }, 500);
  116. }
  117. );
  118. },
  119. })