|
|
|
@ -1,6 +1,6 @@ |
|
|
|
<template> |
|
|
|
<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}} |
|
|
|
</view> |
|
|
|
<view class="kk-shadow" :class="isShowSearch?'show':''" @tap="handleSearchClose"> |
|
|
|
@ -85,7 +85,8 @@ |
|
|
|
serviceId: '', |
|
|
|
writeId: '', |
|
|
|
readId: '', |
|
|
|
iosNo: false |
|
|
|
iosNo: false, |
|
|
|
deviceType: 'ios' |
|
|
|
} |
|
|
|
}, |
|
|
|
props: { |
|
|
|
@ -100,7 +101,7 @@ |
|
|
|
default: '打印中...' |
|
|
|
}, |
|
|
|
bufferData: { |
|
|
|
type: String, |
|
|
|
type: Array, |
|
|
|
require: true |
|
|
|
}, |
|
|
|
roomcode: { |
|
|
|
@ -150,6 +151,14 @@ |
|
|
|
}, |
|
|
|
mounted() { |
|
|
|
|
|
|
|
}, |
|
|
|
onLoad() { |
|
|
|
uni.getSystemInfoSync({ |
|
|
|
success: function(res) { |
|
|
|
this.deviceType = res.osName |
|
|
|
console.log('设备类型', res.osName) |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
beforeDestroy() { |
|
|
|
|
|
|
|
@ -159,102 +168,184 @@ |
|
|
|
doNothing() { |
|
|
|
return; |
|
|
|
}, |
|
|
|
clickBtn() { |
|
|
|
uni.setStorageSync('ok', true) |
|
|
|
this.handlePrintTap() |
|
|
|
}, |
|
|
|
//点击打印按钮 |
|
|
|
handlePrintTap() { |
|
|
|
handlePrintTap() { |
|
|
|
let that = this |
|
|
|
// 初始化蓝牙模块 |
|
|
|
wx.openBluetoothAdapter({ |
|
|
|
wx.openBluetoothAdapter({ //111 |
|
|
|
mode: 'central', |
|
|
|
success: (res2) => { |
|
|
|
console.log('初始化蓝牙模块', res2) |
|
|
|
// 开始搜索附近的蓝牙外围设备 |
|
|
|
wx.startBluetoothDevicesDiscovery({ |
|
|
|
success(res3) { |
|
|
|
console.log('获取设备成功', res3) |
|
|
|
// 监听扫描到新设备事件 |
|
|
|
wx.onBluetoothDeviceFound((res4) => { |
|
|
|
console.log('监听新设备列表,拿到想要的就自动停止', res4) |
|
|
|
res4.devices.forEach((device) => { |
|
|
|
if(device.name == 'QR380A-6824_Ble'){ //搜索到需要的设备 |
|
|
|
// 找到要搜索的设备后,及时停止扫描 |
|
|
|
wx.stopBluetoothDevicesDiscovery() |
|
|
|
wx.createBLEConnection({ |
|
|
|
deviceId: device.deviceId, // 搜索到设备的 deviceId |
|
|
|
success: () => { |
|
|
|
console.log('连接设备成功') |
|
|
|
wx.getBLEDeviceServices({ |
|
|
|
deviceId: device.deviceId, // 搜索到设备的 deviceId |
|
|
|
success: (res5) => { |
|
|
|
console.log('获取服务成功',res5) |
|
|
|
for (let i = 0; i < res5.services.length; i++) { |
|
|
|
if (res5.services[i].isPrimary) { |
|
|
|
// 可根据具体业务需要,选择一个主服务进行通信 |
|
|
|
} |
|
|
|
} |
|
|
|
wx.getBLEDeviceCharacteristics({ |
|
|
|
deviceId:device.deviceId, // 搜索到设备的 deviceId |
|
|
|
serviceId:res5.services[0].uuid, // 上一步中找到的某个服务 |
|
|
|
success: (res6) => { |
|
|
|
console.log('读写特征值成功',res6) |
|
|
|
for (let i =0; i <res6.characteristics.length; i++) { |
|
|
|
let item =res6.characteristics[i] |
|
|
|
if (item.properties.write) { // 该特征值可写 |
|
|
|
console.log('该特征值可写') |
|
|
|
let buffer = gbk.strToGBKByte(that.aaaSS()) |
|
|
|
|
|
|
|
let opt = { |
|
|
|
deviceId: device.deviceId, |
|
|
|
serviceId: res5.services[0].uuid, |
|
|
|
value:buffer, |
|
|
|
characteristicId:item.uuid, |
|
|
|
lasterSuccess: that.onPrintSuccess, |
|
|
|
onceLength: 180 //ios 180 安卓 20 |
|
|
|
} |
|
|
|
blesdk.sendDataToDevice(opt); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
}) |
|
|
|
}else{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
console.log('获取设备失败', res) |
|
|
|
that.sousuo() |
|
|
|
}, |
|
|
|
fail: (res) => { |
|
|
|
that.tui.toast('初始化失败', res) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
}, |
|
|
|
// 开始搜索附近的蓝牙外围设备 |
|
|
|
sousuo() { |
|
|
|
let that = this; |
|
|
|
wx.startBluetoothDevicesDiscovery({ //222 |
|
|
|
success(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) => { //333 |
|
|
|
console.log('监听新设备列表,拿到想要的就自动停止', res4) |
|
|
|
res4.devices.forEach((device) => { |
|
|
|
if (device.name == 'QR380A-6824_Ble') { //搜索到需要的设备 |
|
|
|
// 找到要搜索的设备后,及时停止扫描 |
|
|
|
wx.stopBluetoothDevicesDiscovery() //444 |
|
|
|
that.lianejieshebei(device.deviceId); |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
lianejieshebei(deviceId) { |
|
|
|
let that = this; |
|
|
|
// 连接设备 |
|
|
|
wx.createBLEConnection({ //555 |
|
|
|
deviceId: deviceId, // 搜索到设备的 deviceId |
|
|
|
success: () => { |
|
|
|
console.log('连接设备成功') |
|
|
|
that.huoqufuwu(deviceId) |
|
|
|
}, |
|
|
|
fail: (res) => { |
|
|
|
that.tui.toast('连接设备失败', res) |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
huoqufuwu(deviceId) { |
|
|
|
let that = this; |
|
|
|
//获取服务 |
|
|
|
wx.getBLEDeviceServices({ //666 |
|
|
|
deviceId: deviceId, // 搜索到设备的 deviceId |
|
|
|
success: (res5) => { |
|
|
|
console.log('获取服务成功', res5) |
|
|
|
that.duxietezhengzhi(deviceId, res5) |
|
|
|
}, |
|
|
|
fail: (res) => { |
|
|
|
that.tui.toast('获取服务失败', res) |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
duxietezhengzhi(deviceId, res5) { |
|
|
|
let that = this; |
|
|
|
//读写特征值 |
|
|
|
wx.getBLEDeviceCharacteristics({ //777 |
|
|
|
deviceId: deviceId, // 搜索到设备的 deviceId |
|
|
|
serviceId: res5.services[0].uuid, // 上一步中找到的某个服务 |
|
|
|
success: (res6) => { |
|
|
|
console.log('读写特征值成功', res6) |
|
|
|
that.xierushuju(deviceId, res5, res6) |
|
|
|
}, |
|
|
|
fail: (res) => { |
|
|
|
console.log('初始化失败', 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++) { |
|
|
|
let item = res6.characteristics[i] |
|
|
|
if (item.properties.write) { // 该特征值可写 |
|
|
|
console.log('该特征值可写') |
|
|
|
if (this.defaultText == '打印小票') { |
|
|
|
that.$emit('onPrintSmall'); |
|
|
|
} else if (this.defaultText == '确认打印') { |
|
|
|
that.$emit('goPrint'); |
|
|
|
} 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); |
|
|
|
// // } |
|
|
|
|
|
|
|
}, |
|
|
|
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; |
|
|
|
// } |
|
|
|
|
|
|
|
// }) |
|
|
|
// return; |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
_writeBLECharacteristicValue(buffer) { |
|
|
|
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) |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onGetDevice(res) { |
|
|
|
this.devicesList = res; |
|
|
|
}, |
|
|
|
@ -333,6 +424,7 @@ |
|
|
|
|
|
|
|
}, |
|
|
|
onPrintSuccess() { |
|
|
|
console.log("打印完成22222") |
|
|
|
this.isPrinting = false; |
|
|
|
this.$u.route('/pages/toll/toll', { |
|
|
|
roomcode: this.roomcode, |
|
|
|
|