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

Brother Print SDK对接QL系列标签打印机蓝牙连接缺纸检测问题咨询

方案1:打印前主动查询打印机状态

在调用flushPTTPrint()执行打印前,先主动调用SDK提供的getPrinterStatus()方法查询设备状态,即可提前识别缺纸、开盖等异常,无需等待打印触发超时。
修改后的核心代码示例:

void printTemplate(int templateKey, String newText) {
    // Specify Printer
    final Printer printer = new Printer();
    PrinterInfo settings = printer.getPrinterInfo();
    settings.printerModel = Model.QL_1110NWB;
    settings.ipAddress = "your-printer-ip";
    // 新增:设置蓝牙通讯超时为5秒,避免长时间等待
    settings.setCommunicationTimeout(5000);

    // Connect, then print
    new Thread(new Runnable() {
        @Override
        public void run() {
            if (printer.startCommunication()) {
                // 新增:打印前主动查询状态
                PrinterStatus preStatus = printer.getPrinterStatus();
                if (preStatus.errorCode != ErrorCode.ERROR_NONE) {
                    if (preStatus.errorCode == ErrorCode.ERROR_PAPER_EMPTY) {
                        Log.d("TAG", "检测到缺纸,终止打印");
                    } else {
                        Log.d("TAG", "打印机异常:" + preStatus.errorCode);
                    }
                    printer.endCommunication();
                    return;
                }
                // Specify the template key and the printer encode
                if (startPTTPrint(templateKey, null)) {
                    // Replace text object with new text
                    replaceText(newText);

                    // Start print
                    PrinterStatus result = printer.flushPTTPrint();
                    if (result.errorCode != ErrorCode.ERROR_NONE) {
                        Log.d("TAG", "ERROR - " + result.errorCode);
                    }
                }
                printer.endCommunication();
            }
        }
    }).start();
}

方案2:注册打印机状态监听器实时接收通知

你可以提前注册PrinterStatusListener,蓝牙连接成功后打印机状态变化时会主动推送通知,缺纸时无需触发打印即可实时收到MESSAGE_PAPER_EMPTY消息,适合需要提前感知设备状态的场景。
注册监听器示例代码:

printer.setPrinterStatusListener(new PrinterStatusListener() {
    @Override
    public void onStatusChanged(PrinterStatus status) {
        if (status.errorCode == ErrorCode.ERROR_PAPER_EMPTY) {
            // 实时处理缺纸逻辑
            Log.d("TAG", "实时检测到缺纸");
        }
    }
});

补充注意事项

  • 确保你使用的是官方最新版Brother Print SDK for Android,旧版本存在蓝牙状态上报延迟、缺纸错误码识别异常的已知问题
  • Android 12及以上系统需确保已申请BLUETOOTH_CONNECT、BLUETOOTH_SCAN权限,权限缺失会导致蓝牙状态读取超时

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 21:12:01