addMattresses.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. // subpages/addMattresses/addMattresses.js
  2. const app = getApp();
  3. const util = require('../../utils/util.js');
  4. const timeOut = 40; //超时时间
  5. var timeId = "";
  6. var sequenceControl = 0;
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. isEdit: false,
  13. showBluetoothList: false,
  14. showBluetoothConnected: false,//连接中
  15. isBluetoothConnected: false,//连接成功
  16. bluetoothConnectedStep: 0,
  17. placeOfUse: "",
  18. usingHuman: "",
  19. pid: "",
  20. sn: "",
  21. ssid: "",
  22. password: "",
  23. bluetoothSelectIndex: -1,
  24. activeIcon: "/subpages/images/ic_blue_radio_check.png",
  25. normalIcon: "/subpages/images/ic_blue_radio_uncheck.png",
  26. deviceList: [],
  27. deviceId: "",
  28. deviceIds: [],
  29. wifiInfo: null,
  30. deviceId: "",
  31. password: "",
  32. notice: "",
  33. value: 90,
  34. gradientColor: {
  35. '0%': '#00CEB2',
  36. '50%': '#00C7FF',
  37. '100%': '#53DAFF',
  38. },
  39. statusDefault: ['蓝牙连接...', '蓝牙连接成功', '成功获取设备信息', '获取属性信息成功', '发送配置信息...', '配置信息发送成功', '连接成功'],
  40. descFailList: ["蓝牙连接失败", "获取设备信息失败", "获取属性信息失败", "发送配置信息失败", "分配网络失败"],
  41. failure: false,
  42. value: 0,
  43. desc: "设备连接...",
  44. blueConnectNum: 0,
  45. isChecksum: true,
  46. isEncrypt: false,
  47. flagEnd: false,
  48. defaultData: 1,
  49. ssidType: 2,
  50. passwordType: 3,
  51. meshIdType: 3,
  52. deviceNo: 0,
  53. uuid: "",
  54. serviceId: "",
  55. meshId: "",
  56. processList: [],
  57. result: [],
  58. },
  59. onClose() {
  60. wx.offBluetoothDeviceFound();
  61. this.setData({ showBluetoothList: false, showBluetoothConnected: false });
  62. },
  63. closeEndDialog() {
  64. this.onClose()
  65. wx.navigateBack()
  66. },
  67. checkBluetoothSelectIndex(e) {
  68. const index = e.currentTarget.dataset.index;
  69. this.setData({ bluetoothSelectIndex: index });
  70. },
  71. commitData() {
  72. console.log(this.data.placeOfUse);
  73. console.log(this.data.usingHuman);
  74. console.log(this.data.sn);
  75. console.log(this.data.ssid);
  76. console.log(this.data.password);
  77. this.checkSn(this.data.sn);
  78. },
  79. commitUserData() {
  80. const that = this;
  81. if (that.data.isEdit) {
  82. wx.request({
  83. url: 'https://aipush.aidsleep.cn/customeredit',
  84. method: 'POST',
  85. data: {
  86. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  87. pid: this.data.pid,
  88. sntram: this.data.sn,
  89. per: this.data.usingHuman,
  90. loc: this.data.placeOfUse,
  91. },
  92. success: (res) => {
  93. var codes = res.data.codes;
  94. if (codes && codes == 'success') {
  95. wx.showToast({
  96. icon: 'none',
  97. title: "编辑成功"
  98. });
  99. wx.setStorageSync("successId", this.data.pid);
  100. setTimeout(() => {
  101. wx.navigateBack();
  102. }, 800);
  103. } else {
  104. wx.showToast({
  105. icon: 'none',
  106. title: "编辑失败"
  107. });
  108. }
  109. },
  110. fail: () => {
  111. wx.showToast({
  112. icon: 'none',
  113. title: "编辑失败"
  114. });
  115. }
  116. });
  117. return
  118. }
  119. wx.request({
  120. url: 'https://aipush.aidsleep.cn/wxsaveunionsn',
  121. method: 'POST',
  122. data: {
  123. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  124. unionid: wx.getStorageSync("unionid"),
  125. sntram: this.data.sn,
  126. per: this.data.usingHuman,
  127. loc: this.data.placeOfUse,
  128. },
  129. success: (res) => {
  130. var codes = res.data.codes;
  131. var backPid = res.data.back.pid || "";
  132. if (codes && codes == 'success') {
  133. wx.showToast({
  134. icon: 'none',
  135. title: "添加成功"
  136. });
  137. wx.setStorageSync("successId", backPid);
  138. setTimeout(() => {
  139. wx.navigateBack();
  140. }, 800);
  141. } else {
  142. wx.showToast({
  143. icon: 'none',
  144. title: "添加失败"
  145. });
  146. }
  147. },
  148. fail: () => {
  149. wx.showToast({
  150. icon: 'none',
  151. title: "添加失败"
  152. });
  153. }
  154. });
  155. },
  156. // 开启蓝牙
  157. bindViewBlue: function () {
  158. const that = this;
  159. wx.offBluetoothDeviceFound();
  160. wx.closeBluetoothAdapter();
  161. wx.openBluetoothAdapter({
  162. success(res) {
  163. //console.log(res);
  164. that.getBluDevice()
  165. },
  166. fail(res) {
  167. console.log(res);
  168. wx.showToast({
  169. icon: 'none',
  170. title: `${JSON.stringify(res)}`
  171. });
  172. }
  173. })
  174. },
  175. // 获取蓝牙设备列表
  176. getBluDevice() {
  177. var self = this;
  178. //第一步检查蓝牙适配器是否可用
  179. wx.onBluetoothAdapterStateChange(function (res) {
  180. if (!res.available) {
  181. util.showToast('蓝牙不可用');
  182. return false;
  183. }
  184. });
  185. var oldDeviceList = wx.getStorageSync('deviceList');
  186. if (oldDeviceList && oldDeviceList.length) {
  187. self.setData({
  188. deviceList: oldDeviceList,
  189. showBluetoothList: true
  190. });
  191. }
  192. //第二步关闭适配器,重新来搜索
  193. wx.openBluetoothAdapter({
  194. success: function (res) {
  195. wx.getBluetoothAdapterState({
  196. success: function (res) {
  197. wx.stopBluetoothDevicesDiscovery({
  198. success: function (res) {
  199. wx.startBluetoothDevicesDiscovery({
  200. services: ['0000FFF0-0000-1000-8000-00805F9B34FB'],
  201. success: function (res) {
  202. wx.onBluetoothDeviceFound(function (res) {
  203. console.log(res);
  204. var list = util.filterDevice(res.devices, "name");
  205. if (list.length > 0) {
  206. console.log('==========onBluetoothDeviceFound===========');
  207. console.log(list);
  208. var deviceList = self.data.deviceList;
  209. var doDevices = wx.getStorageSync('doDevices');
  210. if (doDevices) {
  211. } else {
  212. doDevices = [];
  213. }
  214. console.log(deviceList);
  215. console.log(doDevices);
  216. wx.setStorageSync('doDevices', doDevices);
  217. wx.hideLoading();
  218. list.forEach(function (item) {
  219. var flag = true;
  220. for (var i = 0; i < deviceList.length; i++) {
  221. var itemSub = deviceList[i];
  222. if (itemSub.deviceId === item.deviceId) {
  223. flag = false;
  224. break;
  225. }
  226. }
  227. if (flag) {
  228. var arrD = item['name'].split(' ');
  229. item['sn'] = arrD[1];
  230. // wx.request({
  231. // url: 'https://aipush.aidsleep.cn/checkbeds',
  232. // method: 'POST',
  233. // header: {
  234. // 'content-type': 'application/json' // 设置请求的 header
  235. // },
  236. // data: {
  237. // "snlists": arrD[1], // 需要发送的数据
  238. // "token": "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3"
  239. // },
  240. // success: function (res) {
  241. // //console.log(res);
  242. // if (res.data.data[arrD[1]].trim() == 'yes') {
  243. // item['beOnline'] = 'icon-deng-ok';
  244. // } else {
  245. // item['beOnline'] = '';
  246. // // if (doDevices.indexOf(item['deviceId']) >= 0) {
  247. // // item['beOnline'] = 'icon-deng-ok';
  248. // // }
  249. // }
  250. deviceList.push(item);
  251. // },
  252. // fail: function (error) {
  253. // console.error(error);
  254. // }
  255. // });
  256. }
  257. })
  258. self.setData({
  259. deviceList: deviceList,
  260. showBluetoothList: true
  261. });
  262. wx.setStorageSync('deviceList', deviceList);
  263. }
  264. })
  265. }
  266. })
  267. },
  268. fail: function (res) {
  269. console.log(res);
  270. }
  271. });
  272. },
  273. fail: function (res) {
  274. console.log(res);
  275. }
  276. });
  277. },
  278. fail: function (res) {
  279. console.log(res);
  280. }
  281. });
  282. },
  283. commitBluetooth() {
  284. this.onClose();
  285. if (this.data.bluetoothSelectIndex < 0) {
  286. util.showToast("请选择一个床垫");
  287. wx.offBluetoothDeviceFound()
  288. return false;
  289. }
  290. var deviceId = this.data.deviceList[this.data.bluetoothSelectIndex].deviceId;
  291. var name = this.data.deviceList[this.data.bluetoothSelectIndex].name;
  292. var nameList = this.data.deviceList[this.data.bluetoothSelectIndex].name.split(' ');
  293. this.setData({
  294. deviceId: deviceId,
  295. sn: nameList[1]
  296. })
  297. wx.offBluetoothDeviceFound()
  298. },
  299. // 开始配网
  300. bindViewConnect() {
  301. const that = this;
  302. if (that.data.bluetoothSelectIndex < 0) {
  303. util.showToast("没有指定要配网的设备");
  304. return false;
  305. }
  306. var deviceIds = this.data.deviceList[this.data.bluetoothSelectIndex];
  307. if (this.data.deviceList.length == 0 || deviceIds.length === 0) {
  308. util.showToast("没有指定要配网的设备");
  309. return false;
  310. }
  311. that.gotoProvision()
  312. },
  313. // 获取Wifi
  314. getWifiInfo: function () {
  315. wx.startWifi({
  316. success(res) {
  317. //console.log(' ================== startWifi ================== ');
  318. //console.log(res);
  319. wx.getConnectedWifi({
  320. success(res) {
  321. },
  322. fail: function (err) {
  323. console.log(' ================== startWifi fail ================== ');
  324. console.log(err);
  325. if (app.data.sytem == 'ios') {
  326. util.showToast("获取Wi-Fi信息失败,请更新微信到最新版");
  327. } else {
  328. util.showToast("获取Wi-Fi信息失败");
  329. }
  330. }
  331. });
  332. }
  333. });
  334. const self = this;
  335. wx.startWifi({
  336. success(res) {
  337. //console.log(res)
  338. // 先取一次,防止IOS获取不到
  339. wx.getConnectedWifi({
  340. success(res) {
  341. self.setData({
  342. wifiInfo: res.wifi,
  343. ssid: res.wifi.SSID
  344. });
  345. //console.log('== getConnectedWifi ==');
  346. //console.log(res);
  347. if (res.frequency >= 2412 && res.frequency <= 2472) { } else {
  348. self.setData({
  349. notice: "温馨提示:当前您连接的Wi-Fi网络是5G频段,建议连接2.4G频段配网"
  350. })
  351. }
  352. },
  353. fail: function (err) {
  354. util.showToast("获取Wi-Fi信息失败");
  355. }
  356. })
  357. wx.onWifiConnected(function (res) {
  358. // console.log('== onWifiConnected ==');
  359. // console.log(res);
  360. self.setData({
  361. wifiInfo: res.wifi
  362. })
  363. })
  364. },
  365. fail(res) {
  366. console.log('WIFI初始化失败', res)
  367. util.showToast("WIFI初始化失败");
  368. }
  369. })
  370. },
  371. gotoProvision: function () {
  372. const wifiInfo = this.data.wifiInfo;
  373. const ssid = wifiInfo != null ? wifiInfo.SSID : null;
  374. const bssid = wifiInfo != null ? wifiInfo.BSSID : null;
  375. const password = this.data.password;
  376. if (wifiInfo == null) {
  377. util.showToast("没有Wi-Fi连接");
  378. return;
  379. } else {
  380. if (ssid == null || ssid == '' || password == '') {
  381. util.showToast("WiFi登录名和密码不能为空");
  382. return;
  383. } else {
  384. const info = {
  385. ssid: ssid,
  386. bssid: bssid,
  387. password: password,
  388. deviceIds: this.data.deviceId
  389. }
  390. this.blueConnect();
  391. }
  392. }
  393. },
  394. // 蓝牙链接
  395. blueConnect: function (event) {
  396. //console.log(' =============== blueConnect start ===============')
  397. var self = this;
  398. sequenceControl = 0;
  399. self.setData({
  400. flagEnd: false,
  401. serviceId: "",
  402. uuid: "",
  403. showBluetoothConnected: true
  404. });
  405. self.setProcess(0, util.descSucList[0], 1);
  406. wx.offBluetoothAdapterStateChange();
  407. //第一步检查蓝牙适配器是否可用
  408. wx.onBluetoothAdapterStateChange(function (res) {
  409. if (!res.available) {
  410. util.showToast('蓝牙不可用');
  411. return false;
  412. }
  413. });
  414. //console.log(app.data);
  415. if (app.data.sytem == 'ios') {
  416. }
  417. self.onTimeout(0);
  418. //console.log('deviceId = ' + self.data.deviceId)
  419. wx.createBLEConnection({
  420. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  421. deviceId: self.data.deviceId,
  422. timeout: 10000,
  423. success: function (res) {
  424. wx.setBLEMTU({
  425. deviceId: self.data.deviceId,
  426. mtu: 128
  427. })
  428. //console.log("============ createBLEConnection success =============")
  429. //console.log(self.data.deviceId)
  430. self.getDeviceServices(self.data.deviceId);
  431. },
  432. fail: function (res) {
  433. console.log("============ createBLEConnection fail =============")
  434. console.log(res)
  435. console.log(self.data.blueConnectNum)
  436. var num = self.data.blueConnectNum;
  437. if (num < 3) {
  438. self.blueConnect();
  439. num++;
  440. self.setData({
  441. blueConnectNum: num
  442. })
  443. } else {
  444. self.setFailProcess(true, res.errCode + "::" + util.descFailList[0]);
  445. }
  446. }
  447. })
  448. },
  449. getDeviceServices: function (deviceId) {
  450. var self = this;
  451. wx.getBLEDeviceServices({
  452. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  453. deviceId: deviceId,
  454. success: function (res) {
  455. self.setProcess(10, util.descSucList[1], 2);
  456. var services = res.services;
  457. if (services.length > 0) {
  458. for (var i = 0; i < services.length; i++) {
  459. var uuid = services[i].uuid;
  460. if (uuid == app.data.service_uuid) {
  461. self.getDeviceCharacteristics(deviceId, uuid);
  462. return false;
  463. }
  464. }
  465. }
  466. },
  467. fail: function (res) {
  468. self.setFailProcess(true, res.errCode + "::" + util.descFailList[1]);
  469. }
  470. })
  471. },
  472. getDeviceCharacteristics: function (deviceId, serviceId) {
  473. //console.log(" ============== getDeviceCharacteristics =============== ");
  474. var self = this;
  475. wx.getBLEDeviceCharacteristics({
  476. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  477. deviceId: deviceId,
  478. serviceId: serviceId,
  479. success: function (res) {
  480. self.setProcess(40, util.descSucList[2], 3);
  481. var list = res.characteristics;
  482. if (list.length > 0) {
  483. for (var i = 0; i < list.length; i++) {
  484. var uuid = list[i].uuid;
  485. if (uuid == app.data.characteristic_write_uuid) {
  486. self.openNotify(deviceId, serviceId, uuid);
  487. self.setData({
  488. serviceId: serviceId,
  489. uuid: uuid,
  490. })
  491. return false;
  492. }
  493. }
  494. }
  495. },
  496. fail: function (res) {
  497. console.log(res);
  498. self.setFailProcess(true, res.errCode + "::" + util.descFailList[2]);
  499. }
  500. })
  501. },
  502. //连接超时
  503. onTimeout: function (num) {
  504. const self = this;
  505. timeId = setInterval(function () {
  506. if (num < timeOut) {
  507. num++;
  508. } else {
  509. clearInterval(timeId);
  510. if (self.data.showBluetoothConnected) {
  511. wx.showModal({
  512. title: '提示',
  513. content: '配网失败,请检查wifi密码或设备是否连接正常',
  514. showCancel: false
  515. });
  516. self.setData({
  517. showBluetoothConnected: false
  518. });
  519. }
  520. // self.setFailProcess(true, util.descFailList[4]);
  521. }
  522. }, 1000)
  523. },
  524. //启用通知
  525. openNotify: function (deviceId, serviceId, characteristicId) {
  526. //console.log("=========== openNotify ============");
  527. var self = this;
  528. wx.notifyBLECharacteristicValueChange({
  529. state: true, // 启用 notify 功能
  530. deviceId: deviceId,
  531. serviceId: serviceId,
  532. characteristicId: app.data.characteristic_read_uuid,
  533. success: function (res) {
  534. //console.log(res);
  535. self.writeDevice(deviceId, serviceId, characteristicId, null);
  536. self.onNotify();
  537. },
  538. fail: function (res) {
  539. console.log(res);
  540. }
  541. })
  542. },
  543. //监听通知
  544. onNotify: function () {
  545. //console.log("=========== onNotify ============");
  546. var self = this;
  547. wx.onBLECharacteristicValueChange(function (res) {
  548. //console.log(res);
  549. self.getResultType(util.ab2hex(res.value));
  550. })
  551. },
  552. getCharCodeat: function (str) {
  553. let utf8Bytes = [];
  554. for (let i = 0; i < str.length; i++) {
  555. let char = str.charCodeAt(i);
  556. if (char < 0x80) {
  557. utf8Bytes.push(char);
  558. } else if (char < 0x800) {
  559. utf8Bytes.push(0xc0 | (char >> 6));
  560. utf8Bytes.push(0x80 | (char & 0x3f));
  561. } else if (char < 0x10000) {
  562. utf8Bytes.push(0xe0 | (char >> 12));
  563. utf8Bytes.push(0x80 | ((char >> 6) & 0x3f));
  564. utf8Bytes.push(0x80 | (char & 0x3f));
  565. }
  566. }
  567. return utf8Bytes;
  568. // var list = [];
  569. // for (var i = 0; i < str.length; i++) {
  570. //   list.push(str.charCodeAt(i));
  571. // }
  572. // return list;
  573. },
  574. // 设备数据开始写入
  575. writeDevice: function (deviceId, serviceId, characteristicId, data) {
  576. //console.log("===== writeDevice =====");
  577. var self = this,
  578. obj = {};
  579. self.setProcess(60, util.descSucList[4], 3);
  580. sequenceControl = parseInt(sequenceControl) + 1;
  581. if (!util._isEmpty(data)) {
  582. obj = util.isSubcontractor(data, true, sequenceControl);
  583. } else {
  584. data = util.writeData(self.getCharCodeat('"' + self.data.ssid + '","' + self.data.password + '"'));
  585. obj = util.isSubcontractor(data, self.data.isChecksum, sequenceControl);
  586. }
  587. var typedArray = new Uint8Array(obj.lenData)
  588. wx.writeBLECharacteristicValue({
  589. deviceId: deviceId,
  590. serviceId: serviceId,
  591. characteristicId: characteristicId,
  592. value: typedArray.buffer,
  593. success: function (res) {
  594. //console.log(res);
  595. if (obj.flag) {
  596. self.writeDevice(deviceId, serviceId, characteristicId, obj.laveData);
  597. } else {
  598. self.setProcess(80, util.descSucList[5], 4);
  599. }
  600. },
  601. fail: function (res) {
  602. console.log(res);
  603. self.setFailProcess(true, res.errCode + "::" + util.descFailList[3]);
  604. }
  605. })
  606. },
  607. getResultType: function (list) {
  608. console.log(' ========= getResultType ========= ');
  609. var self = this;
  610. console.log(list);
  611. var deviceList = wx.getStorageSync('deviceList');
  612. console.log(deviceList);
  613. if (list.length < 4) {
  614. self.setFailProcess(true, util.descFailList[4]);
  615. return false;
  616. }
  617. var val = list[4];
  618. if (val == "00") {
  619. var idx = self.data.deviceNo + 1;
  620. var doDevices = wx.getStorageSync('doDevices');
  621. if (doDevices) {
  622. if (doDevices.indexOf(self.data.deviceId) < 0) {
  623. doDevices.push(self.data.deviceId);
  624. }
  625. } else {
  626. doDevices = [self.data.deviceId];
  627. }
  628. wx.setStorageSync('doDevices', doDevices);
  629. var deviceSn = self.data.deviceId;
  630. for (var i = 0; i < deviceList.length; i++) {
  631. var itemSub = deviceList[i];
  632. if (itemSub.deviceId === self.data.deviceId) {
  633. deviceSn = itemSub.sn;
  634. break;
  635. }
  636. }
  637. if (idx < self.data.deviceIds.length) {
  638. self.setProcess(90, 'SN:' + deviceSn + ' 连接成功', 5);
  639. wx.closeBLEConnection({
  640. deviceId: self.data.deviceId,
  641. success: function (res) { }
  642. })
  643. self.setData({
  644. deviceNo: idx,
  645. deviceId: self.data.deviceIds[idx],
  646. blueConnectNum: 0
  647. });
  648. self.blueConnect();
  649. } else {
  650. self.setProcess(90, 'SN:' + deviceSn + ' 连接成功', 5);
  651. self.setProcess(100, util.descSucList[6], 7);
  652. self.setData({
  653. result: [],
  654. })
  655. }
  656. } else {
  657. self.setFailProcess(true, util.descFailList[4])
  658. }
  659. },
  660. setProcess: function (value, desc, step = 0) {
  661. var self = this,
  662. list = [];
  663. list = self.data.processList;
  664. if (list.indexOf(desc) == -1) {
  665. list.push(desc);
  666. }
  667. if (self.data.value <= value) {
  668. self.setData({
  669. value: value,
  670. bluetoothConnectedStep: step
  671. });
  672. }
  673. self.setData({
  674. processList: list
  675. });
  676. //console.log(value);
  677. //console.log(desc);
  678. if (value == 100) {
  679. self.closeConnect();
  680. wx.closeBluetoothAdapter();
  681. self.setData({
  682. isBluetoothConnected: true,
  683. desc: util.descSucList[6]
  684. });
  685. clearInterval(timeId);
  686. sequenceControl = 0;
  687. self.commitUserData();
  688. }
  689. },
  690. setFailProcess: function (flag, desc) {
  691. var self = this,
  692. list = [];
  693. list = self.data.processList;
  694. list.push(desc);
  695. self.setData({
  696. failure: flag,
  697. processList: list
  698. });
  699. },
  700. closeConnect: function () {
  701. var self = this;
  702. wx.closeBLEConnection({
  703. deviceId: self.data.deviceId,
  704. success: function (res) { }
  705. })
  706. },
  707. /**
  708. * 生命周期函数--监听页面加载
  709. */
  710. onLoad(options) {
  711. wx.authorize({
  712. scope: 'scope.bluetooth',
  713. success() {
  714. // 用户同意授权后再次获取位置
  715. },
  716. fail() {
  717. // 用户拒绝授权
  718. }
  719. });
  720. this.getWifiInfo();
  721. if (options.type && options.type == "edit") {
  722. wx.setNavigationBarTitle({
  723. title: "编辑床垫"
  724. })
  725. const item = wx.getStorageSync("item");
  726. if (item) {
  727. this.setData({
  728. isEdit: true,
  729. pid: item.pid,
  730. placeOfUse: item.location,
  731. usingHuman: item.person,
  732. deviceId: item.sn,
  733. sn: item.sn,
  734. ssid: "",
  735. password: "",
  736. })
  737. wx.removeStorageSync("item")
  738. }
  739. }
  740. },
  741. // 实验室
  742. // 测试
  743. // 332016850591
  744. // sleep
  745. // sleep1234
  746. checkSn(sntram) {
  747. const that = this;
  748. wx.request({
  749. url: 'https://aipush.aidsleep.cn/wxchecksn',
  750. method: 'POST',
  751. data: {
  752. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  753. sntram: sntram,
  754. },
  755. success: (res) => {
  756. var incustom = res.data.incustom;
  757. if (incustom == "true") {
  758. that.bindViewConnect();
  759. } else {
  760. wx.showToast({
  761. icon: 'none',
  762. title: "sn 不合法"
  763. });
  764. }
  765. },
  766. fail: () => {
  767. wx.showToast({
  768. icon: 'none',
  769. title: "sn 不合法"
  770. });
  771. }
  772. });
  773. },
  774. /**
  775. * 生命周期函数--监听页面初次渲染完成
  776. */
  777. onReady() {
  778. },
  779. /**
  780. * 生命周期函数--监听页面显示
  781. */
  782. onShow() {
  783. },
  784. /**
  785. * 生命周期函数--监听页面隐藏
  786. */
  787. onHide() {
  788. },
  789. /**
  790. * 生命周期函数--监听页面卸载
  791. */
  792. onUnload() {
  793. wx.offWifiConnected({});
  794. wx.stopWifi({})
  795. wx.closeBluetoothAdapter();
  796. wx.offBluetoothDeviceFound();
  797. wx.removeStorageSync('doDevices')
  798. wx.removeStorageSync('deviceList')
  799. clearInterval(timeId);
  800. this.closeConnect()
  801. },
  802. /**
  803. * 页面相关事件处理函数--监听用户下拉动作
  804. */
  805. onPullDownRefresh() {
  806. },
  807. /**
  808. * 页面上拉触底事件的处理函数
  809. */
  810. onReachBottom() {
  811. },
  812. /**
  813. * 用户点击右上角分享
  814. */
  815. onShareAppMessage() {
  816. }
  817. })