deviceTest.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // pages/operation/deviceTest.js
  2. // deviceId:'679F9ABF-3C57-1C70-38D8-030C8DBEAB5B'
  3. const util = require("../../utils/util")
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. deviceId: '',
  10. deviceState: 0,
  11. serviceId: '',
  12. characteristicId: '',
  13. isloading: false,
  14. scanCode: '',
  15. instructType: 1, // 1发送密码指定;2发送出货指令;3发送出货结果确认;
  16. instructReceive: '',
  17. instructSend: '',
  18. gridNum: 20
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {},
  24. scanCodeEvent() {
  25. var that = this;
  26. wx.scanCode({
  27. onlyFromCamera: true, // 只允许从相机扫码
  28. success(res) {
  29. console.log("扫码成功:" + JSON.stringify(res))
  30. that.setData({
  31. scanCode: res.result,
  32. isloading: true
  33. })
  34. that.loadblue()
  35. }
  36. })
  37. },
  38. inputConfirm(e){
  39. var that = this
  40. console.log(e)
  41. if(e.target.dataset.type){
  42. if(!e.target.dataset.val){
  43. wx.showToast({
  44. title: '请输入设备号',
  45. duration: 3000
  46. })
  47. return
  48. }
  49. that.setData({
  50. scanCode: e.target.dataset.val,
  51. isloading: true
  52. })
  53. }else{
  54. if(!e.detail.value){
  55. wx.showToast({
  56. title: '请输入设备号',
  57. duration: 3000
  58. })
  59. return
  60. }
  61. that.setData({
  62. scanCode: e.detail.value,
  63. isloading: true
  64. })
  65. }
  66. that.loadblue()
  67. },
  68. loadblue(){
  69. var that = this
  70. wx.openBluetoothAdapter({
  71. success: function (res) {
  72. console.log('初始化蓝牙适配器成功' + JSON.stringify(res))
  73. wx.startBluetoothDevicesDiscovery({
  74. services: [],
  75. success: function (res) {
  76. console.log('搜索设备')
  77. console.log(res)
  78. //5s内未搜索到设备,关闭搜索,关闭蓝牙模块
  79. setTimeout(function () {
  80. wx.getBluetoothDevices({
  81. success: function (res) {
  82. let i = 0;
  83. while (res.devices[i]) {
  84. console.log(i);
  85. console.log(res.devices[i].name, res.devices[i].deviceId);
  86. // if (res.devices[i].deviceId == that.data.scanCode) {
  87. // that.setData({
  88. // deviceId: res.devices[i].deviceId
  89. // })
  90. // }
  91. // scanCode LY808
  92. console.log("scanCode:" + that.data.scanCode)
  93. if (that.data.scanCode != "" && res.devices[i].name.indexOf(that.data.scanCode) > -1) {
  94. that.setData({
  95. deviceId: res.devices[i].deviceId
  96. })
  97. }
  98. i++;
  99. }
  100. if (!that.data.deviceId) {
  101. that.setData({
  102. isloading: false
  103. });
  104. wx.showToast({
  105. title: '搜索设备超时',
  106. duration: 3000
  107. })
  108. console.log("搜索设备超时");
  109. wx.stopBluetoothDevicesDiscovery({}); //关闭搜索
  110. wx.closeBluetoothAdapter({}); //关闭蓝牙模块
  111. } else {
  112. wx.stopBluetoothDevicesDiscovery({}); //关闭搜索
  113. that.connectBLEDevice(); //去连接蓝牙设备
  114. }
  115. }
  116. })
  117. }, 10000);
  118. },
  119. })
  120. },
  121. fail: function () {
  122. that.setData({
  123. isloading: false
  124. });
  125. util.toast('请打开蓝牙!')
  126. },
  127. complete: function () {
  128. console.log('初始化蓝牙适配器完成')
  129. }
  130. })
  131. },
  132. closediscovery: function () {
  133. var that = this
  134. wx.closeBLEConnection({
  135. deviceId: that.data.deviceId,
  136. success: (res) => {
  137. that.setData({
  138. deviceId: '',
  139. deviceState: -1
  140. })
  141. console.log(res)
  142. }
  143. })
  144. },
  145. connectBLEDevice() {
  146. var that = this
  147. wx.createBLEConnection({
  148. deviceId: that.data.deviceId,
  149. success: function (res) {
  150. console.log(res);
  151. wx.getBLEDeviceServices({
  152. deviceId: that.data.deviceId,
  153. success: function (res) {
  154. console.log(res.services);
  155. let i = 0;
  156. while (res.services[i]) {
  157. if (res.services[i].uuid.indexOf('FFE0') > -1) {
  158. that.setData({
  159. serviceId: res.services[i].uuid
  160. })
  161. }
  162. i++;
  163. }
  164. if (that.data.deviceId.length > 0) {
  165. wx.getBLEDeviceCharacteristics({
  166. deviceId: that.data.deviceId,
  167. serviceId: that.data.serviceId,
  168. success: function (res) {
  169. let i = 0;
  170. while (res.characteristics[i]) {
  171. if (res.characteristics[i].uuid.indexOf('FFE1') > -1) {
  172. that.setData({
  173. characteristicId: res.characteristics[i].uuid
  174. })
  175. }
  176. i++;
  177. }
  178. if (that.data.characteristicId.length > 0) {
  179. that.startnotify()
  180. setTimeout(function () {
  181. let pwd = that.strToHexCharCode('888888')
  182. that.setData({
  183. instructType: 1,
  184. instructSend: "FF0401" + pwd + "EE",
  185. instructReceive: "ee040101ff"
  186. })
  187. that.startwrite(that.data.instructSend)
  188. }, 2000)
  189. }
  190. },
  191. fail: function (res) {
  192. that.setData({
  193. deviceState: -1,
  194. isloading: false
  195. })
  196. }
  197. })
  198. }
  199. },
  200. fail: function (res) {
  201. that.setData({
  202. deviceState: -1,
  203. isloading: false
  204. })
  205. }
  206. })
  207. },
  208. fail: function (res) {
  209. that.setData({
  210. deviceState: -1,
  211. isloading: false
  212. })
  213. }
  214. })
  215. },
  216. startnotify: function () {
  217. var that = this
  218. wx.notifyBLECharacteristicValueChange({
  219. state: true,
  220. deviceId: that.data.deviceId,
  221. serviceId: that.data.serviceId,
  222. characteristicId: that.data.characteristicId,
  223. success: function (res) {
  224. console.log('notifyBLECharacteristicValueChange success', res.errMsg)
  225. },
  226. fail: function (res) {
  227. util.toast(res.errMsg)
  228. }
  229. })
  230. function ab2hex(buffer) {
  231. var hexArr = Array.prototype.map.call(
  232. new Uint8Array(buffer),
  233. function (bit) {
  234. return ('00' + bit.toString(16)).slice(-2)
  235. }
  236. )
  237. return hexArr.join('');
  238. }
  239. wx.onBLECharacteristicValueChange(function (res) {
  240. console.log('characteristic value :', res.value)
  241. console.log('characteristic value comed:', ab2hex(res.value))
  242. var resStr = ab2hex(res.value)
  243. switch (that.data.instructType) {
  244. case 1:
  245. if (resStr == that.data.instructReceive) {
  246. that.setData({
  247. deviceState: 1,
  248. isloading: false
  249. })
  250. }
  251. break;
  252. case 2:
  253. // var total = resStr.length
  254. var num = parseInt(resStr.substr(22, 2))
  255. if (num == 1) {
  256. var resInstruct = resStr.substr(24, 2)
  257. if (resInstruct == "00") {
  258. util.toast('当前格子已打开!');
  259. // 回复蓝牙模块确认
  260. let instruct = resStr.substr(6, 16)
  261. instruct = "FF0202" + instruct + "EE"
  262. that.startwrite(instruct)
  263. } else if (resInstruct == "01") {
  264. util.toast('当前格子关闭!');
  265. }
  266. }
  267. if (resStr == that.data.instructReceive) {
  268. let instruct = "FF0202523F33F3D64455FFEE";
  269. that.setData({
  270. instructType: 3,
  271. instructSend: instruct,
  272. instructReceive: ''
  273. })
  274. that.startwrite(that.data.instructSend)
  275. }
  276. break;
  277. default:
  278. break;
  279. }
  280. })
  281. },
  282. startwrite: function (value_initial) {
  283. var that = this
  284. if (!value_initial) {
  285. value_initial = "FF 04 01 38 38 38 38 38 38 EE";
  286. }
  287. var write_array = []; //用于发送数据的数组。
  288. /* 判断是否存在空格 */
  289. if (value_initial.indexOf(' ') > 0) {
  290. var value_initial = that.splitStr(value_initial, ' '); //存在空格时
  291. console.log('删除掉空格', value_initial);
  292. } else {
  293. var value_initial = value_initial; //不存在空格时
  294. }
  295. /* 判断字节是否超过20字节 */
  296. // if (value_initial.length > 20) {
  297. // //当字节超过20的时候,采用分段发送
  298. // //选择16进制发送时
  299. // var value_initial_exceed = value_initial;
  300. // //将输入框的值取过来,方便循环
  301. // var value_initial_average = Math.ceil(value_initial_exceed.length / 20); //将value_initial_exceed的长度除以20,余数再向上取一,确定循环几次
  302. // console.log('需要循环的次数', value_initial_average);
  303. // for (var i = 0; i < value_initial_average; i++) {
  304. // if (value_initial_exceed.length > 20) {
  305. // var value_initial_send = value_initial_exceed.slice(0, 20); //截取前20个字节
  306. // console.log('截取到的值', value_initial_send);
  307. // value_initial_exceed = value_initial_exceed.substring(20); //value_initial_exceed替换为取掉前20字节后的数据
  308. // write_array.push(value_initial_send); //将所有截取的值放在一个数组
  309. // } else {
  310. // write_array.push(value_initial_exceed);
  311. // }
  312. // }
  313. // console.log('write_array数组', write_array);
  314. // write_array.map(function (val, index) {
  315. // setTimeout(function () {
  316. // var value_set = val;
  317. // console.log('value_set', value_set);
  318. // var write_function = that.write(value_set); //调用数据发送函数
  319. // }, index * 100)
  320. // });
  321. // } else {
  322. //当字节不超过20的时候,直接发送
  323. var value = value_initial;
  324. var write_function = that.write(value); //调用数据发送函数
  325. // }
  326. },
  327. //写入数据
  328. write: function (str) {
  329. var that = this;
  330. var value = str;
  331. console.log('value', value);
  332. /* 将数值转为ArrayBuffer类型数据 */
  333. var typedArray = new Uint8Array(value.match(/[\da-f]{2}/gi).map(function (h) {
  334. return parseInt(h, 16)
  335. }));
  336. var buffer = typedArray.buffer;
  337. wx.writeBLECharacteristicValue({
  338. deviceId: that.data.deviceId,
  339. serviceId: that.data.serviceId,
  340. characteristicId: that.data.characteristicId,
  341. value: buffer,
  342. success: function (res) {
  343. console.log('写入成功', res);
  344. },
  345. fail: function (res) {
  346. console.log('调用失败', res);
  347. /* 调用失败时,再次调用 */
  348. wx.writeBLECharacteristicValue({
  349. deviceId: that.data.deviceId,
  350. serviceId: that.data.serviceId,
  351. characteristicId: that.data.characteristicId,
  352. value: buffer,
  353. success: function (res) {
  354. console.log('写入成功', res);
  355. },
  356. fail: function (res) {
  357. console.log('第2次调用失败', res);
  358. /* 调用失败时,再次调用 */
  359. wx.writeBLECharacteristicValue({
  360. deviceId: that.data.deviceId,
  361. serviceId: that.data.serviceId,
  362. characteristicId: that.data.characteristicId,
  363. value: buffer,
  364. success: function (res) {
  365. console.log('写入成功', res);
  366. },
  367. fail: function (res) {
  368. console.log('第3次调用失败', res);
  369. }
  370. });
  371. }
  372. });
  373. }
  374. });
  375. },
  376. /* 去除输入框输入的值中的空格 */
  377. splitStr: function (str, s) {
  378. var newStr = "";
  379. var strArray = str.split(s);
  380. for (var i = 0; i < strArray.length; i++) {
  381. newStr += strArray[i];
  382. }
  383. return newStr;
  384. },
  385. openGrate(e) {
  386. var that = this
  387. if (that.data.deviceState == 0) {
  388. util.toast("未连接设备!")
  389. return
  390. }
  391. let goodsRoad = ''
  392. switch (e.currentTarget.dataset.index + 1) {
  393. case 1:
  394. goodsRoad = '01'
  395. break;
  396. case 2:
  397. goodsRoad = '02'
  398. break;
  399. case 3:
  400. goodsRoad = '03'
  401. break;
  402. case 4:
  403. goodsRoad = '04'
  404. break;
  405. case 5:
  406. goodsRoad = '05'
  407. break;
  408. case 6:
  409. goodsRoad = '06'
  410. break;
  411. case 7:
  412. goodsRoad = '07'
  413. break;
  414. case 8:
  415. goodsRoad = '08'
  416. break;
  417. case 9:
  418. goodsRoad = '09'
  419. break;
  420. case 10:
  421. goodsRoad = '0A'
  422. break;
  423. case 11:
  424. goodsRoad = '0B'
  425. break;
  426. break;
  427. case 12:
  428. goodsRoad = '0C'
  429. break;
  430. case 13:
  431. goodsRoad = '0D'
  432. break;
  433. case 14:
  434. goodsRoad = '0E'
  435. break;
  436. case 15:
  437. goodsRoad = '0F'
  438. break;
  439. case 16:
  440. goodsRoad = '10'
  441. break;
  442. case 17:
  443. goodsRoad = '11'
  444. break;
  445. case 18:
  446. goodsRoad = '12'
  447. break;
  448. case 19:
  449. goodsRoad = '13'
  450. break;
  451. case 20:
  452. goodsRoad = '14'
  453. break;
  454. case 21:
  455. goodsRoad = '15'
  456. break;
  457. case 22:
  458. goodsRoad = '16'
  459. break;
  460. case 23:
  461. goodsRoad = '17'
  462. break;
  463. case 24:
  464. goodsRoad = '18'
  465. break;
  466. default:
  467. goodsRoad = '01'
  468. break;
  469. }
  470. var clientid = util.generateClientID()
  471. let strclientid = that.strToHexCharCode(clientid)
  472. let instruct = "FF 02 01 " + strclientid + " 01 " + goodsRoad + " EE";
  473. strclientid = strclientid.toLowerCase()
  474. that.setData({
  475. instructType: 2,
  476. instructSend: instruct,
  477. instructReceive: 'ee0201' + strclientid.toLowerCase() + '0100ff',
  478. instructConfirm: 'ee0201' + strclientid.toLowerCase() + 'ff',
  479. })
  480. that.startwrite(that.data.instructSend)
  481. },
  482. strToHexCharCode(str) {
  483. if (str === "")
  484. return "";
  485. var hexCharCode = [];
  486. hexCharCode.push("0x");
  487. for (var i = 0; i < str.length; i++) {
  488. hexCharCode.push((str.charCodeAt(i)).toString(16));
  489. }
  490. return hexCharCode.join("").replace('0x', '');
  491. },
  492. oneKeyOpen() {
  493. var that = this
  494. if (that.data.deviceState == 0) {
  495. util.toast("未连接设备!")
  496. return
  497. }
  498. let goodsRoad = ''
  499. let res = ''
  500. for (let index = 1; index <= that.data.gridNum; index++) {
  501. goodsRoad += (Array(2).join(0) + index).slice(-2)
  502. res += '00'
  503. }
  504. var clientid = util.generateClientID()
  505. let strclientid = that.strToHexCharCode(clientid)
  506. let instruct = "FF 02 01 " + strclientid + that.data.gridNum + goodsRoad + " EE";
  507. strclientid = strclientid.toLowerCase()
  508. that.setData({
  509. instructType: 2,
  510. instructSend: instruct,
  511. instructReceive: 'ee0201' + strclientid.toLowerCase() + that.data.gridNum + res + 'ff'
  512. })
  513. that.startwrite(that.data.instructSend)
  514. },
  515. inputedit: function (e) {
  516. let _this = this;
  517. let dataset = e.currentTarget.dataset;
  518. let value = e.detail.value;
  519. let name = dataset.name;
  520. _this.data[name] = value;
  521. _this.setData({
  522. [name]: _this.data[name]
  523. });
  524. },
  525. })