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

View File

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