wangfukang 2 years ago
parent
commit
a5e7d70a2e
  1. BIN
      components/.DS_Store
  2. 129
      components/kk-printer/index.vue
  3. 11
      components/kk-printer/utils/bluetoolth.js
  4. 1
      components/phone-verify/phone-verify.vue

BIN
components/.DS_Store

Binary file not shown.

129
components/kk-printer/index.vue

@ -120,7 +120,7 @@
let that = this
uni.showLoading({
mask: true,
title: '加载中...'
title: '打印中...'
});
uni.getSystemInfo({
success(res) {
@ -133,13 +133,26 @@
success: (res2) => {
that.sousuo()
},
fail: (res) => {
that.tui.toast('初始化失败', res)
this.isShowSearch = true
uni.hideLoading();
fail: (res) => {
console.log('初始化失败', res)
if(res.errCode == '10001'){
console.log('蓝牙未打开', res)
// that.tui.toast('请打开蓝牙后,再进行连接')
uni.showToast({
duration: 2000,
title: "请打开蓝牙后,再进行连接",
icon: 'none',
mask:true
})
}else{
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入')
that.isShowSearch = true
}
setTimeout(res=>{
uni.hideLoading();
},2000)
}
})
},
// 开始搜索附近的蓝牙外围设备
sousuo() {
@ -148,18 +161,20 @@
success(res3) {
that.jiantingshebei()
},
fail(err) {
that.tui.toast('获取设备失败', err)
fail(err) {
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入')
wx.closeBLEConnection({
deviceId: uni.getStorageSync('deviceId'),
success(res) {
console.log(res)
}
})
uni.removeStorageSync('deviceId');
that.isShowSearch = true
that.sousuo()
uni.hideLoading();
that.sousuo()
setTimeout(res=>{
uni.hideLoading();
},2000)
}
})
},
@ -179,7 +194,7 @@
that.lianejieshebei(uni.getStorageSync('deviceId'))
} else {
uni.hideLoading();
this.isShowSearch = true
that.isShowSearch = true
// 监听扫描到新设备事件
wx.onBluetoothDeviceFound((res4) => { //333
res4.devices.forEach((device) => {
@ -193,7 +208,11 @@
},
lianejieshebei(deviceId) {
let that = this;
let that = this;
uni.showLoading({
mask: true,
title: '加载中...'
});
// 连接设备
wx.createBLEConnection({ //555
deviceId: deviceId, // 搜索到设备的 deviceId
@ -201,8 +220,8 @@
that.isShowSearch = false
that.huoqufuwu(deviceId)
},
fail: (res) => {
that.tui.toast('连接设备失败', res)
fail: (res) => {
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入')
if (uni.getStorageSync('deviceId')) {
that.huoqufuwu(uni.getStorageSync('deviceId'))
} else {
@ -216,8 +235,9 @@
that.sousuo()
that.isShowSearch = true
}
uni.hideLoading();
setTimeout(res=>{
uni.hideLoading();
},2000)
}
})
},
@ -227,12 +247,10 @@
wx.getBLEDeviceServices({ //666
deviceId: deviceId, // 搜索到设备的 deviceId
success: (res5) => {
that.duxietezhengzhi(deviceId, res5)
},
fail: (res) => {
that.tui.toast('获取服务失败', res)
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入')
wx.closeBLEConnection({
deviceId: uni.getStorageSync('deviceId'),
success(res1) {
@ -240,7 +258,9 @@
}
})
that.isShowSearch = true
uni.hideLoading();
setTimeout(res=>{
uni.hideLoading();
},2000)
}
})
},
@ -251,10 +271,11 @@
deviceId: deviceId, // 搜索到设备的 deviceId
serviceId: res5.services[0].uuid, // 上一步中找到的某个服务
success: (res6) => {
console.log('characteristics',res6.characteristics)
for (let i = 0; i < res6.characteristics.length; i++) {
let item = res6.characteristics[i]
if (item.properties.write) { // 该特征值可写
let item = res6.characteristics[i]
that.startNoticeBle(deviceId,res5.services[0].uuid,res6.characteristics[2].uuid)
if (item.properties.write) { // 该特征值可写
uni.setStorageSync('deviceId', deviceId);
uni.setStorageSync('serviceId', res5.services[0].uuid);
@ -265,7 +286,7 @@
}
},
fail: (res) => {
that.tui.toast('读写特征值失败', res)
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入')
wx.closeBLEConnection({
deviceId: uni.getStorageSync('deviceId'),
success(res) {
@ -273,9 +294,55 @@
}
})
that.isShowSearch = true
uni.hideLoading();
setTimeout(res=>{
uni.hideLoading();
},2000)
}
})
},
// 开启蓝牙数据监听
startNoticeBle(deviceId,serviceId,characteristicId) {
let _this = this
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
success(res) {
console.log("监听成功===", res.errMsg, 'deviceId===', deviceId)
_this.GetDataFromBle();
},
fail: function(err) {
console.log('开启监听失败', err)
}
})
},
// 设备返回的数据接收
GetDataFromBle() {
var _this = this;
uni.onBLECharacteristicValueChange((res) => {
// 此时可以拿到蓝牙设备返回来的数据是一个ArrayBuffer类型数据,所以需要通过一个方法转换成字符串
console.log('返回的十六进制', _this.ab2hex(res.value))
_this.bleData = _this.ab2hex(res.value)
uni.hideLoading()
uni.closeBLEConnection({
deviceId: uni.getStorageSync('deviceId'),
success(res) {
console.log('断开')
},
fail(err) {
}
})
})
},
ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
},
xierushuju() {
let that = this;
@ -312,12 +379,10 @@
serviceId: uni.getStorageSync('serviceId'),
characteristicId: uni.getStorageSync('characteristicId'),
value: buffer,
success(res) {
success(res) {
},
fail(res) {
wx.closeBLEConnection({
deviceId: uni.getStorageSync('deviceId'),
success(res) {
@ -325,14 +390,14 @@
}
})
if (uni.getStorageSync('deviceId')) {
that.xierushuju()
} else {
that.jiantingshebei()
}
}
uni.hideLoading();
}
})
uni.hideLoading();
},
handleSearchClose() {
this.isShowSearch = false

11
components/kk-printer/utils/bluetoolth.js

@ -197,15 +197,10 @@ export function getDeviceCharacteristics(deviceId, services, success, fail) {
* @param {function} onChange 监听蓝牙设备传递数据回调函数
*/
export function onGetBLECharacteristicValueChange(options, onChange = function() {}) {
uniAsyncPromise('notifyBLECharacteristicValueChange', {
state: true,
...options,
}).then((res) => {
uni.onBLECharacteristicValueChange(onChange);
});
}
@ -410,10 +405,12 @@ function Hex2Str(num) {
* 8像素=1mm
* printNum: 打印张数,默认为1
* rotation:页面整体旋转 1-90度 2-180度 3-270度 其他-不旋转
* back: 页面回退 back单位像素 退纸1mm,给back 8
*/
export function CreatCPCLPage(width, height, printNum, rotation, offset = 0) {
export function CreatCPCLPage(width, height, printNum, rotation, back, offset = 0) {
var strCmd = '! ' + offset + ' 200 200 ' + height + ' ' + printNum + '\n';
strCmd += "PAGE-WIDTH " + width + '\n';
strCmd += "PAGE-WIDTH " + width + '\n';
strCmd += "BF "+ back + "\n";
if (rotation == 1)
strCmd += "ZPROTATE90\n";
else if (rotation == 2)

1
components/phone-verify/phone-verify.vue

@ -48,7 +48,6 @@
this.choose = true
this.setTime()
}else if(reg(e,k) && this.isChoose == true){
console.log('通过了')
// 调用API
}
},

Loading…
Cancel
Save