更新记录

1.0.5(2026-09-30) 下载此版本

1.调整打印速率,将依赖包打进组件内部

1.0.4(2024-05-21) 下载此版本

完善接入文档

1.0.3(2024-05-17) 下载此版本

1.优化打印不稳定问题

查看更多

平台兼容性

uni-app(5.0)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- - - - √ - 4.4 12 -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - - -

uni-app x(5.0)

Chrome Safari Android iOS 鸿蒙 微信小程序
- - - - - -

芯烨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){
            //协商MTU(仅Android生效,iOS无需设置),可大幅加快打印速度
            await bluetooth.setBLEMTU(512);
            //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();
    //按协商后的MTU自动分包发送
    await bluetooth.writeBuffer(buffer);
}

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

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

注意事项

1.蓝牙打印机要先手动在手机的蓝牙设置中进行设备配对 2.不要频繁的连接关闭打印机,最好只在页面打开关闭时进行一次连接和关闭 3.在manifest.json的"App权限配置"中勾选以下权限:

  • android.permission.BLUETOOTH、android.permission.BLUETOOTH_ADMIN(Android 11及以下)
  • android.permission.BLUETOOTH_SCAN、android.permission.BLUETOOTH_CONNECT(Android 12及以上必需,否则搜索和连接直接失败)
  • android.permission.ACCESS_FINE_LOCATION(Android 6~11搜索BLE设备需要定位权限,且需打开手机的定位开关) 4.打印速度由两个参数控制,可在构建Bluetooth对象后调整:bluetooth.maxChunkSize(单包字节数,默认128)和bluetooth.writeInterval(每包间隔毫秒,默认20)。setBLEMTU只是增大连路层包长,不代表打印机能消化更高速率;调大包长后如果小票缺内容、乱码,说明超过了打印机蓝牙模组的缓冲能力,请调小maxChunkSize或调大writeInterval

隐私、权限声明

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

<uses-permission android:name=\"android.permission.BLUETOOTH\"/> <uses-permission android:name=\"android.permission.BLUETOOTH_ADMIN\"/> <uses-permission android:name=\"android.permission.BLUETOOTH_SCAN\"/> <uses-permission android:name=\"android.permission.BLUETOOTH_CONNECT\"/> <uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\"/>

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

插件不采集任何数据

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

无

许可协议

MIT协议