You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Web Bluetooth连接热敏蓝牙打印机后,如何发送打印指令?

问题描述

我一直尝试通过Web Bluetooth连接热敏蓝牙打印机进行打印,现已成功连接到打印机,但不清楚应使用什么指令向打印机发送字符/文件,网上也找不到相关示例。似乎大家都通过安卓APP实现该功能,但既然已连接打印机,应该可以直接发送内容打印,对吗?

我的连接代码:

function getdevices(){
    navigator.bluetooth.requestDevice({ filters: [{ name: ['BlueTooth Printer'] }] })
.then(device => { 
    console.log(device)
    // Attempts to connect to remote GATT Server.
  return device.gatt.connect();
})
.then(server => { 
    console.log("server log :", server) 
}) 
.catch(error => { console.error(error); });
}

设备控制台日志:

gatt
: 
BluetoothRemoteGATTServer {device: BluetoothDevice, connected: true}
id
: 
"t+kEYShfsUR6puq1km6P+cc=="
name
: 
"BlueTooth Printer"
onadvertisementreceived
: 
null
ongattserverdisconnected
: 
null
watchingAdvertisements
: 
false

Server控制台日志:

server log : 
BluetoothRemoteGATTServer {device: BluetoothDevice, connected: true}
connected
: 
true
device
: 
BluetoothDevice {id: 't+kEYShfsUR6puq1km6P4Q==', name: 'BlueTooth Printer', gatt: BluetoothRemoteGATTServer, ongattserverdisconnected: null, watchingAdvertisements: false, …}
[[Prototype]]
: 
BluetoothRemoteGATTServer

核心问题:已显示连接成功,该如何与打印机通信?是调用server.open?还是server.print?或是server.print(file)?连接蓝牙打印机后,具体应执行哪些操作来完成打印?


解决方案

Web Bluetooth API没有server.open或server.print这类直接方法,必须通过**GATT服务(Service)和特征(Characteristic)**来和打印机通信,具体操作步骤如下:

1. 定位打印机的GATT服务与可写特征

绝大多数热敏蓝牙打印机使用通用的串口透传服务,常见UUID组合为:

  • 服务UUID:0000ffe0-0000-1000-8000-00805f9b34fb
  • 可写特征UUID:0000ffe1-0000-1000-8000-00805f9b34fb

如果该组合无效,需查看打印机官方手册获取准确UUID。

2. 扩展连接代码,完成通信链路搭建

在连接GATT Server后,继续调用API获取服务和特征,并发送打印数据:

function getdevices(){
    navigator.bluetooth.requestDevice({ filters: [{ name: ['BlueTooth Printer'] }] })
    .then(device => { 
        console.log(device);
        return device.gatt.connect();
    })
    .then(server => { 
        console.log("server log :", server);
        // 获取串口透传服务
        return server.getPrimaryService('0000ffe0-0000-1000-8000-00805f9b34fb');
    })
    .then(service => {
        // 获取可写特征
        return service.getCharacteristic('0000ffe1-0000-1000-8000-00805f9b34fb');
    })
    .then(characteristic => {
        // 构造打印内容(支持普通文本)
        const printContent = "测试打印内容\n\n";
        // 将文本转换为打印机可识别的二进制数据
        const encoder = new TextEncoder();
        const data = encoder.encode(printContent);
        // 向特征写入数据,触发打印
        return characteristic.writeValue(data);
    })
    .then(() => {
        console.log("打印指令已发送");
    })
    .catch(error => { console.error(error); });
}

3. 使用ESC/POS指令控制打印格式

热敏打印机支持ESC/POS指令集,可实现格式控制,常用指令示例:

  • 换行:\n
  • 开启加粗:\x1B\x45\x01
  • 关闭加粗:\x1B\x45\x00
  • 自动切纸:\x1D\x56\x00

示例:打印加粗文本+自动切纸

// 构造带格式的打印内容
const printContent = "\x1B\x45\x01这是加粗文本\x1B\x45\x00\n\n普通测试文本\n\n\x1D\x56\x00";
const encoder = new TextEncoder();
const data = encoder.encode(printContent);
characteristic.writeValue(data);

4. 打印文件(以图片为例)

打印图片需将其转换为ESC/POS支持的位图格式,大致步骤:

  1. 将图片缩放到打印机打印宽度(通常为384像素)
  2. 转换为灰度图像
  3. 使用ESC/POS位图指令封装数据后发送

内容的提问来源于stack exchange,提问作者Marco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:25:30