小程序蓝牙通信例子
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
7.4 KiB

  1. const app = getApp();
  2. var longUtil = require('./../../utils/long');
  3. var util = require('../../utils/util');
  4. import { BLE } from "./../../utils/btls/ble";
  5. const emitter = app.globalData.emitter
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. baseUrlImg: app.globalData.baseUrlImg,
  12. bleStatus: false,
  13. respond: [],
  14. blueyes: false, //是否重发同步指令
  15. connectStatus: "",
  16. info:"未初始化蓝牙适配器",
  17. },
  18. btntest1(event){
  19. var that = this;
  20. this.startconnect()
  21. },
  22. btntest2(event){
  23. var that = this
  24. let userTelephone = "13028878230"
  25. let userPassword = "130288"
  26. let pwd = "12344321"
  27. let openLockType = 2
  28. this.openLock(userTelephone, userPassword, pwd, openLockType)
  29. },
  30. btntest3(event){
  31. var that = this
  32. let deviceNum = "yxwl01680000000362"
  33. this.startFingerprint(deviceNum)
  34. },
  35. /*
  36. btntest3(event){
  37. var that = this
  38. let managerTelephone = "19270949446"
  39. let managerPassword = "123456"
  40. let userTelephone = "38580151200"
  41. let userPassword = "385801"
  42. let userStartTime = "20210813010101"
  43. let userEndTime = "20680813010101"
  44. let fpDataLength = 2048
  45. let fpStartTime = "20210813010101"
  46. let fpEndTime = "20680813010101"
  47. let times = 255
  48. let recycleTime = 65535
  49. this.startFingerprint(managerTelephone, managerPassword, userTelephone, userPassword, userStartTime, userEndTime,
  50. fpDataLength, fpStartTime, fpEndTime, times, recycleTime)
  51. },
  52. */
  53. //获取输入框的数据
  54. getmsg(event){
  55. this.setData({
  56. sendmsg:event.detail.value
  57. })
  58. },
  59. btntest4(event){
  60. var that = this;
  61. this.closeblue()
  62. },
  63. startconnect() {
  64. //if(checkWechatVersion())
  65. {
  66. const ble = new BLE("yxwl01680000004895", emitter)
  67. app.globalData.ble = ble
  68. this.watchBLE()
  69. app.globalData.ble.init()
  70. }
  71. },
  72. watchBLE() {
  73. if (app.globalData.ble) {
  74. app.globalData.ble.listen(res => {
  75. if (res.type == 'connect') {
  76. if (res.data == "未打开适配器") {
  77. wx.showModal({
  78. title: "提示",
  79. content: "请检查手机蓝牙和定位功能是否打开?",
  80. showCancel: false,
  81. confirmText: "确定",
  82. });
  83. } else {
  84. this.setData({
  85. info: res.data
  86. })
  87. app.globalData.bleStatus = res.data
  88. }
  89. }
  90. })
  91. }
  92. },
  93. closeblue() {
  94. wx.showLoading({
  95. title: "正在停止...",
  96. });
  97. app.globalData.ble.close()
  98. setTimeout(() => {
  99. wx.hideLoading()
  100. }, 1000);
  101. app.globalData.bleStatus = false
  102. this.setData({
  103. bleStatus: false,
  104. respond: []
  105. })
  106. },
  107. openLock(userTelephone, userPassword, pwd, openLockType) {
  108. wx.showLoading({
  109. title: "正在发送...",
  110. });
  111. setTimeout(() => {
  112. wx.hideLoading()
  113. }, 1000);
  114. let cmd = 0x41
  115. let ut = util.stringToUint8Array(userTelephone)
  116. let up = util.stringToUint8Array(userPassword)
  117. let p = util.hexStringToBytesWithPadding(pwd, 10, 0xff)
  118. let length = 1+11+6+10+1
  119. let b = new Uint8Array(length)
  120. b[0] = cmd
  121. b.set(ut, 1)
  122. b.set(up, 12)
  123. b.set(p, 18)
  124. b[28] = openLockType
  125. console.log(b)
  126. app.globalData.ble.send(cmd, b, length)
  127. },
  128. startFingerprint(deviceNum) {
  129. wx.showLoading({
  130. title: "正在发送...",
  131. });
  132. setTimeout(() => {
  133. wx.hideLoading()
  134. }, 1000);
  135. let cmd = 0x29
  136. let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)
  137. let length = 1 + 20 + 5 + 1 + 1
  138. let b = new Uint8Array(length)
  139. b[0] = cmd
  140. b.set(dn, 1)
  141. b[21] = 0x00
  142. b[22] = 0x36
  143. b[23] = 0x00
  144. b[24] = 0x00
  145. b[25] = 0x08
  146. b[26] = 0x03
  147. b[27] = 0x01
  148. console.log(b)
  149. app.globalData.ble.send(cmd, b, length)
  150. },
  151. startFingerprint_1(managerTelephone, managerPassword, userTelephone, userPassword, userStartTime, userEndTime,
  152. fpDataLength, fpStartTime, fpEndTime, times, recycleTime) {
  153. wx.showLoading({
  154. title: "正在发送...",
  155. });
  156. setTimeout(() => {
  157. wx.hideLoading()
  158. }, 1000);
  159. let cmd = 0x87
  160. let mt = util.stringToUint8Array(managerTelephone)
  161. let mp = util.stringToUint8Array(managerPassword)
  162. let ut = util.stringToUint8Array(userTelephone)
  163. let up = util.stringToUint8Array(userPassword)
  164. let ust = util.time40ToBytes(userStartTime)
  165. let uet = util.time40ToBytes(userEndTime)
  166. let fpPacketCount = (fpDataLength+511)/512
  167. let crc = 0xAAFF46D3;
  168. let fst = util.time40ToBytes(fpStartTime)
  169. let fet = util.time40ToBytes(fpEndTime)
  170. let length = 1 + 11 + 6 + 11 + 6 + 5 + 5 + 4 + 4 + 4 + 5 + 5 + 1 + 2
  171. let b = new Uint8Array(length)
  172. b[0] = cmd
  173. b.set(mt, 1)
  174. b.set(mp, 12)
  175. b.set(ut, 18)
  176. b.set(up, 29)
  177. b.set(ust, 35)
  178. b.set(uet, 40)
  179. b[45] = (fpDataLength>>24)&0xff;
  180. b[46] = (fpDataLength>>16)&0xff;
  181. b[47] = (fpDataLength>>8)&0xff;
  182. b[48] = fpDataLength&0xff;
  183. b[49] = (fpPacketCount>>24)&0xff;
  184. b[50] = (fpPacketCount>>16)&0xff;
  185. b[51] = (fpPacketCount>>8)&0xff;
  186. b[52] = fpPacketCount&0xff;
  187. b[53] = (crc>>24)&0xff;
  188. b[54] = (crc>>16)&0xff;
  189. b[55] = (crc>>8)&0xff;
  190. b[56] = crc&0xff;
  191. b.set(fst, 57)
  192. b.set(fet, 62)
  193. b[67] = times;
  194. b[68] = (recycleTime>>8)&0xff;
  195. b[69] = recycleTime&0xff;
  196. console.log(b)
  197. app.globalData.ble.send(cmd, b, length)
  198. },
  199. })
  200. /**
  201. * 版本比较
  202. */
  203. function versionCompare (ver1, ver2) { //版本比较
  204. // console.log("ver1" + ver1 + 'ver2' + ver2);
  205. var version1pre = parseFloat(ver1)
  206. var version2pre = parseFloat(ver2)
  207. var version1next = parseInt(ver1.replace(version1pre + ".", ""))
  208. var version2next = parseInt(ver2.replace(version2pre + ".", ""))
  209. if (version1pre > version2pre)
  210. return true
  211. else if (version1pre < version2pre)
  212. return false
  213. else {
  214. if (version1next > version2next)
  215. return true
  216. else
  217. return false
  218. }
  219. }
  220. /**
  221. * 微信版本检测
  222. * Android从微信 6.5.7 开始支持iOS从微信 6.5.6 开始支持
  223. */
  224. function checkWechatVersion() {
  225. wx.getSystemInfo({
  226. success: function (res) {
  227. let si = res;
  228. if (si.platform == 'android' && versionCompare('6.5.7', si.version)) {
  229. console.log("当前安卓微信版本过低,请更新至最新版本体验");
  230. wx.showModal({
  231. title: '提示',
  232. content: '当前微信版本过低,请更新至最新版本体验',
  233. showCancel: false
  234. })
  235. return false;
  236. }else if (si.platform == 'ios' && versionCompare('6.5.6', si.version)) {
  237. console.log("当前苹果微信版本过低,请更新至最新版本体验");
  238. wx.showModal({
  239. title: '提示',
  240. content: '当前微信版本过低,请更新至最新版本体验',
  241. showCancel: false
  242. })
  243. return false;
  244. }
  245. return true;
  246. }
  247. })
  248. }