跳至正文
rdlabo.dev文档

事件

监听打印机发现与打印结果。请在搜索和打印之前注册监听器,避免错过首个事件。

import type { PluginListenerHandle } from '@capacitor/core';
import { BrotherPrint, BrotherPrintEventsEnum } from '@rdlabo/capacitor-brotherprint';

const handles: PluginListenerHandle[] = [];

const registerPrintListeners = async () => {
  handles.push(
    await BrotherPrint.addListener(BrotherPrintEventsEnum.onPrinterAvailable, (printer) => {
      console.log('printer', printer.channelInfo);
    }),
  );
  handles.push(
    await BrotherPrint.addListener(BrotherPrintEventsEnum.onPrint, () => {
      console.log('onPrint');
    }),
  );
  handles.push(
    await BrotherPrint.addListener(BrotherPrintEventsEnum.onPrintFailedCommunication, (info) => {
      console.log('onPrintFailedCommunication', info);
    }),
  );
  handles.push(
    await BrotherPrint.addListener(BrotherPrintEventsEnum.onPrintError, (info) => {
      console.log('onPrintError', info);
    }),
  );
};

const removePrintListeners = async () => {
  await Promise.all(handles.map((handle) => handle.remove()));
};
事件 触发时机
onPrinterAvailable 已发现可连接的打印机
onPrint 打印成功
onPrintFailedCommunication 无法连接打印机
onPrintError 打印失败

如果因图像无效、型号或端口不受支持或无法创建打印设置而导致 printImage 拒绝请求,插件还会发出 onPrintError,其中包含 code: 0 和说明原因的 message。此代码表示插件校验失败,并非 SDK 错误。如果打开打印机通道失败,插件会改为发出 onPrintFailedCommunication。

完整的页面示例见演示应用:

https://github.com/rdlabo-dev/capacitor-brotherprint/blob/v9.0.0/demo/src/app/home/home.page.tshttps://github.com/rdlabo-dev/capacitor-brotherprint/blob/v9.0.0/demo/src/app/home/home.page.ts

method addListener(BrotherPrintEventsEnum.onPrinterAvailable, ...)

查找可与设备连接的打印机。

addListener(eventName: BrotherPrintEventsEnum.onPrinterAvailable, listenerFunc: (printers: BRLMChannelResult) => void) => Promise<PluginListenerHandle>

method addListener(BrotherPrintEventsEnum.onPrint, ...)

打印成功事件

addListener(eventName: BrotherPrintEventsEnum.onPrint, listenerFunc: () => void) => Promise<PluginListenerHandle>

method addListener(BrotherPrintEventsEnum.onPrintFailedCommunication, ...)

无法连接打印机,例如 Bluetooth 未开启或打印机已关机。

addListener(eventName: BrotherPrintEventsEnum.onPrintFailedCommunication, listenerFunc: (info: ErrorInfo) => void) => Promise<PluginListenerHandle>

method addListener(BrotherPrintEventsEnum.onPrintError, ...)

打印失败。

addListener(eventName: BrotherPrintEventsEnum.onPrintError, listenerFunc: (info: ErrorInfo) => void) => Promise<PluginListenerHandle>

enum BrotherPrintEventsEnum

成员 值
onPrinterAvailable 'onPrinterAvailable'
onPrint 'onPrint'
onPrintFailedCommunication 'onPrintFailedCommunication'
onPrintError 'onPrintError'

interface PluginListenerHandle

属性 类型
remove () => Promise<void>

type alias ErrorInfo

{ message: string; code: number; }