index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. var deviceId;
  5. var i = 0;
  6. var serviceId = [];
  7. var characteristicId = [];
  8. var aleApi = require('../../utils/antsBleApi.js');
  9. Page({
  10. data: {
  11. motto: 'Hello World',
  12. userInfo: {},
  13. hasUserInfo: false,
  14. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  15. sendData: 'FF01020AEE',
  16. writeDatas: ''
  17. },
  18. //事件处理函数
  19. bindViewTap: function () {
  20. wx.navigateTo({
  21. url: '../logs/logs'
  22. })
  23. },
  24. onLoad: function () {
  25. if (app.globalData.userInfo) {
  26. this.setData({
  27. userInfo: app.globalData.userInfo,
  28. hasUserInfo: true
  29. })
  30. } else if (this.data.canIUse) {
  31. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  32. // 所以此处加入 callback 以防止这种情况
  33. app.userInfoReadyCallback = res => {
  34. this.setData({
  35. userInfo: res.userInfo,
  36. hasUserInfo: true
  37. })
  38. }
  39. } else {
  40. // 在没有 open-type=getUserInfo 版本的兼容处理
  41. wx.getUserInfo({
  42. success: res => {
  43. app.globalData.userInfo = res.userInfo
  44. this.setData({
  45. userInfo: res.userInfo,
  46. hasUserInfo: true
  47. })
  48. }
  49. })
  50. }
  51. },
  52. getUserInfo: function (e) {
  53. console.log(e)
  54. app.globalData.userInfo = e.detail.userInfo
  55. this.setData({
  56. userInfo: e.detail.userInfo,
  57. hasUserInfo: true
  58. })
  59. },
  60. inArray(arr, key, val) {
  61. for (let i = 0; i < arr.length; i++) {
  62. if (arr[i][key] === val) {
  63. return i;
  64. }
  65. }
  66. return -1;
  67. },
  68. // ArrayBuffer转16进度字符串示例
  69. ab2hex(buffer) {
  70. var hexArr = Array.prototype.map.call(
  71. new Uint8Array(buffer),
  72. function (bit) {
  73. return ('00' + bit.toString(16)).slice(-2)
  74. }
  75. )
  76. return hexArr.join('');
  77. },
  78. myStringtoHex(str) {
  79. str = str.toLowerCase();
  80. let newBuffer = new ArrayBuffer(str.length / 2);
  81. let hexBuffer = new Uint8Array(newBuffer, 0);
  82. let h = 0,
  83. l = 0;
  84. for (let i = 0; i < str.length / 2; i++) {
  85. h = str.charCodeAt(2 * i);
  86. l = str.charCodeAt(2 * i + 1);
  87. if (48 <= h && h <= 57) {
  88. h = h - 48;
  89. } else {
  90. h = h - 97 + 10;
  91. }
  92. if (48 <= l && l <= 57) {
  93. l = l - 48;
  94. } else {
  95. l = l - 97 + 10;
  96. }
  97. hexBuffer[i] = h * 16 + l;
  98. }
  99. return hexBuffer;
  100. },
  101. // 初始化蓝牙模块
  102. openadapter() {
  103. var that = this
  104. wx.openBluetoothAdapter({
  105. success: function (res) {
  106. console.log('初始化蓝牙适配器成功' + JSON.stringify(res))
  107. that.msg = '初始化蓝牙适配器成功'
  108. wx.showModal({
  109. title: '蓝牙适配情况',
  110. content: '初始化蓝牙适配器成功'
  111. })
  112. },
  113. fail: function () {
  114. that.msg = '初始化蓝牙适配器失败'
  115. wx.showModal({
  116. title: '蓝牙适配情况',
  117. content: '蓝牙适配失败,请检查手机蓝牙和定位功能是否打开'
  118. })
  119. },
  120. complete: function (res) {
  121. console.log(res)
  122. console.log('初始化蓝牙适配器完成')
  123. }
  124. })
  125. },
  126. closeadapter: function () {
  127. wx.closeBluetoothAdapter({
  128. success: function (res) {
  129. console.log(res, "success")
  130. },
  131. fail: function (res) {
  132. console.log(res, "fail")
  133. },
  134. })
  135. // wx.getBluetoothAdapterState({
  136. // complete: function (res) {
  137. // console.log("currentstate:", res)
  138. // }
  139. // })
  140. },
  141. opendiscovery: function () {
  142. wx.startBluetoothDevicesDiscovery({
  143. services: [],
  144. success: function (res) {
  145. console.log(res)
  146. },
  147. fail: function (res) {
  148. console.log(res, "fail")
  149. },
  150. })
  151. },
  152. closediscovery: function () {
  153. wx.stopBluetoothDevicesDiscovery({
  154. success: function (res) {
  155. console.log(res)
  156. },
  157. fail: function (res) {
  158. console.log(res, "fail")
  159. },
  160. })
  161. },
  162. getdevice: function () {
  163. function ab2hex(buffer) {
  164. var hexArr = Array.prototype.map.call(
  165. new Uint8Array(buffer),
  166. function (bit) {
  167. return ('00' + bit.toString(16)).slice(-2)
  168. }
  169. )
  170. return hexArr.join('');
  171. }
  172. wx.getBluetoothDevices({
  173. success: function (res) {
  174. console.log(res)
  175. i = 0;
  176. while (res.devices[i]) {
  177. console.log(i);
  178. console.log(res.devices[i].name, res.devices[i].deviceId);
  179. if (res.devices[i].name == 'LY808') {
  180. deviceId = res.devices[i].deviceId;
  181. console.log(deviceId);
  182. }
  183. i++;
  184. }
  185. }
  186. })
  187. },
  188. getconnecteddevice: function () {
  189. wx.getConnectedBluetoothDevices({
  190. //services:[],
  191. success: function (res) {
  192. console.log(res)
  193. }
  194. })
  195. },
  196. connecteddevice: function () {
  197. wx.createBLEConnection({
  198. deviceId: deviceId,
  199. success: function (res) {
  200. console.log(res);
  201. },
  202. })
  203. },
  204. getservice: function () {
  205. wx.getBLEDeviceServices({
  206. deviceId: deviceId,
  207. success: function (res) {
  208. console.log(res.services);
  209. i = 0;
  210. while (res.services[i]) {
  211. serviceId[i] = res.services[i].uuid;
  212. console.log(serviceId[i]);
  213. i++;
  214. }
  215. },
  216. })
  217. },
  218. getcharacteristics: function () {
  219. wx.getBLEDeviceCharacteristics({
  220. deviceId: deviceId,
  221. serviceId: serviceId[0],
  222. success: function (res) {
  223. console.log('device getBLEDeviceCharacteristics:', res.characteristics)
  224. }
  225. })
  226. wx.getBLEDeviceCharacteristics({
  227. deviceId: deviceId,
  228. serviceId: serviceId[0],
  229. success: function (res) {
  230. i = 0;
  231. while (res.characteristics[i]) {
  232. characteristicId[i] = res.characteristics[i].uuid;
  233. console.log(characteristicId[i]);
  234. i++;
  235. }
  236. }
  237. })
  238. },
  239. startread: function () {
  240. wx.readBLECharacteristicValue({
  241. deviceId: deviceId,
  242. serviceId: serviceId[0],
  243. characteristicId: characteristicId[1],
  244. success: function (res) {
  245. console.log('readBLECharacteristicValue:', res.errCode)
  246. }
  247. })
  248. },
  249. startnotify: function () {
  250. wx.notifyBLECharacteristicValueChange({
  251. state: true,
  252. deviceId: deviceId,
  253. serviceId: serviceId[0],
  254. characteristicId: characteristicId[1],
  255. success: function (res) {
  256. console.log('notifyBLECharacteristicValueChange success', res.errMsg)
  257. }
  258. })
  259. function ab2hex(buffer) {
  260. var hexArr = Array.prototype.map.call(
  261. new Uint8Array(buffer),
  262. function (bit) {
  263. return ('00' + bit.toString(16)).slice(-2)
  264. }
  265. )
  266. return hexArr.join('');
  267. }
  268. wx.onBLECharacteristicValueChange(function (res) {
  269. console.log('characteristic value :', res.value)
  270. console.log('characteristic value comed:', ab2hex(res.value))
  271. })
  272. },
  273. startwrite: function () {
  274. var that = this
  275. let buffer = new ArrayBuffer(15)
  276. let dataView = new DataView(buffer)
  277. dataView.setUint8(1, 'FF')
  278. dataView.setUint8(2, '02')
  279. dataView.setUint8(3, '01')
  280. dataView.setUint8(4, '523F33F3D64455FF')
  281. dataView.setUint8(5, '02')
  282. dataView.setUint8(6, '05')
  283. dataView.setUint8(7, '06')
  284. dataView.setUint8(8, 'EE')
  285. // let data = that.data.sendData.split(',')
  286. // let dataBuffer = new ArrayBuffer(data.length)
  287. // let dataView = new DataView(dataBuffer)
  288. // for (let j = 0; j < data.length; j++) {
  289. // dataView.setUint8(j, '0x' + data[j])
  290. // }
  291. // for (let i = 0; i < dataView.byteLength; i++) {
  292. // var writeData = '0x' + dataView.getUint8(i).toString(16)
  293. // that.data.writeDatas = that.data.writeDatas + '\n' + writeData
  294. // }
  295. // var hex = "FF0401383838383838EE" //要发送的信息
  296. // console.log('要发送的信息是:'+hex)
  297. // var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
  298. // return parseInt(h, 16)
  299. // }))
  300. // console.log(typedArray)
  301. // var buffer1 = typedArray.buffer
  302. // wx.writeBLECharacteristicValue({
  303. // deviceId: deviceId,
  304. // serviceId: serviceId[0],
  305. // characteristicId: characteristicId[1],
  306. // value: buffer1,
  307. // success: function (res) {
  308. // console.log('writeBLECharacteristicValue success', res.errMsg)
  309. // },
  310. // fail(res){
  311. // console.log('写入失败', res.errMsg)
  312. // }
  313. // })
  314. var value_initial_1 = "FF 04 01 38 38 38 38 38 38 EE";
  315. var write_array = []; //用于发送数据的数组。
  316. /* 判断是否存在空格 */
  317. if (value_initial_1.indexOf(' ') > 0) {
  318. var value_initial = that.splitStr(value_initial_1, ' '); //存在空格时
  319. console.log('删除掉空格', value_initial);
  320. } else {
  321. var value_initial = value_initial_1; //不存在空格时
  322. }
  323. /* 判断字节是否超过20字节 */
  324. if (value_initial.length > 20) {
  325. //当字节超过20的时候,采用分段发送
  326. //选择16进制发送时
  327. var value_initial_exceed = value_initial;
  328. //将输入框的值取过来,方便循环
  329. var value_initial_average = Math.ceil(value_initial_exceed.length / 20); //将value_initial_exceed的长度除以20,余数再向上取一,确定循环几次
  330. console.log('需要循环的次数', value_initial_average);
  331. for (var i = 0; i < value_initial_average; i++) {
  332. if (value_initial_exceed.length > 20) {
  333. var value_initial_send = value_initial_exceed.slice(0, 20); //截取前20个字节
  334. console.log('截取到的值', value_initial_send);
  335. value_initial_exceed = value_initial_exceed.substring(20); //value_initial_exceed替换为取掉前20字节后的数据
  336. write_array.push(value_initial_send); //将所有截取的值放在一个数组
  337. } else {
  338. write_array.push(value_initial_exceed);
  339. }
  340. }
  341. console.log('write_array数组', write_array);
  342. write_array.map(function (val, index) {
  343. setTimeout(function () {
  344. var value_set = val;
  345. console.log('value_set', value_set);
  346. var write_function = that.write(value_set); //调用数据发送函数
  347. }, index * 100)
  348. });
  349. } else {
  350. //当字节不超过20的时候,直接发送
  351. var value = value_initial;
  352. var write_function = that.write(value); //调用数据发送函数
  353. }
  354. },
  355. openDoor: function () {
  356. var that = this
  357. // progressUtils.showLoding();
  358. aleApi.sendCommand({
  359. deviceId: "xx:xx:xx:xx:xx:xx",
  360. sendCommend: "xxxxxxxxx",
  361. onSuccessCallBack: function onSuccessCallBack(result) {
  362. wx.showToast({
  363. title: result,
  364. icon: '',
  365. image: '',
  366. duration: 1500,
  367. })
  368. console.log('result:' + result);
  369. }
  370. });
  371. },
  372. //写入数据
  373. write: function (str) {
  374. var that = this;
  375. var value = str;
  376. console.log('value', value);
  377. /* 将数值转为ArrayBuffer类型数据 */
  378. var typedArray = new Uint8Array(value.match(/[\da-f]{2}/gi).map(function (h) {
  379. return parseInt(h, 16)
  380. }));
  381. var buffer = typedArray.buffer;
  382. wx.writeBLECharacteristicValue({
  383. deviceId: deviceId,
  384. serviceId: serviceId[0],
  385. characteristicId: characteristicId[1],
  386. value: buffer,
  387. success: function (res) {
  388. console.log('数据发送成功', res);
  389. },
  390. fail: function (res) {
  391. console.log('调用失败', res);
  392. /* 调用失败时,再次调用 */
  393. wx.writeBLECharacteristicValue({
  394. deviceId: deviceId,
  395. serviceId: serviceId[0],
  396. characteristicId: characteristicId[1],
  397. value: buffer,
  398. success: function (res) {
  399. console.log('第2次数据发送成功', res);
  400. },
  401. fail: function (res) {
  402. console.log('第2次调用失败', res);
  403. /* 调用失败时,再次调用 */
  404. wx.writeBLECharacteristicValue({
  405. deviceId: deviceId,
  406. serviceId: serviceId[0],
  407. characteristicId: characteristicId[1],
  408. value: buffer,
  409. success: function (res) {
  410. console.log('第3次数据发送成功', res);
  411. },
  412. fail: function (res) {
  413. console.log('第3次调用失败', res);
  414. }
  415. });
  416. }
  417. });
  418. }
  419. });
  420. },
  421. /* 去除输入框输入的值中的空格 */
  422. splitStr: function (str, s) {
  423. var newStr = "";
  424. var strArray = str.split(s);
  425. for (var i = 0; i < strArray.length; i++) {
  426. newStr += strArray[i];
  427. }
  428. return newStr;
  429. },
  430. getBinaryData: function (message) { //将数据转为二进制数组
  431. let buffer = new ArrayBuffer(6);
  432. let dataView = new DataView(buffer);
  433. var numTitle = 0;
  434. for (var i = 0; i < message.length; i = i + 2) {
  435. var numStr16 = message.substr(i, 2);
  436. var num = parseInt(numStr16, 16);
  437. dataView.setUint8(numTitle, num);
  438. numTitle++;
  439. }
  440. return buffer;
  441. }
  442. })