addMattresses.js 23 KB

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