uniapp 实现安卓蓝牙连接热敏打印进打印标签功能
勾选设备低功耗蓝牙配置模块
在 manifest.json 中勾选低功耗蓝牙模块。

代码实现
低功耗打印机的链接各个厂商大同小异,需要注意的点如下:
-
根据自己需要的读写权限,匹配正确的
serviceId和characteristicId.这两个
UUID如果不匹配,尽管能连上蓝牙,也不能正常工作。Service和Characteristic、Property
相对来说,Service是服务,Characteristic则是特征值。一般来说,蓝牙里面有多个Service,一个Service里面包括多个Characteristic。一个蓝牙协议里面包含的Service和Characteristic是比较多的 ,那么这么多的同名属性用什么来区分呢?就是UUID,每个Service或者Characteristic都有一个 128 bit 的UUID来标识。Service可以理解为一个功能集合,而Characteristic比较重要,蓝牙设备正是通过Characteristic来进行设备间的交互的,这些Characteristic又包含一些属性Property,如读、写、订阅等操作。原文在这里
-
明确要使用哪种指令进行打印输入。
一般热敏打印机采用的输入指令集为 ESC 指令集或者 TSPL 指令集,这个要找厂商确认好。
-
指令编写完毕后,要选择正确的编码格式将字符串转为输入流传入打印机中。
具体代码如下:
<template>
<view class="content">
<button class="btn" type="primary" :loading="isSearching" @tap="startSearch">开始搜索 </button>
<button class="btn" type="warn" @tap="stopSearch">停止搜索</button>
<button class="btn" type="warn" :disabled="BLEInformation.deviceId.length == 0" @tap="labelTest()">打印</button>
<picker style='margin:20px' mode='selector' :range='buffSize' :value='buffIndex' @change='buffBindChange'>
当前每次发送字节数为(点击可更换):{{buffSize[buffIndex]}}
</picker>
<picker style='margin:20px' mode='selector' :range='printNum' :value='printNumIndex' @change='printNumBindChange'>
当前打印份数(点击可更换):{{printNum[printNumIndex]}}
</picker>
<view v-for="(item) in list" :data-title="item.deviceId" :data-name="item.name"
:data-advertisData="item.advertisServiceUUIDs" :key="item.deviceId" @tap="bindViewTap">
<view class="item">
<view class="deviceId block">{{item.deviceId}}</view>
<view class="name block">{{item.name}}</view>
</view>
</view>
</view>
</template>
<script>
var tsc = require("./tsc.js");
var encode = require("./encoding.js");
export default {
data() {
return {
isSearching: false, //是否正在搜索中
list: [],
services: [],
serviceId: 0,
writeCharacter: false,
readCharacter: false,
notifyCharacter: false,
BLEInformation: {
deviceId: "",
writeCharaterId: "",
writeServiceId: "",
notifyCharaterId: "",
notifyServiceId: "",
readCharaterId: "",
readServiceId: "",
},
sendContent: "",
looptime: 0,
currentTime: 1,
lastData: 0,
oneTimeData: 0,
buffSize: [],
buffIndex: 0,
printNum: [],
printNumIndex: 0,
printerNum: 1,
currentPrint: 1,
isLabelSend: false
};
},
onLoad() {
// console.log(this.Bluetooth)
},
onReady() {
let list = []
let numList = []
let j = 0
for (let i = 20; i < 200; i += 10) {
list[j] = i;
j++
}
for (let i = 1; i < 10; i++) {
numList[i - 1] = i
}
this.buffSize = list;
this.oneTimeData = list[0];
this.printNum = numList;
this.printerNum = numList[0];
},
onUnload() {
//停止搜索蓝牙设备
if (this.isSearching) {
uni.stopBluetoothDevicesDiscovery();
}
let that = this
uni.closeBLEConnection({
deviceId:that.BLEInformation.deviceId,
success: function(res) {
console.log("关闭蓝牙成功")
},
})
},
methods: {
//错误码提示
errorCodeTip(code) {
if (code == 0) {
//正常
} else if (code == 10000) {
uni.showToast({
title: '未初始化蓝牙适配器',
icon: 'none'
})
} else if (code == 10001) {
uni.showToast({
title: '当前蓝牙适配器不可用',
icon: 'none'
})
} else if (code == 10002) {
uni.showToast({
title: '没有找到指定设备',
icon: 'none'
})
} else if (code == 10003) {
uni.showToast({
title: '连接失败',
icon: 'none'
})
} else if (code == 10004) {
uni.showToast({
title: '没有找到指定服务',
icon: 'none'
})
} else if (code == 10005) {
uni.showToast({
title: '没有找到指定特征值',
icon: 'none'
})
} else if (code == 10006) {
uni.showToast({
title: '当前连接已断开',
icon: 'none'
})
} else if (code == 10007) {
uni.showToast({
title: '当前特征值不支持此操作',
icon: 'none'
})
} else if (code == 10008) {
uni.showToast({
title: '其余所有系统上报的异常',
icon: 'none'
})
} else if (code == 10009) {
uni.showToast({
title: 'Android 系统特有,系统版本低于 4.3 不支持 BLE',
icon: 'none'
})
}
},
//开始搜索蓝牙
startSearch() {
let that = this
uni.openBluetoothAdapter({
success(res) {
uni.getBluetoothAdapterState({
success(res2) {
console.log('getBluetoothAdapterState:', res2)
if (res2.available) {
that.isSearching = true;
if (res2.discovering) {
uni.showToast({
title: '正在搜索附近打印机设备',
icon: "none"
})
return;
}
//获取蓝牙设备信息
that.getBluetoothDevices()
// that.checkPemission()
} else {
uni.showModal({
title: '提示',
content: '本机蓝牙不可用',
})
}
}
});
},
fail() {
uni.showModal({
title: '提示',
content: '蓝牙初始化失败,请打开蓝牙',
})
}
})
},
stopSearch() {
uni.stopBluetoothDevicesDiscovery({
success: (res) => {
this.isSearching = false;
console.log('stop:', res)
},
fail: (e) => {
console.log('stop:', e)
this.errorCodeTip(e.errCode);
}
})
},
//校验权限
checkPemission() {
let that = this
that.getBluetoothDevices();
},
//获取蓝牙设备信息
getBluetoothDevices() {
let that = this
that.list = [];
uni.startBluetoothDevicesDiscovery({
success(res) {
// console.log(res)
//蓝牙设备监听 uni.onBluetoothDeviceFound
plus.bluetooth.onBluetoothDeviceFound((result) => {
let arr = that.list;
let devices = [];
let list = result.devices;
for (let i = 0; i < list.length; ++i) {
if (list[i].name && list[i].name != "未知设备") {
let arrNew = arr.filter((item) => {
return item.deviceId == list[i].deviceId;
});
// console.log('arrNew:',arrNew.length)
if (arrNew.length == 0) {
devices.push(list[i]);
}
}
}
that.list = arr.concat(devices);
});
that.time = setTimeout(() => {
// uni.getBluetoothDevices
plus.bluetooth.getBluetoothDevices({
success(res2) {
let devices = [];
let list = res2.devices;
for (let i = 0; i < list.length; ++i) {
if (list[i].name && list[i].name != "未知设备") {
devices.push(list[i]);
}
}
that.list = devices;
console.log('getBluetoothDevices:', res2);
},
})
clearTimeout(that.time);
}, 3000);
}
});
},
//绑定蓝牙
bindViewTap(e) {
let that = this;
let {
title
} = e.currentTarget.dataset;
this.stopSearch();
that.serviceId = 0;
that.writeCharacter = false;
that.readCharacter = false;
that.notifyCharacter = false;
uni.showLoading({
title: '正在连接',
})
console.log('deviceId:', title)
plus.bluetooth.createBLEConnection({
deviceId: title,
success(res) {
console.log('createBLEConnection success:', res)
that.BLEInformation.deviceId = title;
that.getSeviceId()
},
fail(e) {
that.errorCodeTip(e.errCode);
uni.hideLoading()
}
})
},
//获取蓝牙设备所有服务(service)。
getSeviceId() {
let that = this;
let t = setTimeout(() => {
plus.bluetooth.getBLEDeviceServices({
deviceId: that.BLEInformation.deviceId,
success(res) {
console.log('getBLEDeviceServices success:', res)
that.services = res.services;
that.getCharacteristics()
},
fail: function(e) {
that.errorCodeTip(e.code);
uni.hideLoading()
}
})
clearTimeout(t);
}, 1500)
},
getCharacteristics() {
var that = this
let {
services: list,
serviceId: num,
writeCharacter: write,
readCharacter: read,
notifyCharacter: notify
} = that;
plus.bluetooth.getBLEDeviceCharacteristics({
deviceId: that.BLEInformation.deviceId,
serviceId: list[num].uuid,
success(res) {
// console.log(res)
for (var i = 0; i < res.characteristics.length; ++i) {
var properties = res.characteristics[i].properties
var item = res.characteristics[i].uuid
if (!notify) {
if (properties.notify) {
that.BLEInformation.notifyCharaterId = item;
that.BLEInformation.notifyServiceId = list[num].uuid;
notify = true
}
}
if (!write) {
if (properties.write) {
that.BLEInformation.writeCharaterId = item;
that.BLEInformation.writeServiceId = list[num].uuid;
write = true
}
}
if (!read) {
if (properties.read) {
that.BLEInformation.readCharaterId = item;
that.BLEInformation.readServiceId = list[num].uuid;
read = true
}
}
}
if (!write || !notify || !read) {
num++
that.writeCharacter = write;
that.readCharacter = read;
that.notifyCharacter = notify;
that.serviceId = num;
if (num == list.length) {
uni.showModal({
title: '提示',
content: '找不到该读写的特征值',
})
} else {
that.getCharacteristics()
}
} else {
uni.showToast({
title: "连接成功",
});
uni.hideLoading()
}
},
fail: function(e) {
console.log("getBLEDeviceCharacteristics fail:", e);
that.errorCodeTip(e.errCode);
}
})
},
//标签测试
labelTest() {
let that = this;
let command = tsc.jpPrinter.createNew()
command.setSize(48, 40)
command.setGap(0)
command.setCls()
// command.setText(0, 30, "TSS24.BF2", 1, 1, "图片")
// // command.setQR(40, 120, "L", 5, "A", "www.smarnet.cc佳博智汇")
// command.setText(60, 90, "TSS24.BF2", 1, 1, "佳博智汇")
// command.setText(170, 50, "TSS24.BF2", 1, 1, "小程序测试")
command.setText(170, 90, "TSS24.BF2", 1, 1, "测试数字12345678")
command.setText(170, 120, "TSS24.BF2", 1, 1, "测试英文abcdefg")
// command.setText(170, 150, "TSS24.BF2", 1, 1, "测试符号/*-+!@#$")
// command.setBarCode(170, 180, "EAN8", 64, 1, 3, 3, "1234567")
command.setPagePrint()
that.isLabelSend = true;
that.prepareSend(command.getData())
},
//准备发送,根据每次发送字节数来处理分包数量
prepareSend(buff) {
console.log(buff);
let that = this
let time = that.oneTimeData
let looptime = parseInt(buff.length / time);
let lastData = parseInt(buff.length % time);
console.log(looptime + "---" + lastData)
this.looptime = looptime + 1;
this.lastData = lastData;
this.currentTime = 1;
that.Send(buff)
},
//查询打印机状态
queryStatus() {
let command = esc.jpPrinter.Query();
command.getRealtimeStatusTransmission(1);
},
//分包发送
Send(buff) {
let that = this
let {
currentTime,
looptime: loopTime,
lastData,
oneTimeData: onTimeData,
printerNum: printNum,
currentPrint
} = that;
let buf;
let dataView;
if (currentTime < loopTime) {
buf = new ArrayBuffer(onTimeData)
dataView = new DataView(buf)
for (var i = 0; i < onTimeData; ++i) {
dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
}
} else {
buf = new ArrayBuffer(lastData)
dataView = new DataView(buf)
for (var i = 0; i < lastData; ++i) {
dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
}
}
console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
plus.bluetooth.writeBLECharacteristicValue({
deviceId: that.BLEInformation.deviceId,
serviceId: that.BLEInformation.writeServiceId,
characteristicId: that.BLEInformation.writeCharaterId,
value: buf,
success: function(res) {
console.log(res)
},
fail: function(e) {
console.log(e)
},
complete: function() {
currentTime++
if (currentTime <= loopTime) {
that.currentTime = currentTime;
that.Send(buff)
} else {
uni.showToast({
title: '已打印第' + currentPrint + '张',
})
if (currentPrint == printNum) {
that.looptime = 0;
that.lastData = 0;
that.currentTime = 1;
that.isLabelSend = false;
that.currentPrint = 1;
} else {
currentPrint++;
that.currentPrint = currentPrint;
that.currentTime = 1;
that.Send(buff)
}
}
}
})
},
buffBindChange: function(res) { //更改打印字节数
let index = res.detail.value
let time = this.buffSize[index]
this.buffIndex = index;
this.oneTimeData = time;
},
printNumBindChange: function(res) { //更改打印份数
let index = res.detail.value
let num = this.printNum[index]
this.printNumIndex = index;
this.printerNum = num;
},
}
}
</script>
<style lang="less">
.btn {
margin-top: 50rpx;
height: 40px;
width: 600rpx;
line-height: 40px;
}
.item {
display: block;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
margin: 0 30px;
margin-top: 10px;
background-color: #FFA850;
border-radius: 5px;
border-bottom: 2px solid #68BAEA;
}
.block {
display: block;
color: #ffffff;
padding: 5px;
}
</style>
1万+

被折叠的 条评论
为什么被折叠?



