uni-app攻略:如何對接馳騰打印機

一.引言
在當前的移動開發生態中,跨平臺框架如uni-app因其高效、靈活的特點受到了開發者們的青睞。同時,隨著物聯網技術的飛速發展,智能打印設備已成為許多業務場景中不可或缺的一環。今天,我們就來探討如何使用uni-app輕松對接馳騰品牌的智能打印機,實現無線打印功能。無論您是初學者還是有經驗的開發者,本教程都將帶您一步步實現這一目標。
二.準備工作
首先確保您的開發環境已就緒。這包括安裝HBuilderX和uni-app框架。同時,您需要準備一臺馳騰打印機,并熟悉其用戶手冊和API文檔。了解打印機支持的通信協議(比如藍牙或Wi-Fi)也至關重要。
三.對接流程解析
在進行代碼編寫之前,我們需要理解整個接口調用的流程。這通常包括建立與打印機的連接、發送打印指令以及處理返回結果。此外,我們還需要關注數據格式、編碼要求以及安全機制。
四.詳細步驟與實施
1.設備連接與通訊建立
藍牙連接流程
- 使用uni-app提供的藍牙模塊初始化并搜索打印機設備。
- 配對并連接到馳騰打印機。
2.發送打印指令
- 數據封裝與傳輸
- 依據馳騰打印機的API文檔,正確封裝打印數據。
- 調用相關API發送打印任務。
- 錯誤處理與反饋
- 監聽打印狀態變化,及時響應可能出現的錯誤。
- 向用戶提供清晰的狀態反饋信息。
3.打印狀態展示
- 實時顯示當前打印任務的狀態,包括成功、等待和失敗等。
五.代碼實例與詳解
前期準備:
需要下載一個js文件支持包,請先點擊下載
點擊下載js支持包
點擊下載開發指南
開發說明中有詳細的指令文檔,需要的大家可以自行翻閱

以下提供一個使用打印機進行二維碼打印的經典案例
1.先將js包解壓,并在項目中創建文件夾保存

