deviceConnect4.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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. });
  79. // BtHelper.getInstance().startScan(device,
  80. // async function (b) {
  81. // if (!b) {
  82. // that.setData({
  83. // connected: false
  84. // })
  85. // that.setData({
  86. // ruterStatus: 2
  87. // });
  88. // }
  89. // setTimeout(function () {
  90. // BleUtil.getInstance().startConnect(device, function (changed) {
  91. // if (changed) {
  92. // BleUtil.getInstance().sendData(device, app.globalData.ssid, app.globalData.pwdData);
  93. // }
  94. // });
  95. // }, 10 * 1000);
  96. // },
  97. // async function (res) {
  98. // var deviceId = that.data.name;
  99. // var device = {
  100. // connectType: 3,
  101. // devName: "",
  102. // state: "online",
  103. // name: deviceId,
  104. // deviceId: deviceId,
  105. // mac: deviceId,
  106. // imageUrl: that.data.imageUrl,
  107. // // imageUrl: "./../../img/min.png",
  108. // }
  109. // getCurrentPages()[0].addWifiDevice(device);
  110. // // 记住密码
  111. // wx.setStorage({
  112. // key: "wifiInfo",
  113. // data: JSON.stringify({
  114. // "password": app.globalData.pwdData,
  115. // "ssid": app.globalData.ssid
  116. // }),
  117. // });
  118. // setTimeout(() => {
  119. // clearInterval(percentIn);
  120. // that.setData({
  121. // percent: 100,
  122. // ruterStatus: 1,
  123. // });
  124. // }, 500);
  125. // }
  126. // );
  127. },
  128. })