Saltar al contenido

Eventos

Escucha los eventos que notifican las impresoras detectadas y los resultados de impresión. Registra los listeners antes de la búsqueda y de la impresión para no perder los primeros eventos.

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()));
};
Evento Cuándo se produce
onPrinterAvailable Se encontró una impresora disponible para la conexión
onPrint La impresión finalizó correctamente
onPrintFailedCommunication No se pudo contactar con la impresora
onPrintError La impresión falló

Si printImage rechaza la solicitud porque la imagen no es válida, el modelo o el puerto no son compatibles o no se ha podido crear la configuración de impresión, también emite onPrintError con code: 0 y un campo message que explica el motivo. Este código indica un error de validación del plugin. Si falla la apertura del canal de la impresora, se emite onPrintFailedCommunication.

Consulta una página completa en el proyecto de demostración:

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, ...)

Busca la impresora a la que se puede conectar el dispositivo.

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

method addListener(BrotherPrintEventsEnum.onPrint, ...)

Evento de impresión correcta

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

method addListener(BrotherPrintEventsEnum.onPrintFailedCommunication, ...)

No se pudo conectar con la impresora. Por ejemplo, es posible que Bluetooth o la impresora estén apagados.

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

method addListener(BrotherPrintEventsEnum.onPrintError, ...)

La impresión ha fallado.

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

enum BrotherPrintEventsEnum

Miembro Valor
onPrinterAvailable 'onPrinterAvailable'
onPrint 'onPrint'
onPrintFailedCommunication 'onPrintFailedCommunication'
onPrintError 'onPrintError'

interface PluginListenerHandle

Propiedad Tipo
remove () => Promise<void>

type alias ErrorInfo

{ message: string; code: number; }