wangfukang 3 years ago
parent
commit
0d098ccdf0
  1. 218
      components/kk-printer/index.vue
  2. 10
      components/kk-printer/utils/bluetoolth.js

218
components/kk-printer/index.vue

@ -1,6 +1,6 @@
<template> <template>
<view class="kk-printer"> <view class="kk-printer">
<view class="kk-printer-btn" @tap="clickBtn" style="color:#FFF;"> <view class="kk-printer-btn" @tap="handlePrintTap" style="color:#FFF;">
{{isPrinting?printingText:defaultText}} {{isPrinting?printingText:defaultText}}
</view> </view>
<view class="kk-shadow" :class="isShowSearch?'show':''" @tap="handleSearchClose"> <view class="kk-shadow" :class="isShowSearch?'show':''" @tap="handleSearchClose">
@ -85,7 +85,8 @@
serviceId: '', serviceId: '',
writeId: '', writeId: '',
readId: '', readId: '',
iosNo: false iosNo: false,
deviceType: 'ios'
} }
}, },
props: { props: {
@ -100,7 +101,7 @@
default: '打印中...' default: '打印中...'
}, },
bufferData: { bufferData: {
type: String, type: Array,
require: true require: true
}, },
roomcode: { roomcode: {
@ -150,6 +151,14 @@
}, },
mounted() { mounted() {
},
onLoad() {
uni.getSystemInfoSync({
success: function(res) {
this.deviceType = res.osName
console.log('设备类型', res.osName)
}
});
}, },
beforeDestroy() { beforeDestroy() {
@ -159,102 +168,184 @@
doNothing() { doNothing() {
return; return;
}, },
clickBtn() {
uni.setStorageSync('ok', true)
this.handlePrintTap()
},
//点击打印按钮 //点击打印按钮
handlePrintTap() { handlePrintTap() {
let that = this let that = this
// 初始化蓝牙模块 // 初始化蓝牙模块
wx.openBluetoothAdapter({ wx.openBluetoothAdapter({ //111
mode: 'central', mode: 'central',
success: (res2) => { success: (res2) => {
console.log('初始化蓝牙模块', res2) console.log('初始化蓝牙模块', res2)
that.sousuo()
},
fail: (res) => {
that.tui.toast('初始化失败', res)
}
})
},
// 开始搜索附近的蓝牙外围设备 // 开始搜索附近的蓝牙外围设备
wx.startBluetoothDevicesDiscovery({ sousuo() {
let that = this;
wx.startBluetoothDevicesDiscovery({ //222
success(res3) { success(res3) {
console.log('获取设备成功', res3) console.log('获取设备成功', res3)
that.jiantingshebei()
},
fail(err) {
that.tui.toast('获取设备失败', res)
}
})
},
/**
* jiantingshebei方法,判断缓存中有没有deviceId等数据的时候,
* 有: 直接拿到缓存中的deviceId等,直接请求 wx.createBLEConnection
* 没有:才会调用 wx.onBluetoothDeviceFound 搜索
*/
jiantingshebei() {
let that = this;
if (uni.getStorageSync('deviceId')) {
that.lianejieshebei(uni.getStorageSync('deviceId'))
} else {
// 监听扫描到新设备事件 // 监听扫描到新设备事件
wx.onBluetoothDeviceFound((res4) => { wx.onBluetoothDeviceFound((res4) => { //333
console.log('监听新设备列表,拿到想要的就自动停止', res4) console.log('监听新设备列表,拿到想要的就自动停止', res4)
res4.devices.forEach((device) => { res4.devices.forEach((device) => {
if (device.name == 'QR380A-6824_Ble') { //搜索到需要的设备 if (device.name == 'QR380A-6824_Ble') { //搜索到需要的设备
// 找到要搜索的设备后,及时停止扫描 // 找到要搜索的设备后,及时停止扫描
wx.stopBluetoothDevicesDiscovery() wx.stopBluetoothDevicesDiscovery() //444
wx.createBLEConnection({ that.lianejieshebei(device.deviceId);
deviceId: device.deviceId, // 搜索到设备的 deviceId }
})
})
}
},
lianejieshebei(deviceId) {
let that = this;
// 连接设备
wx.createBLEConnection({ //555
deviceId: deviceId, // 搜索到设备的 deviceId
success: () => { success: () => {
console.log('连接设备成功') console.log('连接设备成功')
wx.getBLEDeviceServices({ that.huoqufuwu(deviceId)
deviceId: device.deviceId, // 搜索到设备的 deviceId },
fail: (res) => {
that.tui.toast('连接设备失败', res)
}
})
},
huoqufuwu(deviceId) {
let that = this;
//获取服务
wx.getBLEDeviceServices({ //666
deviceId: deviceId, // 搜索到设备的 deviceId
success: (res5) => { success: (res5) => {
console.log('获取服务成功', res5) console.log('获取服务成功', res5)
for (let i = 0; i < res5.services.length; i++) { that.duxietezhengzhi(deviceId, res5)
if (res5.services[i].isPrimary) { },
// 可根据具体业务需要,选择一个主服务进行通信 fail: (res) => {
} that.tui.toast('获取服务失败', res)
} }
wx.getBLEDeviceCharacteristics({ })
deviceId:device.deviceId, // 搜索到设备的 deviceId },
duxietezhengzhi(deviceId, res5) {
let that = this;
//读写特征值
wx.getBLEDeviceCharacteristics({ //777
deviceId: deviceId, // 搜索到设备的 deviceId
serviceId: res5.services[0].uuid, // 上一步中找到的某个服务 serviceId: res5.services[0].uuid, // 上一步中找到的某个服务
success: (res6) => { success: (res6) => {
console.log('读写特征值成功', res6) console.log('读写特征值成功', res6)
that.xierushuju(deviceId, res5, res6)
},
fail: (res) => {
that.tui.toast('读写特征值失败', res)
}
})
},
xierushuju(deviceId, res5, res6) {
let that = this;
uni.setStorageSync('deviceId', deviceId);
uni.setStorageSync('serviceId', res5.services[0].uuid);
uni.setStorageSync('characteristicId', item.uuid);
//写入数据
for (let i = 0; i < res6.characteristics.length; i++) { for (let i = 0; i < res6.characteristics.length; i++) {
let item = res6.characteristics[i] let item = res6.characteristics[i]
if (item.properties.write) { // 该特征值可写 if (item.properties.write) { // 该特征值可写
console.log('该特征值可写') console.log('该特征值可写')
let buffer = gbk.strToGBKByte(that.aaaSS()) if (this.defaultText == '打印小票') {
that.$emit('onPrintSmall');
let opt = { } else if (this.defaultText == '确认打印') {
deviceId: device.deviceId, that.$emit('goPrint');
serviceId: res5.services[0].uuid,
value:buffer,
characteristicId:item.uuid,
lasterSuccess: that.onPrintSuccess,
onceLength: 180 //ios 180 安卓 20
}
blesdk.sendDataToDevice(opt);
}
}
}
})
}
})
}
})
} else { } else {
that.$emit('onPrint');
}
console.log('查看数据是否正确', this.bufferData)
let buffer = gbk.strToGBKByte(data)
const maxChunk = 20;
const delay = 20;
for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) {
let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length);
setTimeout(this._writeBLECharacteristicValue, j * delay, subPackage);
}
// that.$nextTick(()=>{
// for(var m=0;m<that.bufferData.length;m++){
// // if(m > 0){
// // console.log('打印前',that.bufferData[m])
// // setTimeout(res=> {
// // var data = that.bufferData[m]
// // let buffer = gbk.strToGBKByte(data)
// // let opt = {
// // deviceId: deviceId,
// // serviceId: res5.services[0].uuid,
// // value:buffer,
// // characteristicId:item.uuid,
// // lasterSuccess: that.onPrintSuccess,
// // onceLength: that.deviceType == 'android'?20:180 //ios 180 安卓 20
// // }
// // blesdk.sendDataToDevice(opt);
// // },3000 * m)
// // }else{
// console.log('打印前',that.bufferData[m])
// var data = that.bufferData[m]
// var buffer = gbk.strToGBKByte(that.bufferData[m])
// var opt = {
// deviceId: deviceId,
// serviceId: res5.services[0].uuid,
// value:buffer,
// characteristicId:item.uuid,
// lasterSuccess: that.onPrintSuccess,
// onceLength: 20 //ios 180 安卓 20
// }
// blesdk.sendDataToDevice(opt);
// // }
// }
// })
// return;
} }
})
})
},
fail(err) {
console.log('获取设备失败', res)
} }
})
}, },
fail: (res) => { _writeBLECharacteristicValue(buffer) {
console.log('初始化失败', res) wx.writeBLECharacteristicValue({
deviceId:uni.getStorageSync('deviceId'),
serviceId: uni.getStorageSync('serviceId'),
characteristicId: uni.getStorageSync('characteristicId'),
value: buffer,
success(res) {
console.log('writeBLECharacteristicValue success', res)
},
fail(res) {
console.log('writeBLECharacteristicValue fail', res)
} }
}) })
}, },
aaaSS() {
let strCmd = blesdk.CreatCPCLPage(440, 320, 1, 0);
strCmd += blesdk.addCPCLText('0', '30', '24', '0', 0, '商品名:张三')
strCmd += blesdk.addCPCLText('0', '80', '24', '0', 0, '货 号:ZS001')
strCmd += blesdk.addCPCLText('0', '130', '24', '0', 0, '供应商:李四')
strCmd += blesdk.addCPCLText('0', '180', '24', '0', 0, '批发价:100')
strCmd += blesdk.addCPCLText('0', '230', '24', '0', 0, '零售价:100')
strCmd += blesdk.addCPCLQRCode(200, 140, 'H', '1', 6, ['45646132464651648','1654867048641651654']) //二维码
strCmd += blesdk.addCPCLLocation(0); //对齐指令 0-左对齐 1-右对齐 2-居中
strCmd += blesdk.addCPCLPrint();
return strCmd;
},
onGetDevice(res) { onGetDevice(res) {
this.devicesList = res; this.devicesList = res;
}, },
@ -333,6 +424,7 @@
}, },
onPrintSuccess() { onPrintSuccess() {
console.log("打印完成22222")
this.isPrinting = false; this.isPrinting = false;
this.$u.route('/pages/toll/toll', { this.$u.route('/pages/toll/toll', {
roomcode: this.roomcode, roomcode: this.roomcode,

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

@ -242,12 +242,22 @@ export function sendDataToDevice(options) {
value: options.value.slice(speed, byteLength), value: options.value.slice(speed, byteLength),
}); });
} else { } else {
console.log("打印完成")
options.lasterSuccess && options.lasterSuccess(); options.lasterSuccess && options.lasterSuccess();
} }
}) })
.catch((res) => { .catch((res) => {
console.log('打印错误了',res) console.log('打印错误了',res)
uni.showToast({
title: res,
icon: 'none'
})
}); });
}else{
uni.showToast({
title: '没数据',
icon: 'none'
})
} }
} }
export function charToArrayBuffer(str) { export function charToArrayBuffer(str) {

Loading…
Cancel
Save