更新记录

1.0.4(2024-05-21)

完善接入文档

1.0.3(2024-05-17)

1.优化打印不稳定问题

1.0.2(2023-08-08)

1.优化一些问题

查看更多

平台兼容性

HbuilderX/cli最低兼容版本
3.7.1

uni-app

Vue2 Vue3
?
app-vue app-nvue app-android app-ios
? ? ? ?
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
? ? ? ? ? ? ? ? ?
微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序 钉钉小程序 快手小程序 飞书小程序 京东小程序
? ? ? ? ? ? ? ? ?
快应用-华为 快应用-联盟
? ?

uni-app x

app-android app-ios
? ?
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
? ? ? ? ? ? ? ? ?

芯烨XP810便携式蓝牙打印机,打印命令工具类,目前只测试过XP810打印机,理论上支持芯烨80系列所有热敏打印机

概述

本插件是基于uni-app的蓝牙低功耗通讯APIuni.writeBLECharacteristicValue实现的蓝牙打印功能。

使用示例

1.搜索可用蓝牙设备,并选中蓝牙打印机

在页面引入蓝牙功能封装js,构建蓝牙对象bluetooth

import Bluetooth from '@/uni_modules/wufeng-xp810/js_sdk/bluetooth.js';
let bluetooth = new Bluetooth();

在页面的onShow,onHide方法中打开和关闭蓝牙适配器

onShow() {
    bluetooth.openBluetoothAdapter();
}
onHide() {
    bluetooth.closeBluetoothAdapter();
}

打开蓝牙适配器成功后,调用蓝牙设备搜索方法,发现蓝牙设备,然后选中蓝牙打印机

//搜索周边设备
startBluetoothDeviceDiscovery() {
    uni.showLoading({
        title: '蓝牙搜索中'
    });
    let self = this;
    self.bluetoothDeviceList = [];
    setTimeout(() => {
        uni.startBluetoothDevicesDiscovery({
            success: res => {
                uni.onBluetoothDeviceFound(devices => {
                    console.log("发现设备: " + JSON.stringify(devices));
                    if(devices.devices[0].name != ''){
                        //不重复,就添加到devicesList中,
                        if (!self.bluetoothDeviceList.some(item => {
                        return item.deviceId===devices.devices[0].deviceId;
                        })) {
                            self.bluetoothDeviceList.push(devices.devices[0]);
                            }
                        }
                    });
                },
            fail: res => {
                uni.hideLoading();
            }
        })
    }, 200)
},
//停止搜索蓝牙设备
stopBluetoothDevicesDiscovery() {
    uni.hideLoading();
        bluetooth.stopBluetoothDevicesDiscovery();
    },
//选择蓝牙设备
selectDeviceId(device){
    this.stopBluetoothDevicesDiscovery();
    uni.setStorageSync("currentPrinter", device);
}

选择好设备后,将设备信息保存到缓存中,缓存的key值不要修改,后面的连接打印机,打印小票都是通过这个key值去缓存中获取设备信息

2.链接蓝牙打印机,设置打印内容,打印小票

在需要打印小票的页面引入

import PrinterJobs from '@/uni_modules/wufeng-xp810/js_sdk/printerjobs.js'
import Bluetooth from '@/uni_modules/wufeng-xp810/js_sdk/bluetooth.js'
let bluetooth = new Bluetooth();

在页面的onShow或者onLoad方法中调用连接蓝牙打印机方法,由于连接设备耗时比较长,最好使用异步方法

onShow(){
    openPrinter();
}

//打开打印机
async openPrinter(){
    bluetooth.openBluetoothAdapter();
    let device = uni.getStorageSync('currentPrinter');
    bluetooth.deviceId = device.deviceId;
    try {
        //1.链接设备
        let result = await bluetooth.createBLEConnection();
        if(result){
            //2.寻找服务
            let result2 = await bluetooth.getBLEDeviceServices();
            for(let i = 0; i < result2.length; i++){
                let result3 = await bluetooth.getBLEDeviceCharacteristics(device.deviceId, result2[i].uuid);
                if(result3.writeId != ''){
                    bluetooth.serviceId = result2[i].uuid;
                    break;
                }
            }
        }
        console.log("获取服务: " + JSON.stringify(result2));
    } catch (e) {
        console.log("e: " + JSON.stringify(e));
    }
}

构建打印小票内容,并发送内容到打印机打印

async print(data){
    console.log("data:" + JSON.stringify(data));
    let printerJobs = new PrinterJobs();
    printerJobs.setBold(true);//字体加粗
    printerJobs.setAlign('ct');//居中 lt:靠左 ct:居中 rt:靠右
    printerJobs.print('标题');
    printerJobs.setSize(1, 1);//设置字体宽高,1:正常,2:倍宽或倍高
    printerJobs.setBold(false);//正常字体
    printerJobs.setAlign('lt');//内容
    printerJobs.barcode(80, '123456');//条形码高度1-255,条形码内容
    printerJobs.qrcode(8, '678910');//二维码尺寸1-16,二维码内容
    let buffer = printerJobs.buffer();
    const maxChunk = 20;
    for (let i = 0, length = buffer.byteLength; i < length; i += maxChunk) {
        let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length);
        let result = await bluetooth.writeBLECharacteristicValue(subPackage);
        }
}

在页面销毁或关闭时关闭连接

onDestory(){
    closePrinter();
}
//关闭打印机
closePrinter(){
    bluetooth.closeBLEConnection();
    bluetooth.closeBluetoothAdapter();
}

注意事项

1.蓝牙打印机要先手动在手机的蓝牙设置中进行设备配对 2.不要频繁的连接关闭打印机,最好只在页面打开关闭时进行一次连接和关闭

隐私、权限声明

1. 本插件需要申请的系统权限列表:

<uses-permission android:name=\"android.permission.BLUETOOTH\"/>

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问