小程序蓝牙通信例子
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.

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