This commit is contained in:
aihe 2024-05-09 16:34:01 +08:00
parent 8553dbdc12
commit aaf4d23eff
2 changed files with 76 additions and 85 deletions

View File

@ -5,10 +5,11 @@
<button style="width: 400upx; color: #0081FF;" @click="onConn(item)">{{item.name}}</button>
</view>
<button style="margin-top: 100upx;" @click="senBleLabel()">标签打印</button>
<textarea auto-height placeholder-style="color:#F76260" placeholder="请输入票据信息" v-model="piaojuText" />
<button style="margin-top: 100upx;" @click="senBleLabel2()">票据打印</button>
<canvas :style="{width: canvasWidth +'px', height: canvasHeight +'px'}" canvas-id="firstCanvas" id="firstCanvas" class="firstCanvas" />
<!-- <textarea auto-height placeholder-style="color:#F76260" placeholder="请输入票据信息" v-model="piaojuText" />
<button style="margin-top: 100upx;" @click="senBleLabel2()">票据打印</button> -->
<canvas :style="{width: canvasWidth +'px', height: canvasHeight +'px'}" canvas-id="firstCanvas" id="firstCanvas"
class="firstCanvas" />
</view>
@ -17,26 +18,31 @@
<script>
import * as tsc from '@/common/libs/tsc.js'
import * as esc from '@/common/libs/esc.js'
import {
showToast,
showLoading,
hideLoading
} from "@/common/untool"
export default {
data() {
return {
devices: [],
currDev: null,
connId: '',
piaojuText:'',
piaojuText: '',
tableDomId: '',
tableImgPath: '',
canvasWidth: 800,
canvasHeight: 600,
msg: ''
}
},
watch:{
msg(){
watch: {
msg() {
uni.showToast({
title: this.msg
})
@ -73,53 +79,45 @@
if (res.discovering) {
that.stopFindBule()
}
//搜索蓝牙
//开始搜寻附近的蓝牙外围设备
console.log("开始搜寻附近的蓝牙外围设备")
uni.startBluetoothDevicesDiscovery({
success(res) {
console.log(res)
console.log(
'startBluetoothDevicesDiscovery:success',
res)
}
})
} else {
console.log('本机蓝牙不可用')
showToast('openBluetoothAdapter: 本机蓝牙不可用')
}
},
})
}
})
},
onDevice(){
onDevice() {
console.log("监听寻找到新设备的事件---------------")
var that = this
//监听寻找到新设备的事件
uni.onBluetoothDeviceFound(function(devices) {
console.log('--------------new-----------------------'+JSON.stringify(devices))
var re = JSON.parse(JSON.stringify(devices))
console.log(re.devices[0].name + " " + re.devices[0].deviceId)
let name = re.devices[0].name
if (name != "未知设备") {
let deviceId = re.devices[0].deviceId
that.devices.push({
name: name,
deviceId: deviceId,
services: []
})
}
console.log('-------------onBluetoothDeviceFound-----------------' + JSON.stringify(devices))
var re = JSON.parse(JSON.stringify(devices))
console.log(re.devices[0].name + " " + re.devices[0].deviceId)
let name = re.devices[0].name
if (name != "未知设备") {
let deviceId = re.devices[0].deviceId
that.devices.push({
name: name,
deviceId: deviceId,
services: []
})
}
})
},
stopFindBule() {
console.log("停止搜寻附近的蓝牙外围设备---------------")
console.log("stopFindBule: 停止搜寻附近的蓝牙外围设备---------------")
uni.stopBluetoothDevicesDiscovery({
success(res) {
console.log(res)
@ -134,35 +132,33 @@
deviceId: deviceId,
complete(res) {
if (res.errMsg == "createBLEConnection:ok") {
console.log("连接蓝牙-[" + item.name + "]--成功")
showToast("连接蓝牙-[" + item.name + "]--成功")
that.connId = deviceId;
that.currDev = item
// 注意!! 必须设置 延迟,否则获取不到服务数据
setTimeout(function() {
that.getBLEServices(deviceId)
}, 2000)
that.stopFindBule()
} else {
console.log(res)
showToast("连接蓝牙失败:", res.errMsg)
}
//连接成功 关闭搜索
that.stopFindBule()
},
})
},
getBLEServices(_deviceId) {
var that = this;
let deviceId = _deviceId
console.log("获取蓝牙设备所有服务(service)。---------------")
// 一次获取该设备所有的 getBLEDeviceServices、getBLEDeviceCharacteristics
uni.getBLEDeviceServices({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: deviceId,
complete(res) {
console.log(res)
let serviceId = ""
for (var s = 0; s < res.services.length; s++) {
console.log(res.services[s].uuid)
let serviceId = res.services[s].uuid
@ -174,13 +170,15 @@
success(res) {
var re = JSON.parse(JSON.stringify(res))
console.log('deviceId = [' + deviceId + '] serviceId = [' + serviceId + ']')
console.log('deviceId = [' + deviceId + '] serviceId = [' +
serviceId + ']')
for (var c = 0; c < re.characteristics.length; c++) {
// 查询可打印的 characteristics
if (re.characteristics[c].properties.write == true) {
let uuid = re.characteristics[c].uuid
console.log(' deviceId = [' + deviceId + '] serviceId = [' + serviceId + '] characteristics=[' +
console.log(' deviceId = [' + deviceId + '] serviceId = [' +
serviceId + '] characteristics=[' +
uuid)
for (var index in that.devices) {
if (that.devices[index].deviceId == deviceId) {
that.devices[index].services.push({
@ -189,33 +187,30 @@
})
break
}
}
console.log(JSON.stringify(that.devices))
console.log("that.devices======", JSON.stringify(that.devices))
}
}
}
})
}
},
fail(res) {
console.log(res)
showToast("getBLEDeviceServices:获取失败")
console.log("getBLEDeviceServices:获取失败", res)
},
})
},
senBlData(deviceId, serviceId, characteristicId,uint8Array) {
console.log('************deviceId = [' + deviceId + '] serviceId = [' + serviceId + '] characteristics=[' +characteristicId+ "]")
senBlData(deviceId, serviceId, characteristicId, uint8Array) {
console.log('************deviceId = [' + deviceId + '] serviceId = [' + serviceId +
'] characteristics=[' + characteristicId + "]")
var uint8Buf = Array.from(uint8Array);
function split_array(datas,size){
function split_array(datas, size) {
var result = {};
var j = 0
if(datas.length < size) {
if (datas.length < size) {
size = datas.length
}
for (var i = 0; i < datas.length; i += size) {
@ -230,7 +225,7 @@
// console.log(sendloop.length)
function realWriteData(sendloop, i) {
var data = sendloop[i]
if(typeof(data) == "undefined"){
if (typeof(data) == "undefined") {
return
}
//console.log("第【" + i + "】次写数据"+data)
@ -245,12 +240,12 @@
characteristicId,
value: buffer,
success(res) {
console.log('发送成功',i)
setTimeout(()=>{
console.log('发送成功', i)
setTimeout(() => {
realWriteData(sendloop, i + 1);
},100)
}, 100)
//realWriteData(sendloop, i + 1);
},
fail(e) {
console.log('发送数据失败')
@ -258,25 +253,25 @@
}
})
}
var i = 0;
var i = 0;
realWriteData(sendloop, i);
},
senBleLabel() {
senBleLabel() {
//标签模式
uni.canvasGetImageData({
canvasId: 'firstCanvas',
x: 0,
y: 0,
width: this.canvasWidth,
height: this.canvasHeight,
success: (res)=> {
success: (res) => {
uni.hideLoading()
let deviceId = this.currDev.deviceId;
let serviceId = this.currDev.services[0].serviceId;
let characteristicId = this.currDev.services[0].characteristicId;
var command = tsc.jpPrinter.createNew()
command.init()
command.setSize(80, 60)
@ -291,27 +286,27 @@
command.setPagePrint()
console.log('command.getData()')
//console.log(command.getData())
this.senBlData(deviceId, serviceId, characteristicId,command.getData())
this.senBlData(deviceId, serviceId, characteristicId, command.getData())
},
fail: function(res) {
console.log(res)
}
})
},
senBleLabel2(){
senBleLabel2() {
//票据模式
let deviceId = this.currDev.deviceId;
let serviceId = this.currDev.services[0].serviceId;
let characteristicId = this.currDev.services[0].characteristicId;
var command = esc.jpPrinter.createNew()
command.init()
command.setText(this.piaojuText);
command.setPrintAndFeedRow(1)
this.senBlData(deviceId, serviceId, characteristicId,command.getData())
command.setText(this.piaojuText);
command.setPrintAndFeedRow(1)
this.senBlData(deviceId, serviceId, characteristicId, command.getData())
},
renderFinish() {
let filePath = '../../static/logo.png'
let filePath = 'http://oss-emis.tanex56.com/sapp/sdg/hdbg.png'
let that = this
const firstCanvas = uni.createCanvasContext('firstCanvas', this);
let scla = 1
@ -324,7 +319,7 @@
firstCanvas.drawImage(filePath, 0, 0, that.canvasWidth, that.canvasHeight);
firstCanvas.draw();
that.$nextTick(() => { //获取画布像素数据
})
},
fail(res) {
@ -338,4 +333,4 @@
<style>
</style>
</style>

View File

@ -172,11 +172,7 @@
// });
},
handleSubmit(type) {
if(type == 2) {
goto("print/bluetooth-mini-printer")
} else {
goto("print/bluetoothPrinter")
}
goto("print/bluetooth-mini-printer")
},
handleSelAll() {
if(this.submitParam.sltDanValues.length == this.yundanList.length) {