2.現在需要兩個頁面,一個負責藍牙搜索和連接,一個復制連接后的打印工作
測試藍牙連接頁面代碼:
<template>
<view class="container">
<view class="top-box">
<view class="name">打印機搜索</view>
<view class="value" @click="onLoadFun" v-if="submitMain">
點擊搜索
</view>
<!-- <view class="value" @click="rescan" v-else>
重新搜索
</view> -->
</view>
<scroll-view scroll-y class="box">
<view
class="item"
v-for="(item, index) in blueDeviceList"
:key="index"
@click="connect(item, index)"
:class="{ active: blueIndex === index }"
>
<view>
<text>{{ item.name }}</text>
</view>
<view>
<text>{{ item.deviceId }}</text>
</view>
</view>
<!-- <view class="item">{{code}}</view> -->
</scroll-view>
</view>
</template>
<script>
import CTPL from "@/web-CTPL-SDK/ctpl.js";
export default {
data() {
return {
blueDeviceList: [], //藍牙設備列表
blueName: "", //藍牙設備名稱
blueDeviceId: "", //藍牙設備特征值
blueIndex: -1,
submitMain:true
};
},
onUnload() {
if(this.blueDeviceId){
CTPL.disconnect();
}
},
methods: {
async onLoadFun(){
await CTPL.init();
this.submitMain = false;
await this.discoveryList()
},
clickLeft() {
uni.navigateBack();
},
async discoveryList() {
uni.showLoading({
title:'搜索設備中'
});
CTPL.discovery().then((res)=>{
uni.hideLoading();
this.blueDeviceList = res;
}).catch((err)=>{
uni.hideLoading();
uni.$u.toast(err)
})
},
//重新掃描
rescan() {
this.blueDeviceList = [];
this.discoveryList();
},
//開始連接藍牙
connect(data, index) {
const that = this;
uni.showModal({
title:'溫馨提示',
content:'是否使用選中設備進行打印',
success(res) {
if(res.confirm){
CTPL.connect(data.deviceId);
that.blueIndex = index;
that.blueDeviceId = data.deviceId;
that.blueName = data.name;
setTimeout(() => {
uni.showLoading({
title:'配置設備中'
})
that.setCodeFun()
}, 1000);
}
}
})
},
setCodeFun(){
const that = this;
CTPL.setPaperType(0);
setTimeout(()=>{
CTPL.setMemoryPrint(0);
uni.hideLoading()
setTimeout(()=>{
uni.navigateTo({
url: `要進行打印的頁面?id=${that.orderId}&deviceId=${that.blueDeviceId}`,
});
},500)
},500)
},
},
};
</script>
<style lang="scss" scoped>
.container {
width: 100%;
overflow: hidden;
min-height: 100vh;
}
.top-box{
width: 100%;
padding: 30rpx;
background-color: white;
color: #000000;
line-height: 70rpx;
font-size: 32rpx;
overflow: hidden;
.name{
width: 50%;
display: inline-block;
vertical-align: top;
}
.value{
width: 30%;
float: right;
display: inline-block;
vertical-align: top;
background:#009180;
color: white;
text-align: center;
border-radius: 10rpx;
}
}
$nav-height: 30px;
.box-bg {
background-color: #f5f5f5;
.nav {
text {
font-size: 28rpx !important;
}
.uni-nav-bar-right-text {
color: #1aad19 !important;
}
}
}
.city {
/* #ifndef APP-PLUS-NVUE */
display: flex;
/* #endif */
flex-direction: row;
align-items: center;
justify-content: flex-start;
// width: 160rpx;
margin-left: 4px;
}
.input-view {
/* #ifndef APP-PLUS-NVUE */
display: flex;
/* #endif */
flex-direction: row;
// width: 500rpx;
flex: 1;
background-color: #f8f8f8;
height: $nav-height;
border-radius: 15px;
padding: 0 15px;
flex-wrap: nowrap;
margin: 7px 0;
line-height: $nav-height;
}
.input-uni-icon {
line-height: $nav-height;
}
.nav-bar-input {
height: $nav-height;
line-height: $nav-height;
/* #ifdef APP-PLUS-NVUE */
width: 370rpx;
/* #endif */
padding: 0 5px;
font-size: 14px;
background-color: #f8f8f8;
}
.box {
height: calc(100vh - 100px);
overflow: hidden;
}
.item {
height: 120rpx;
border-bottom: 1px solid #e5e5e5;
background-color: white;
width: 700rpx;
margin: 26rpx auto 0 auto;
overflow: hidden;
font-size: 28rpx;
line-height: 120rpx;
padding: 0 20rpx;
border-radius: 10rpx;
}
.active {
background-color: #1aad19;
color: white;
}
</style>
注意點:連接了設備后,除非斷開并關閉小程序,不然不要重新連接,會直接卡死
測試打印頁面代碼(核心打印代碼):
數據:
mainCodeArr:[],
qrcodeObj: {
x: 100,
y: 70,
eccLevel: "H",
cellWidth: 6,
encodeMode: "A",
rotation: 0,
codeMode: "M1",
mask: "S7",
content: 1234567890,
},
textObj: {
x: "80",
y: "20",
rotation: "0",
xRatio: "1",
yRatio: "1",
textAlignment: "0",
text: "我的測試商品(1)"
},
code:''
調用方法:
async setCodeIndex(index){
uni.showLoading({
title:'打印中'
})
const item = this.mainCodeArr[index]
CTPL.queryPrintMode(0);
CTPL.setSize(4,3);
CTPL.clearCache();
let code = item.code;
this.code = code;
setTimeout(()=>{
CTPL.drawQRCode(
this.qrcodeObj.x,
this.qrcodeObj.y,
this.qrcodeObj.eccLevel,
this.qrcodeObj.cellWidth,
this.qrcodeObj.encodeMode,
this.qrcodeObj.rotation,
this.qrcodeObj.codeMode,
this.qrcodeObj.mask,
code
);
setTimeout(()=>{
let left = 40;
if(item.product_title.length < 9){
left += ((10 - item.product_title.length) * 10)
}else{
item.product_title = item.product_title.slice(0,9) +'...'
}
// 繪制條碼
CTPL.drawText(
left,
this.textObj.y,
this.textObj.rotation,
this.textObj.xRatio,
this.textObj.yRatio,
this.textObj.textAlignment,
(item.product_title+'('+item.index+')')
);
setTimeout(()=>{
CTPL.setPrintCopies(1, 1);
CTPL.execute();
uni.hideLoading()
if(this.mainCodeArr.length != index +1){
setTimeout(()=>{
this.setCodeIndex(index +1)
},500)
}else{
uni.showModal({
title:'溫馨提示',
content:'打印完成',
showCancel:false
})
}
},1000)
},500)
},1000)
},
調用代碼:
this.setCodeIndex(0)


浙公網安備 33010602011771號