From faee62f47523b2e1c0d1eeed34e047be8ca0c99d Mon Sep 17 00:00:00 2001
From: chenhaozhe <cgz@hz-kingdee.com>
Date: 星期三, 09 七月 2025 16:57:01 +0800
Subject: [PATCH] 完善蓝牙连接组件 实现蓝牙状态全局可查询
---
components/blueToothConnector/blueToothConnector.vue | 148 +++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 142 insertions(+), 6 deletions(-)
diff --git a/components/blueToothConnector/blueToothConnector.vue b/components/blueToothConnector/blueToothConnector.vue
index 9b86d0f..2b9aba0 100644
--- a/components/blueToothConnector/blueToothConnector.vue
+++ b/components/blueToothConnector/blueToothConnector.vue
@@ -2,7 +2,15 @@
<view>
<!-- 钃濈墮杩炴帴鐘舵�� MAC鍦板潃 -->
<view class="connection-status">
- <view>杩炴帴鐘舵�侊細</view>
+ <view style="display: flex; flex-direction: row; gap: 20rpx;padding: 20rpx;">
+ <view style="width: 5rem;">杩炴帴璁惧: </view>
+ <view v-if="$printer.isConnected() === false">鏃�</view>
+ <view v-else="$printer.isConnected() === true"
+ style="flex: 1;display: inline-flex;flex-direction: row;justify-content: space-between;">
+ <view>{{ $printer.connectedDevice().name }}</view>
+ <view>{{ $printer.connectedDevice().address }}</view>
+ </view>
+ </view>
</view>
<!-- 鎿嶄綔鍖� -->
<view class="buttons">
@@ -34,12 +42,13 @@
FakeConnectedDevice,
WriteOptions,
} from '@psdk/frame-father';
+import { getAddress } from '../../utils/auth';
export default {
name: "blueToothConnector",
data() {
return {
discoveredDevices: [], // 鏌ヨ鍒扮殑璁惧
- connectedDeviceId: ""
+ connectedDeviceId: "",
};
},
methods: {
@@ -86,6 +95,7 @@
}
}
})
+
},
onDevice(device) {
console.log("鐩戝惉瀵绘壘鍒版柊璁惧鐨勪簨浠�---------------")
@@ -108,7 +118,8 @@
title: '杩炴帴涓�'
});
bluetoothTool.connDevice(device.address, (result) => {
- console.log(result)
+ console.log("璁惧MAC鍦板潃: ", device.address)
+ console.log("printer锛�",vm.$printer._connectedDevice)
uni.hideLoading()
if (result) {
// // console.log(result);
@@ -116,10 +127,14 @@
// console.log(vm.$printer)
vm.$printer.init(new FakeConnectedDevice());
vm.connectedDeviceId = device.address;
+ vm.$printer._connectedDevice.address = device.address
+ vm.$printer._connectedDevice.name = device.name
+ console.log("printer锛�",vm.$printer._connectedDevice)
uni.showToast({
icon: 'none',
title: '杩炴帴鎴愬姛'
})
+
} else {
uni.showToast({
icon: 'none',
@@ -138,12 +153,132 @@
if (vm.connectedDeviceId != '') {
bluetoothTool.closeBtSocket();
vm.connectedDeviceId = "";
+ vm.$printer._connectedDevice = null
+
}
+ },
+ labelPrint() {
+ // 鎵撳嵃
+ // 妫�鏌ヨ摑鐗欒繛鎺�
+ let btStatus = bluetoothTool.getBluetoothStatus()
+ if (btStatus != true) {
+ this.showToast("钃濈墮杩炴帴寮傚父!")
+ return
+ }
+
+ // 妫�鏌ユ槸鍚﹁繛鎺ヨ澶�
+ let pairedDevices = bluetoothTool.getPairedDevices()
+ if (pairedDevices.length < 1) {
+ this.showToast("鏃犺澶囪繛鎺�!")
+ return
+ }
+
+ // 妫�鏌ヨ〃鍗曢」鏄惁鏈夌┖鍊�
+ for (var key in this.hform) {
+ if (this.hform[key] == "") {
+ // todo 鎻愮ず琛ㄥ崟椤逛笉鑳戒负绌�
+ this.showToast("琛ㄥ崟涓嶈兘鏈夌┖鍊�!")
+ return
+ }
+ }
+ this.printWrite()
+
+
+ },
+ showToast(msg, status = "none") {
+ uni.showToast({
+ title: msg,
+ icon: status,
+ duration: 2000
+ });
+ },
+ ///杞垚瀹夊崜鏈夌鍙风殑
+ uint8ArrayToSignedArray(uint8Array) {
+ let signedArray = new Array(uint8Array.length);
+ for (let i = 0; i < uint8Array.length; i++) {
+ if (uint8Array[i] >= 128) {
+ signedArray[i] = uint8Array[i] - 256;
+ } else {
+ signedArray[i] = uint8Array[i];
+ }
+ }
+ return signedArray;
+ },
+ async printWrite(type = "cpcl") {
+ const vm = this;
+ console.log("寮�濮嬫墦鍗�------------------")
+ switch (type) {
+ case "cpcl":
+ await vm.writeCpclModel();
+ break;
+ }
+ },
+ async writeCpclModel() {
+ const vm = this;
+ try {
+ const cpcl = await vm.$printer.cpcl().clear()
+ .page(new CPage({
+ width: 608,
+ height: 260
+ }))
+ .text(new CText({
+ x: 54,
+ y: 40,
+ content: "鐗╂枡浠g爜: " + vm.hform.HMaterCode,
+ font: CFont.TSS24
+ }))
+ .text(new CText({
+ x: 54,
+ y: 80,
+ content: "鍚� 绉�: " + vm.hform.HMaterName,
+ font: CFont.TSS24
+ }))
+ .text(new CText({
+ x: 54,
+ y: 120,
+ content: "瑙勬牸鍨嬪彿: " + vm.hform.HMaterType,
+ font: CFont.TSS24
+ }))
+ .text(new CText({
+ x: 54,
+ y: 160,
+ content: "鏁� 閲�: " + vm.hform.HNumber,
+ font: CFont.TSS24
+ }))
+ .bar(new CBar({
+ x: 120,
+ y: 200,
+ lineWidth: 1,
+ height: 80,
+ content: vm.hform.HBarCode,
+ codeRotation: CCodeRotation.ROTATION_0,
+ codeType: CCodeType.CODE128
+ }))
+ .form(new CForm()) //鏍囩绾搁渶瑕佸姞瀹氫綅鎸囦护
+ .print();
+ console.log(cpcl.command().string());
+ var binary = cpcl.command().binary();
+ await this.sendMessage(Array.from(this.uint8ArrayToSignedArray(binary)));
+ } catch (e) {
+ console.error(e);
+ uni.showToast({
+ title: '澶辫触',
+ });
+ }
+ },
+ async sendMessage(cmd) {
+ console.log(cmd);
+ const result = bluetoothTool.sendByteData(cmd);
+ uni.showToast({
+ icon: 'none',
+ title: result ? '鍙戦�佹垚鍔燂紒' : '鍙戦�佸け璐�...'
+ })
},
},
async mounted() {
//#ifdef APP-PLUS
// 钃濈墮
+ console.log("钃濈墮鍒濆鍖�")
bluetoothTool.init({
listenBTStatusCallback: (state) => {
if (state == 'STATE_ON') {
@@ -166,9 +301,10 @@
}
});
- // 鏌ョ湅杩炴帴璁惧
- let pariedDevices = bluetoothTool.getPairedDevices()
- console("宸查厤瀵硅澶�", pariedDevices)
+ console.log(this.$printer)
+ if(this.$printer.isConnected()){
+ this.connectedDeviceId = this.$printer.connectedDevice().address
+ }
//#endif
},
}
--
Gitblit v1.9.1