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

277 lines
6.8 KiB

const app = getApp();
var longUtil = require('./../../utils/long');
var util = require('../../utils/util');
import { BLE } from "./../../utils/btls/ble";
const emitter = app.globalData.emitter
Page({
/**
* 页面的初始数据
*/
data: {
baseUrlImg: app.globalData.baseUrlImg,
bleStatus: false,
respond: [],
blueyes: false, //是否重发同步指令
connectStatus: "",
info:"未初始化蓝牙适配器",
},
btntest1(event){
var that = this;
this.startconnect()
},
btntest2(event){
var that = this
let userTelephone = "13028878230"
let userPassword = "130288"
let pwd = "12344321"
let openLockType = 2
this.openLock(userTelephone, userPassword, pwd, openLockType)
},
btntest3(event){
var that = this
let managerTelephone = "19270949446"
let managerPassword = "123456"
let userTelephone = "38580151200"
let userPassword = "385801"
let userStartTime = "20210813010101"
let userEndTime = "20680813010101"
let fpDataLength = 2048
let fpStartTime = "20210813010101"
let fpEndTime = "20680813010101"
let times = 255
let recycleTime = 65535
this.startFingerprint(managerTelephone, managerPassword, userTelephone, userPassword, userStartTime, userEndTime,
fpDataLength, fpStartTime, fpEndTime, times, recycleTime)
},
//获取输入框的数据
getmsg(event){
this.setData({
sendmsg:event.detail.value
})
},
btntest4(event){
var that = this;
this.closeblue()
},
startconnect() {
//if(checkWechatVersion())
{
const ble = new BLE("yxwl01680000004895", emitter)
app.globalData.ble = ble
this.watchBLE()
app.globalData.ble.init()
}
},
watchBLE() {
if (app.globalData.ble) {
app.globalData.ble.listen(res => {
if (res.type == 'connect') {
if (res.data == "未打开适配器") {
wx.showModal({
title: "提示",
content: "请检查手机蓝牙和定位功能是否打开?",
showCancel: false,
confirmText: "确定",
});
} else {
this.setData({
info: res.data
})
app.globalData.bleStatus = res.data
}
}
})
}
},
closeblue() {
wx.showLoading({
title: "正在停止...",
});
app.globalData.ble.close()
setTimeout(() => {
wx.hideLoading()
}, 1000);
app.globalData.bleStatus = false
this.setData({
bleStatus: false,
respond: []
})
},
openLock(userTelephone, userPassword, pwd, openLockType) {
wx.showLoading({
title: "正在发送...",
});
setTimeout(() => {
wx.hideLoading()
}, 1000);
let cmd = 0x41
let ut = util.stringToUint8Array(userTelephone)
let up = util.stringToUint8Array(userPassword)
let p = util.hexStringToBytesWithPadding(pwd, 10, 0xff)
let length = 1+11+6+10+1
let b = new Uint8Array(length)
b[0] = cmd
b.set(ut, 1)
b.set(up, 12)
b.set(p, 18)
b[28] = openLockType
console.log(b)
app.globalData.ble.send(cmd, b, length)
},
startFingerprint(managerTelephone, managerPassword, userTelephone, userPassword, userStartTime, userEndTime,
fpDataLength, fpStartTime, fpEndTime, times, recycleTime) {
wx.showLoading({
title: "正在发送...",
});
setTimeout(() => {
wx.hideLoading()
}, 1000);
let cmd = 0x87
let mt = util.stringToUint8Array(managerTelephone)
let mp = util.stringToUint8Array(managerPassword)
let ut = util.stringToUint8Array(userTelephone)
let up = util.stringToUint8Array(userPassword)
let ust = util.time40ToBytes(userStartTime)
let uet = util.time40ToBytes(userEndTime)
let fpPacketCount = (fpDataLength+511)/512
let crc = 0xAAFF46D3;
let fst = util.time40ToBytes(fpStartTime)
let fet = util.time40ToBytes(fpEndTime)
let length = 1 + 11 + 6 + 11 + 6 + 5 + 5 + 4 + 4 + 4 + 5 + 5 + 1 + 2
let b = new Uint8Array(length)
b[0] = cmd
b.set(mt, 1)
b.set(mp, 12)
b.set(ut, 18)
b.set(up, 29)
b.set(ust, 35)
b.set(uet, 40)
b[45] = (fpDataLength>>24)&0xff;
b[46] = (fpDataLength>>16)&0xff;
b[47] = (fpDataLength>>8)&0xff;
b[48] = fpDataLength&0xff;
b[49] = (fpPacketCount>>24)&0xff;
b[50] = (fpPacketCount>>16)&0xff;
b[51] = (fpPacketCount>>8)&0xff;
b[52] = fpPacketCount&0xff;
b[53] = (crc>>24)&0xff;
b[54] = (crc>>16)&0xff;
b[55] = (crc>>8)&0xff;
b[56] = crc&0xff;
b.set(fst, 57)
b.set(fet, 62)
b[67] = times;
b[68] = (recycleTime>>8)&0xff;
b[69] = recycleTime&0xff;
console.log(b)
app.globalData.ble.send(cmd, b, length)
},
})
/**
* 版本比较
*/
function versionCompare (ver1, ver2) { //版本比较
// console.log("ver1" + ver1 + 'ver2' + ver2);
var version1pre = parseFloat(ver1)
var version2pre = parseFloat(ver2)
var version1next = parseInt(ver1.replace(version1pre + ".", ""))
var version2next = parseInt(ver2.replace(version2pre + ".", ""))
if (version1pre > version2pre)
return true
else if (version1pre < version2pre)
return false
else {
if (version1next > version2next)
return true
else
return false
}
}
/**
* 微信版本检测
* Android从微信 6.5.7 开始支持,iOS从微信 6.5.6 开始支持
*/
function checkWechatVersion() {
wx.getSystemInfo({
success: function (res) {
let si = res;
if (si.platform == 'android' && versionCompare('6.5.7', si.version)) {
console.log("当前安卓微信版本过低,请更新至最新版本体验");
wx.showModal({
title: '提示',
content: '当前微信版本过低,请更新至最新版本体验',
showCancel: false
})
return false;
}else if (si.platform == 'ios' && versionCompare('6.5.6', si.version)) {
console.log("当前苹果微信版本过低,请更新至最新版本体验");
wx.showModal({
title: '提示',
content: '当前微信版本过低,请更新至最新版本体验',
showCancel: false
})
return false;
}
return true;
}
})
}