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>
<!-- <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" />
<canvas :style="{width: canvasWidth +'px', height: canvasHeight +'px'}" canvas-id="firstCanvas" id="firstCanvas"
class="firstCanvas" />
</view>
@ -17,6 +18,11 @@
<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() {
@ -73,31 +79,30 @@
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() {
console.log("监听寻找到新设备的事件---------------")
var that = this
//监听寻找到新设备的事件
uni.onBluetoothDeviceFound(function(devices) {
console.log('--------------new-----------------------'+JSON.stringify(devices))
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
@ -109,17 +114,10 @@
services: []
})
}
})
},
stopFindBule() {
console.log("停止搜寻附近的蓝牙外围设备---------------")
console.log("stopFindBule: 停止搜寻附近的蓝牙外围设备---------------")
uni.stopBluetoothDevicesDiscovery({
success(res) {
console.log(res)
@ -134,21 +132,19 @@
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)
} else {
console.log(res)
}
//连接成功 关闭搜索
that.stopFindBule()
} else {
showToast("连接蓝牙失败:", res.errMsg)
}
},
})
},
getBLEServices(_deviceId) {
@ -156,13 +152,13 @@
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,29 +187,26 @@
})
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+ "]")
console.log('************deviceId = [' + deviceId + '] serviceId = [' + serviceId +
'] characteristics=[' + characteristicId + "]")
var uint8Buf = Array.from(uint8Array);
function split_array(datas, size) {
var result = {};
var j = 0
@ -311,7 +306,7 @@
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

View File

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