uu
This commit is contained in:
parent
99d4eb8cf7
commit
fdae6eeedf
@ -128,7 +128,7 @@ var blueToothTool = {
|
|||||||
try {
|
try {
|
||||||
activity.unregisterReceiver(btFindReceiver);
|
activity.unregisterReceiver(btFindReceiver);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
console.log("turnOffBluetooth error=========", e)
|
||||||
}
|
}
|
||||||
btFindReceiver = null;
|
btFindReceiver = null;
|
||||||
}
|
}
|
||||||
@ -181,7 +181,7 @@ var blueToothTool = {
|
|||||||
try {
|
try {
|
||||||
activity.unregisterReceiver(btFindReceiver);
|
activity.unregisterReceiver(btFindReceiver);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.log("discoveryNewDevice error=========", e)
|
||||||
}
|
}
|
||||||
btFindReceiver = null;
|
btFindReceiver = null;
|
||||||
this.cancelDiscovery();
|
this.cancelDiscovery();
|
||||||
@ -231,7 +231,7 @@ var blueToothTool = {
|
|||||||
try {
|
try {
|
||||||
activity.unregisterReceiver(btStatusReceiver);
|
activity.unregisterReceiver(btStatusReceiver);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.log("listenBluetoothStatus error=========", e)
|
||||||
}
|
}
|
||||||
btStatusReceiver = null;
|
btStatusReceiver = null;
|
||||||
}
|
}
|
||||||
@ -295,7 +295,7 @@ var blueToothTool = {
|
|||||||
let device = invoke(btAdapter, "getRemoteDevice", address);
|
let device = invoke(btAdapter, "getRemoteDevice", address);
|
||||||
btSocket = invoke(device, "createRfcommSocketToServiceRecord", MY_UUID);
|
btSocket = invoke(device, "createRfcommSocketToServiceRecord", MY_UUID);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.error("连接失败,获取Socket失败!", e);
|
||||||
shortToast("连接失败,获取Socket失败!");
|
shortToast("连接失败,获取Socket失败!");
|
||||||
callback(false)
|
callback(false)
|
||||||
return false;
|
return false;
|
||||||
@ -306,14 +306,14 @@ var blueToothTool = {
|
|||||||
this.shortToast("连接成功");
|
this.shortToast("连接成功");
|
||||||
callback(true)
|
callback(true)
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.error("connDevice error:========!", e);
|
||||||
this.shortToast("连接失败");
|
this.shortToast("连接失败");
|
||||||
callback(false)
|
callback(false)
|
||||||
try {
|
try {
|
||||||
btSocket.close();
|
btSocket.close();
|
||||||
btSocket = null;
|
btSocket = null;
|
||||||
} catch(e1) {
|
} catch(e1) {
|
||||||
console.error(e1);
|
console.error("btSocket.close error:========!", e1);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -344,7 +344,7 @@ var blueToothTool = {
|
|||||||
try {
|
try {
|
||||||
btSocket.close();
|
btSocket.close();
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.log("closeBtSocket error=========", e)
|
||||||
btSocket = null;
|
btSocket = null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -376,7 +376,7 @@ var blueToothTool = {
|
|||||||
btInStream = invoke(btSocket, "getInputStream");
|
btInStream = invoke(btSocket, "getInputStream");
|
||||||
btOutStream = invoke(btSocket, "getOutputStream");
|
btOutStream = invoke(btSocket, "getOutputStream");
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.error("readData error:========!", e);
|
||||||
this.shortToast("创建输入输出流失败!");
|
this.shortToast("创建输入输出流失败!");
|
||||||
this.closeBtSocket();
|
this.closeBtSocket();
|
||||||
return false;
|
return false;
|
||||||
@ -400,6 +400,7 @@ var blueToothTool = {
|
|||||||
try {
|
try {
|
||||||
btOutStream.write([0b00]);
|
btOutStream.write([0b00]);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
console.log("===connExceptionCallback===", e)
|
||||||
this.state.readThreadState = false;
|
this.state.readThreadState = false;
|
||||||
this.options.connExceptionCallback && this.options.connExceptionCallback(e);
|
this.options.connExceptionCallback && this.options.connExceptionCallback(e);
|
||||||
}
|
}
|
||||||
@ -433,6 +434,7 @@ var blueToothTool = {
|
|||||||
try {
|
try {
|
||||||
btOutStream.write(bytes);
|
btOutStream.write(bytes);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
console.log("sendData error=========", e)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -443,8 +445,10 @@ var blueToothTool = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
console.log("====byteData====", byteData)
|
||||||
btOutStream.write(byteData);
|
btOutStream.write(byteData);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
console.log("sendByteData error=========", e)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
242
components/tpx-bluetooth-native/tpx-bluetooth-native.vue
Normal file
242
components/tpx-bluetooth-native/tpx-bluetooth-native.vue
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
<template>
|
||||||
|
<tpx-layout>
|
||||||
|
<template v-slot:header>
|
||||||
|
<view class="blcok-white" style="margin-top: 0;">
|
||||||
|
<view >
|
||||||
|
<u-row custom-style="gap: 10rpx;">
|
||||||
|
<u-button @click="discoveryPrinter">
|
||||||
|
{{searchLoading?'搜索中...':'搜索周边设备'}}
|
||||||
|
|
||||||
|
</u-button>
|
||||||
|
<u-button @click="stopDiscoveryPrinter">停止搜索</u-button>
|
||||||
|
<slot name="button"></slot>
|
||||||
|
</u-row>
|
||||||
|
</view>
|
||||||
|
<view class="">
|
||||||
|
<view class="pt-10 pb-10">连接信息</view>
|
||||||
|
<u-steps activeColor="#969799" :current="curTab" direction="column" dot="true">
|
||||||
|
|
||||||
|
<u-steps-item v-for="(item, index) in conInfos">
|
||||||
|
<template v-slot:title>
|
||||||
|
<view @click="changeTab(index)" :class="`${index == curTab ? 'clr-prm' : 'clr-sub'} pl-20`">{{item.title}}</view>
|
||||||
|
</template>
|
||||||
|
<template v-slot:desc>
|
||||||
|
<view @click="changeTab(index)" class="mt-10 pl-20">{{item.desc}}</view>
|
||||||
|
</template>
|
||||||
|
</u-steps-item>
|
||||||
|
</u-steps>
|
||||||
|
</view>
|
||||||
|
</view>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<template v-slot:body>
|
||||||
|
<view class="blcok-white">
|
||||||
|
<view v-if="curTab == 0">
|
||||||
|
<view class="pb-10">蓝牙设备列表</view>
|
||||||
|
<template v-for="(item, index) in devices">
|
||||||
|
<view @click="handleSelectDeviceId(item)" v-if="item.name.length>0"
|
||||||
|
:key="index" :class="`list-item ${item.deviceId == deviceId?'list-item-active':''}`"
|
||||||
|
>
|
||||||
|
<u-row justify="between">
|
||||||
|
<view class="font-weight">{{item.name }}</view>
|
||||||
|
</u-row>
|
||||||
|
<view>
|
||||||
|
<text class="clr-sub">deviceId:</text>{{item.deviceId}}
|
||||||
|
</view>
|
||||||
|
</view>
|
||||||
|
</template>
|
||||||
|
</view>
|
||||||
|
|
||||||
|
<slot name="content"></slot>
|
||||||
|
</view>
|
||||||
|
</template>
|
||||||
|
</tpx-layout>
|
||||||
|
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { showLoading, showToast, hideLoading, uniPromiseApi, promiseTimeout } from "@/common/untool"
|
||||||
|
import { getSessionBluetoothInfo, setSessionBluetoothInfo } from "@/session"
|
||||||
|
import bluetoothTool from './nativeBlueTooth'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
computed: {
|
||||||
|
conInfos() {
|
||||||
|
let ary = [
|
||||||
|
{ title: '蓝牙设备', desc: this.deviceId || '-' },
|
||||||
|
]
|
||||||
|
return ary
|
||||||
|
},
|
||||||
|
},
|
||||||
|
watch: {
|
||||||
|
deviceId(val){
|
||||||
|
this.updateBlueSessionInfo()
|
||||||
|
},
|
||||||
|
},
|
||||||
|
props:{
|
||||||
|
|
||||||
|
},
|
||||||
|
data () {
|
||||||
|
let { deviceId } = getSessionBluetoothInfo()
|
||||||
|
return {
|
||||||
|
canvas_width: 240,
|
||||||
|
canvas_height: 240,
|
||||||
|
curTab: 0,
|
||||||
|
searchLoading: false,
|
||||||
|
isStateOn: false,
|
||||||
|
devices: [],
|
||||||
|
deviceId,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async mounted () {
|
||||||
|
// 初始化蓝牙模块
|
||||||
|
await this.openBluetoothAdapter()
|
||||||
|
// this.autoConnect()
|
||||||
|
},
|
||||||
|
unmounted() {
|
||||||
|
this.stopDiscoveryPrinter()
|
||||||
|
// this.closeBluetoothAdapter()
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
updateBlueSessionInfo() {
|
||||||
|
let { deviceId } = this
|
||||||
|
setSessionBluetoothInfo({ deviceId })
|
||||||
|
},
|
||||||
|
// 自动连接历史蓝牙配置
|
||||||
|
async autoConnect() {
|
||||||
|
console.log("autoConnect start===========")
|
||||||
|
let { deviceId, isStateOn } = this
|
||||||
|
let conSta = false
|
||||||
|
const _this = this
|
||||||
|
// 判断是否有历史连接
|
||||||
|
if(deviceId && isStateOn) {
|
||||||
|
conSta = await this.connect(deviceId)
|
||||||
|
}
|
||||||
|
console.log("=========当前蓝牙连接状态=======", conSta)
|
||||||
|
// 连接不成功,打开蓝牙配置页面
|
||||||
|
if(!conSta) {
|
||||||
|
this.discoveryPrinter()
|
||||||
|
}
|
||||||
|
return conSta
|
||||||
|
},
|
||||||
|
changeTab(type) {
|
||||||
|
this.curTab = type
|
||||||
|
},
|
||||||
|
async openBluetoothAdapter () {
|
||||||
|
var _this = this
|
||||||
|
const { res, err } = await uniPromiseApi('openBluetoothAdapter', { })
|
||||||
|
if(err) {
|
||||||
|
if (err.errCode == 10001) {
|
||||||
|
showToast('请打开手机蓝牙')
|
||||||
|
} else {
|
||||||
|
showToast(err.errMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let isStateOn = false
|
||||||
|
//#ifdef APP-PLUS
|
||||||
|
await new Promise((resolve)=> {
|
||||||
|
// 蓝牙
|
||||||
|
bluetoothTool.init({
|
||||||
|
listenBTStatusCallback: (state)=> {
|
||||||
|
resolve(state == 'STATE_ON')
|
||||||
|
},
|
||||||
|
discoveryDeviceCallback: _this.onDiscoverDevice.bind(_this),
|
||||||
|
discoveryFinishedCallback: function() {
|
||||||
|
_this.msg = "搜索完成";
|
||||||
|
},
|
||||||
|
readDataCallback: function(dataByteArr) {
|
||||||
|
// 读取蓝牙返回的数据
|
||||||
|
/* if(_this.receiveDataArr.length >= 200) {
|
||||||
|
_this.receiveDataArr = [];
|
||||||
|
}
|
||||||
|
_this.receiveDataArr.push.apply(_this.receiveDataArr, dataByteArr); */
|
||||||
|
},
|
||||||
|
connExceptionCallback: function(e) {
|
||||||
|
_this.msg = "设备连接失败";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
//#endif
|
||||||
|
this.isStateOn = isStateOn
|
||||||
|
},
|
||||||
|
onDiscoverDevice(item = {}){
|
||||||
|
const _this = this
|
||||||
|
let { address: deviceId, name } = item
|
||||||
|
console.log("监听寻找到新设备的事件---------------", item)
|
||||||
|
if(
|
||||||
|
name && deviceId
|
||||||
|
&& (deviceId && _this.devices.map(t=> t.deviceId).indexOf(deviceId) == -1)
|
||||||
|
) {
|
||||||
|
_this.devices.push({
|
||||||
|
name,
|
||||||
|
deviceId
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// 开始搜寻附近的蓝牙外围设备
|
||||||
|
async discoveryPrinter () {
|
||||||
|
var _this = this
|
||||||
|
_this.devices = []
|
||||||
|
_this.searchLoading = true
|
||||||
|
bluetoothTool.discoveryNewDevice();
|
||||||
|
return { res, err }
|
||||||
|
},
|
||||||
|
// 停止搜寻附近的蓝牙外围设备
|
||||||
|
async stopDiscoveryPrinter () {
|
||||||
|
this.searchLoading = false
|
||||||
|
return uniPromiseApi('stopBluetoothDevicesDiscovery')
|
||||||
|
},
|
||||||
|
// 断开蓝牙
|
||||||
|
async closeBluetoothAdapter () {
|
||||||
|
this.searchLoading = false
|
||||||
|
return uniPromiseApi('closeBluetoothAdapter')
|
||||||
|
},
|
||||||
|
async handleSelectDeviceId (item) {
|
||||||
|
this.deviceId = item.deviceId
|
||||||
|
// TODO
|
||||||
|
//连接蓝牙
|
||||||
|
let bl = await this.connect(this.deviceId)
|
||||||
|
},
|
||||||
|
async connect (deviceId) {
|
||||||
|
var _this = this
|
||||||
|
showLoading('正在连接设备')
|
||||||
|
console.log("连接蓝牙 设备=====", deviceId)
|
||||||
|
let bl = await new Promise((resolve)=> {
|
||||||
|
bluetoothTool.connDevice(deviceId,(result) => {
|
||||||
|
resolve(!!result)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
hideLoading()
|
||||||
|
showToast(bl ? '连接成功!':'连接失败...')
|
||||||
|
if(bl) {
|
||||||
|
this.stopDiscoveryPrinter()
|
||||||
|
}
|
||||||
|
return bl
|
||||||
|
},
|
||||||
|
getConData() {
|
||||||
|
return { deviceId: this.deviceId, }
|
||||||
|
},
|
||||||
|
sendByteData(data) {
|
||||||
|
let bl = bluetoothTool.sendByteData(data)
|
||||||
|
return bl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped lang="scss">
|
||||||
|
.list-item {
|
||||||
|
font-size: 26rpx;
|
||||||
|
margin-bottom: 20rpx;
|
||||||
|
border-radius: 10rpx;
|
||||||
|
border: 2rpx solid #ddd;
|
||||||
|
padding: 20rpx;
|
||||||
|
background-color: #F6F6F6;
|
||||||
|
}
|
||||||
|
.list-item-active{
|
||||||
|
border-color: #B12D29;
|
||||||
|
&:last-child{
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
193
components/tpx-bluetooth-print/render-template-native.vue
Normal file
193
components/tpx-bluetooth-print/render-template-native.vue
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
<template>
|
||||||
|
<tpx-bluetooth-native ref="curBlueTooth">
|
||||||
|
<template v-slot:button>
|
||||||
|
<u-button @click="handlePrint" type="primary">确认打印</u-button>
|
||||||
|
</template>
|
||||||
|
<template v-slot:content>
|
||||||
|
<view class="pt-20 pb-10">打印内容</view>
|
||||||
|
<view class="canvas-con">
|
||||||
|
<canvas :canvas-id="canvasId" :style="{width:canvasWidth + 'px', height: canvasHeight + 'px'}"></canvas>
|
||||||
|
</view>
|
||||||
|
</template>
|
||||||
|
</tpx-bluetooth-native>
|
||||||
|
|
||||||
|
<tpx-dialog type="type-dialog" title="打印中" :show="percentage">
|
||||||
|
<u-row custom-style="width: 500rpx;min-height: 150rpx;padding: 10rpx;">
|
||||||
|
<view>
|
||||||
|
<u-line-progress :percentage="percentage" activeColor="#B12D29" height="30"></u-line-progress>
|
||||||
|
<view class="pt-20">正在打印, 请勿退出页面, 如果卡住了请重连打印机重试</view>
|
||||||
|
</view>
|
||||||
|
</u-row>
|
||||||
|
</tpx-dialog>
|
||||||
|
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { showLoading, showToast, hideLoading, canvasGetImageData, getImageInfo, promiseTimeout } from "@/common/untool"
|
||||||
|
import * as canvasUtil from "@/common/canvasUtil"
|
||||||
|
import * as printJob from './print/printJob'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
props:{
|
||||||
|
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
|
||||||
|
},
|
||||||
|
data () {
|
||||||
|
return {
|
||||||
|
canvasId: uni.$u.guid(20),
|
||||||
|
canvasWidth: 1,
|
||||||
|
canvasHeight: 1,
|
||||||
|
imgData: '',
|
||||||
|
percentage: 0, // 打印进度
|
||||||
|
imageUrl: '',
|
||||||
|
paperWidth: 320,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
mounted () {
|
||||||
|
this.initData()
|
||||||
|
},
|
||||||
|
unmounted() {
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
async initData() {
|
||||||
|
await this.renderCanvasImg(this.imageUrl);
|
||||||
|
},
|
||||||
|
// 打印
|
||||||
|
handlePrint(){
|
||||||
|
const that = this
|
||||||
|
return new Promise(async (resolve, reject)=> {
|
||||||
|
let { bl } = that.checkPrintParam()
|
||||||
|
if(!bl) {
|
||||||
|
return reject('打印参数校验不通过')
|
||||||
|
}
|
||||||
|
|
||||||
|
// let sendBl = await that.$refs.curBlueTooth.sendByteData(sendData)
|
||||||
|
const opt = {
|
||||||
|
onProgress: (percentage) => {
|
||||||
|
console.log("打印进度====", percentage)
|
||||||
|
that.percentage = percentage
|
||||||
|
},
|
||||||
|
lasterSuccess: () => {
|
||||||
|
that.percentage = 0
|
||||||
|
hideLoading()
|
||||||
|
uni.showModal({
|
||||||
|
title: '提示',
|
||||||
|
content: '数据已发送完,请检查打印的内容是否正常',
|
||||||
|
showCancel: false,
|
||||||
|
confirmText: '好的',
|
||||||
|
});
|
||||||
|
resolve()
|
||||||
|
},
|
||||||
|
onError: (e)=> {
|
||||||
|
that.percentage = 0
|
||||||
|
showToast(`打印异常`)
|
||||||
|
hideLoading()
|
||||||
|
reject(e)
|
||||||
|
},
|
||||||
|
customerSend: (val)=> {
|
||||||
|
that.$refs.curBlueTooth.sendByteData(val)
|
||||||
|
},
|
||||||
|
lineByLine: false
|
||||||
|
}
|
||||||
|
//打印图片
|
||||||
|
printJob.printImage(
|
||||||
|
opt,
|
||||||
|
// 将图片数据转成位图数据
|
||||||
|
printJob.overwriteImageData({
|
||||||
|
imageData: that.imgData,
|
||||||
|
width: that.canvasWidth,
|
||||||
|
height: that.canvasHeight,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
renderCanvasImg(filePath) {
|
||||||
|
let that = this
|
||||||
|
showLoading()
|
||||||
|
console.log("===renderCanvasImg:filePath=====", filePath)
|
||||||
|
return new Promise(async (resolve, reject)=> {
|
||||||
|
try{
|
||||||
|
// 多次渲染,createCanvasContext,必须使用新的才可以
|
||||||
|
that.canvasId = uni.$u.guid(20)
|
||||||
|
await promiseTimeout(500)
|
||||||
|
let res = await getImageInfo(filePath)
|
||||||
|
console.log("getImageInfo res====", res)
|
||||||
|
const ctx = uni.createCanvasContext(that.canvasId, that);
|
||||||
|
// 打印宽度须是8的整数倍,这里处理掉多余的,使得宽度合适,不然有可能乱码
|
||||||
|
const remainder = that.paperWidth % 8;
|
||||||
|
const w = remainder === 0 ? that.paperWidth : that.paperWidth - remainder;
|
||||||
|
// 等比算出图片的高度
|
||||||
|
const h = Math.floor((res.height * w) / res.width);
|
||||||
|
that.canvasWidth = w
|
||||||
|
that.canvasHeight = h
|
||||||
|
// 在canvas 画一张图片
|
||||||
|
let imgRect = canvasUtil.containImg(0, 0, w, h, res.width, res.height)
|
||||||
|
let ctData = [imgRect.dx, imgRect.dy, imgRect.dWidth, imgRect.dHeight]
|
||||||
|
ctx.fillStyle = 'rgba(255,255,255,1)';
|
||||||
|
ctx.clearRect(...ctData);
|
||||||
|
ctx.fillRect(...ctData);
|
||||||
|
// 把原图缩放到 打印宽度的比例 绘制
|
||||||
|
ctx.drawImage(filePath, ...ctData);
|
||||||
|
|
||||||
|
ctx.draw(false, async() => {
|
||||||
|
await promiseTimeout(1000)
|
||||||
|
let { data } = await canvasGetImageData({ canvasId: that.canvasId, width: w, height: h, })
|
||||||
|
hideLoading()
|
||||||
|
that.imgData = data
|
||||||
|
resolve(data)
|
||||||
|
});
|
||||||
|
}catch(e){
|
||||||
|
hideLoading()
|
||||||
|
reject(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
checkPrintParam() {
|
||||||
|
let msg = '', bl = false
|
||||||
|
let { deviceId } = this.$refs.curBlueTooth.getConData()
|
||||||
|
!this.imgData && (msg = '打印内容不能为空,请重试')
|
||||||
|
if(!msg && !(deviceId)) {
|
||||||
|
msg = '请选选择打印设备'
|
||||||
|
}
|
||||||
|
msg && (showToast(msg))
|
||||||
|
return {
|
||||||
|
msg,
|
||||||
|
bl: !msg
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// 给外部调用api,打印自定义图
|
||||||
|
async callPrint({ paperWidth, imageUrl } = {}) {
|
||||||
|
try{
|
||||||
|
this.paperWidth = paperWidth
|
||||||
|
if(this.imageUrl != imageUrl || !this.imgData) {
|
||||||
|
this.imageUrl = imageUrl
|
||||||
|
await this.renderCanvasImg(this.imageUrl);
|
||||||
|
}
|
||||||
|
let conSta = await this.$refs.curBlueTooth.autoConnect()
|
||||||
|
if(conSta) {
|
||||||
|
await this.handlePrint()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} catch(e){
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped lang="scss">
|
||||||
|
.canvas-con{
|
||||||
|
width: 100%;
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
|
/*图片隐藏到窗口外*/
|
||||||
|
canvas {
|
||||||
|
// position: absolute;
|
||||||
|
// z-index: -999;
|
||||||
|
// top: -999px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@ -1,359 +0,0 @@
|
|||||||
<template>
|
|
||||||
<tpx-layout v-if="showConnectPage">
|
|
||||||
<template v-slot:header>
|
|
||||||
<view class="blcok-white" style="margin-top: 0;">
|
|
||||||
<view >
|
|
||||||
<u-row custom-style="gap: 10rpx;">
|
|
||||||
<u-button @click="discoveryPrinter">
|
|
||||||
{{searchLoading?'搜索中...':'搜索周边设备'}}
|
|
||||||
|
|
||||||
</u-button>
|
|
||||||
<u-button @click="stopDiscoveryPrinter">停止搜索</u-button>
|
|
||||||
<slot name="button"></slot>
|
|
||||||
</u-row>
|
|
||||||
</view>
|
|
||||||
<view class="">
|
|
||||||
<view class="pt-10 pb-10">连接信息</view>
|
|
||||||
<u-steps activeColor="#969799" :current="curTab" direction="column" dot="true">
|
|
||||||
|
|
||||||
<u-steps-item v-for="(item, index) in conInfos">
|
|
||||||
<template v-slot:title>
|
|
||||||
<view @click="changeTab(index)" :class="`${index == curTab ? 'clr-prm' : 'clr-sub'} pl-20`">{{item.title}}</view>
|
|
||||||
</template>
|
|
||||||
<template v-slot:desc>
|
|
||||||
<view @click="changeTab(index)" class="mt-10 pl-20">{{item.desc}}</view>
|
|
||||||
</template>
|
|
||||||
</u-steps-item>
|
|
||||||
</u-steps>
|
|
||||||
</view>
|
|
||||||
</view>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<template v-slot:body>
|
|
||||||
<view class="blcok-white">
|
|
||||||
<view v-if="curTab == 0">
|
|
||||||
<view class="pb-10">蓝牙设备列表</view>
|
|
||||||
<template v-for="(item, index) in devices">
|
|
||||||
<view @click="handleAutoSelectAllData(item)" v-if="item.name.length>0"
|
|
||||||
:key="index" :class="`list-item ${item.deviceId == deviceId?'list-item-active':''}`"
|
|
||||||
>
|
|
||||||
<u-row justify="between">
|
|
||||||
<view class="font-weight">{{item.name }}</view>
|
|
||||||
</u-row>
|
|
||||||
<view>
|
|
||||||
<text class="clr-sub">信号强度:</text> {{item.RSSI}}dBm ({{Math.max(100+item.RSSI,0)}}%)
|
|
||||||
</view>
|
|
||||||
<view>
|
|
||||||
<text class="clr-sub">deviceId:</text>{{item.deviceId}}
|
|
||||||
</view>
|
|
||||||
<view >
|
|
||||||
<text class="clr-sub">Service数量:</text> {{item.advertisServiceUUIDs.length || 0}}
|
|
||||||
</view>
|
|
||||||
</view>
|
|
||||||
</template>
|
|
||||||
</view>
|
|
||||||
|
|
||||||
<view v-if="curTab == 1">
|
|
||||||
<view class="pb-10">服务列表</view>
|
|
||||||
<template v-for="(item, index) in serverList" :key="index">
|
|
||||||
<view @click="handleSelectService(item)"
|
|
||||||
:class="`list-item ${item.uuid == serviceId?'list-item-active':''}`"
|
|
||||||
>
|
|
||||||
<view>
|
|
||||||
<text class="clr-sub">uuid:</text> {{item.uuid}}
|
|
||||||
</view>
|
|
||||||
</view>
|
|
||||||
</template>
|
|
||||||
</view>
|
|
||||||
|
|
||||||
<view v-if="curTab == 2">
|
|
||||||
<view class="pb-10">特征值列表</view>
|
|
||||||
<template v-for="(item, index) in characteristics" :key="index">
|
|
||||||
<view @click="handleSelectChara(item)"
|
|
||||||
:class="`list-item ${item.uuid == characteristicId?'list-item-active':''}`"
|
|
||||||
>
|
|
||||||
<view>
|
|
||||||
<text class="clr-sub">uuid:</text> {{item.uuid}}
|
|
||||||
</view>
|
|
||||||
|
|
||||||
<template v-if="item.properties">
|
|
||||||
<view>
|
|
||||||
<text class="clr-sub">write:</text> {{item.properties.write}}
|
|
||||||
</view>
|
|
||||||
<view>
|
|
||||||
<text class="clr-sub">notify:</text> {{item.properties.notify}}
|
|
||||||
</view>
|
|
||||||
<view>
|
|
||||||
<text class="clr-sub">indicate:</text> {{item.properties.indicate}}
|
|
||||||
</view>
|
|
||||||
</template>
|
|
||||||
</view>
|
|
||||||
</template>
|
|
||||||
</view>
|
|
||||||
<slot name="content"></slot>
|
|
||||||
</view>
|
|
||||||
</template>
|
|
||||||
</tpx-layout>
|
|
||||||
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import { showLoading, showToast, hideLoading, uniPromiseApi, promiseTimeout } from "@/common/untool"
|
|
||||||
import { getSessionBluetoothInfo, setSessionBluetoothInfo } from "@/session"
|
|
||||||
|
|
||||||
export default {
|
|
||||||
computed: {
|
|
||||||
conInfos() {
|
|
||||||
let ary = [
|
|
||||||
{ title: '蓝牙设备', desc: this.deviceId || '-' },
|
|
||||||
{ title: '服务', desc: this.serviceId || '-' },
|
|
||||||
{ title: '特征值', desc: this.characteristicId || '-' }
|
|
||||||
]
|
|
||||||
return ary
|
|
||||||
},
|
|
||||||
},
|
|
||||||
watch: {
|
|
||||||
deviceId(val){
|
|
||||||
this.updateBlueSessionInfo()
|
|
||||||
},
|
|
||||||
serviceId(val){
|
|
||||||
this.updateBlueSessionInfo()
|
|
||||||
},
|
|
||||||
characteristicId(val){
|
|
||||||
this.updateBlueSessionInfo()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
props:{
|
|
||||||
|
|
||||||
},
|
|
||||||
data () {
|
|
||||||
let { deviceId, serviceId, characteristicId } = getSessionBluetoothInfo()
|
|
||||||
return {
|
|
||||||
showConnectPage: true,
|
|
||||||
canvasId: 'shareCanvas',
|
|
||||||
canvas_width: 240,
|
|
||||||
canvas_height: 240,
|
|
||||||
curTab: 0,
|
|
||||||
searchLoading: false,
|
|
||||||
devices: [],
|
|
||||||
deviceId,
|
|
||||||
serverList: [],
|
|
||||||
serviceId,
|
|
||||||
characteristics: [],
|
|
||||||
characteristicId,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async mounted () {
|
|
||||||
// 初始化蓝牙模块
|
|
||||||
await this.openBluetoothAdapter()
|
|
||||||
// this.autoConnect()
|
|
||||||
},
|
|
||||||
unmounted() {
|
|
||||||
this.stopDiscoveryPrinter()
|
|
||||||
// this.closeBluetoothAdapter()
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
updateBlueSessionInfo() {
|
|
||||||
let { deviceId, serviceId, characteristicId } = this
|
|
||||||
setSessionBluetoothInfo({ deviceId, serviceId, characteristicId })
|
|
||||||
},
|
|
||||||
// 自动连接历史蓝牙配置
|
|
||||||
async autoConnect() {
|
|
||||||
console.log("autoConnect start===========")
|
|
||||||
let { characteristicId, serviceId, deviceId } = this
|
|
||||||
let conSta = false
|
|
||||||
const _this = this
|
|
||||||
// 判断是否有历史连接
|
|
||||||
if(deviceId) {
|
|
||||||
await this.handleAutoSelectAllData({ deviceId })
|
|
||||||
conSta = !!_this.characteristicId // 判断是否有选中 characteristicId
|
|
||||||
}
|
|
||||||
console.log("=========当前蓝牙连接状态=======", conSta)
|
|
||||||
// 连接不成功,打开蓝牙配置页面
|
|
||||||
if(!conSta) {
|
|
||||||
this.showConnectPage = true
|
|
||||||
// this.discoveryPrinter()
|
|
||||||
} else {
|
|
||||||
this.stopDiscoveryPrinter()
|
|
||||||
}
|
|
||||||
return conSta
|
|
||||||
},
|
|
||||||
changeTab(type) {
|
|
||||||
this.curTab = type
|
|
||||||
},
|
|
||||||
async openBluetoothAdapter () {
|
|
||||||
var _this = this
|
|
||||||
const { res, err } = await uniPromiseApi('openBluetoothAdapter', { })
|
|
||||||
if(err) {
|
|
||||||
if (err.errCode == 10001) {
|
|
||||||
showToast('请打开手机蓝牙')
|
|
||||||
} else {
|
|
||||||
showToast(err.errMsg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// 开始搜寻附近的蓝牙外围设备
|
|
||||||
async discoveryPrinter () {
|
|
||||||
var _this = this
|
|
||||||
_this.devices = []
|
|
||||||
_this.searchLoading = true
|
|
||||||
_this.curTab = 0
|
|
||||||
const { res, err } = await uniPromiseApi('startBluetoothDevicesDiscovery', { })
|
|
||||||
if(res) {
|
|
||||||
// 打开搜索后,监听设备回调信息
|
|
||||||
uni.onBluetoothDeviceFound(devices => {
|
|
||||||
console.log("========devices=====", devices)
|
|
||||||
let curDvs = devices?.devices?.[0] || {}
|
|
||||||
// 过滤已经搜到的设备
|
|
||||||
if(curDvs.deviceId && _this.devices.map(t=> t.deviceId).indexOf(curDvs.deviceId) == -1) {
|
|
||||||
_this.devices.push(curDvs)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return { res, err }
|
|
||||||
},
|
|
||||||
// 停止搜寻附近的蓝牙外围设备
|
|
||||||
async stopDiscoveryPrinter () {
|
|
||||||
this.searchLoading = false
|
|
||||||
return uniPromiseApi('stopBluetoothDevicesDiscovery')
|
|
||||||
},
|
|
||||||
// 断开蓝牙
|
|
||||||
async closeBluetoothAdapter () {
|
|
||||||
this.searchLoading = false
|
|
||||||
return uniPromiseApi('closeBluetoothAdapter')
|
|
||||||
},
|
|
||||||
async handleSelectDeviceId (item) {
|
|
||||||
this.deviceId = item.deviceId
|
|
||||||
this.serviceId = ''
|
|
||||||
this.serverList = []
|
|
||||||
this.characteristics = []
|
|
||||||
this.characteristicId = ''
|
|
||||||
//连接蓝牙
|
|
||||||
await this.connect()
|
|
||||||
if(this.serverList?.length > 0) {
|
|
||||||
this.curTab = 1
|
|
||||||
this.stopDiscoveryPrinter()
|
|
||||||
} else {
|
|
||||||
showToast('该设备无服务,请检查配置或换一个设备连接')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
handleSelectService (item) {
|
|
||||||
this.serviceId = item.uuid
|
|
||||||
this.characteristics = []
|
|
||||||
this.characteristicId = ''
|
|
||||||
// 获取蓝牙特征值
|
|
||||||
setTimeout(async () =>{
|
|
||||||
await this.getBLEDeviceCharacteristics()
|
|
||||||
if(this.characteristics?.length > 0) {
|
|
||||||
this.curTab = 2
|
|
||||||
} else {
|
|
||||||
showToast('该服务无特征值,请检查配置或换一个服务')
|
|
||||||
}
|
|
||||||
}, 500);
|
|
||||||
},
|
|
||||||
handleSelectChara (item) {
|
|
||||||
this.characteristicId = item.uuid
|
|
||||||
},
|
|
||||||
async connect () {
|
|
||||||
var _this = this
|
|
||||||
console.log("连接蓝牙=====deviceId", _this.deviceId)
|
|
||||||
showLoading('正在连接蓝牙')
|
|
||||||
let { res, err } = await uniPromiseApi('createBLEConnection', { deviceId: _this.deviceId })
|
|
||||||
hideLoading()
|
|
||||||
if(res || err?.code == -1) {
|
|
||||||
// code: -1 已连接
|
|
||||||
res = res || err
|
|
||||||
err = undefined
|
|
||||||
}
|
|
||||||
if(res) {
|
|
||||||
showToast(`蓝牙连接成功`)
|
|
||||||
await promiseTimeout(1500)
|
|
||||||
// 获取蓝牙设备所有服务(service)。
|
|
||||||
await _this.getBLEDeviceServices()
|
|
||||||
}
|
|
||||||
if(err) {
|
|
||||||
showToast(`连接设备失败:${err.errMsg}`)
|
|
||||||
}
|
|
||||||
return { res, err }
|
|
||||||
},
|
|
||||||
async getBLEDeviceServices() {
|
|
||||||
var _this = this
|
|
||||||
let { res, err } = await uniPromiseApi('getBLEDeviceServices', {
|
|
||||||
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
|
||||||
deviceId: _this.deviceId,
|
|
||||||
})
|
|
||||||
if(res) {
|
|
||||||
_this.serverList = res.services
|
|
||||||
console.log('serverId:', _this.serviceId)
|
|
||||||
}
|
|
||||||
if(err) {
|
|
||||||
this.serverList = []
|
|
||||||
showToast(`设备服务获取失败:${err.errMsg}`)
|
|
||||||
}
|
|
||||||
return { res, err }
|
|
||||||
},
|
|
||||||
async getBLEDeviceCharacteristics () {
|
|
||||||
var _this = this
|
|
||||||
let { res, err } = await uniPromiseApi('getBLEDeviceCharacteristics', {
|
|
||||||
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
|
||||||
deviceId: _this.deviceId,
|
|
||||||
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
|
|
||||||
serviceId:_this.serviceId,
|
|
||||||
})
|
|
||||||
if(res) {
|
|
||||||
_this.characteristics = res.characteristics
|
|
||||||
}
|
|
||||||
return { res, err }
|
|
||||||
},
|
|
||||||
// 根据选择的deviceId自动匹配 serviceId, characteristicId
|
|
||||||
async handleAutoSelectAllData(item) {
|
|
||||||
console.log("handleAutoSelectAllData :item======", item)
|
|
||||||
const _this = this
|
|
||||||
await this.handleSelectDeviceId(item)
|
|
||||||
|
|
||||||
if(this.serverList?.length > 0) {
|
|
||||||
for(let i=0; i<_this.serverList?.length; i++) {
|
|
||||||
let _serviceId = _this.serverList[i].uuid
|
|
||||||
let charPms = await uniPromiseApi('getBLEDeviceCharacteristics', {
|
|
||||||
deviceId: _this.deviceId,
|
|
||||||
serviceId: _serviceId,
|
|
||||||
})
|
|
||||||
let { res, err } = charPms
|
|
||||||
if(res) {
|
|
||||||
let charItem = res.characteristics.filter(t=> {
|
|
||||||
let { notify, indicate, write } = t.properties || {}
|
|
||||||
// return write
|
|
||||||
return (notify || indicate) && write
|
|
||||||
})[0]
|
|
||||||
if(charItem) {
|
|
||||||
_this.serviceId = _serviceId
|
|
||||||
_this.characteristicId = charItem.uuid
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
getConData() {
|
|
||||||
return { deviceId: this.deviceId, serviceId: this.serviceId, characteristicId: this.characteristicId }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped lang="scss">
|
|
||||||
.list-item {
|
|
||||||
font-size: 26rpx;
|
|
||||||
margin-bottom: 20rpx;
|
|
||||||
border-radius: 10rpx;
|
|
||||||
border: 2rpx solid #ddd;
|
|
||||||
padding: 20rpx;
|
|
||||||
background-color: #F6F6F6;
|
|
||||||
}
|
|
||||||
.list-item-active{
|
|
||||||
border-color: #B12D29;
|
|
||||||
&:last-child{
|
|
||||||
margin-bottom: 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
Loading…
Reference in New Issue
Block a user