Skip to content
rdlabo.devdocs

Search

Search finds nearby Brother printers. Results arrive on onPrinterAvailable. Call this after Installation. Register the listener before search, keep the discovered BRLMChannelResult (especially channelInfo and port), then continue to Print. Full event list: Events.

Register onPrinterAvailable, retain the channel, then start a Wi-Fi search. The search call itself returns void.

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

let discovered: BRLMChannelResult | undefined;
let availableHandle: PluginListenerHandle | undefined;

const searchWifiPrinters = async () => {
  if (!availableHandle) {
    availableHandle = await BrotherPrint.addListener(
    BrotherPrintEventsEnum.onPrinterAvailable,
    (printer) => {
      discovered = printer;
      console.log('channelInfo', printer.channelInfo);
    },
    );
  }

  await BrotherPrint.search({
    port: BRLMPrinterPort.wifi,
    searchDuration: 15, // seconds
  });
};

const stopSearching = async () => {
  try {
    await BrotherPrint.cancelSearchWiFiPrinter();
  } finally {
    await availableHandle?.remove();
    availableHandle = undefined;
  }
};

Call searchWifiPrinters from the search button and await stopSearching when leaving the screen.

searchDuration applies to wifi and bluetoothLowEnergy. usb is Android only. If nothing is found, you get no error and no printers. Signatures are on the API page.

isChannelAvailable

If you saved the last BRLMChannelResult, check whether that channel is still usable before Print.

import { BrotherPrint } from '@rdlabo/capacitor-brotherprint';
import type { BRLMChannelResult } from '@rdlabo/capacitor-brotherprint';

const checkChannel = async (lastPrinter: BRLMChannelResult) => {
  const { result } = await BrotherPrint.isChannelAvailable(lastPrinter);
  if (!result) {
    await BrotherPrint.search({
      port: lastPrinter.port,
      searchDuration: 15,
    });
  }
};

method isChannelAvailable(...)

If you have saved the last connected BRLMChannelResult,
you can use it to verify whether it is currently usable.

isChannelAvailable(option: BRLMChannelResult) => Promise<isChannelAvailableResult>

type alias isChannelAvailableResult

{ result: boolean; }

type alias BRLMChannelResult

{ port: BRLMPrinterPort; modelName: string; serialNumber: string; macAddress: string; nodeName: string; location: string; /** * This need to connect to the printer. * wifi: IP Address * bluetooth: macAddress * bluetoothLowEnergy: modelName for bluetoothLowEnergy */ channelInfo: string; }

cancelSearchWiFiPrinter / cancelSearchBluetoothPrinter

Use these to stop an active search before its timeout, including when leaving the screen.

import { BrotherPrint } from '@rdlabo/capacitor-brotherprint';

await BrotherPrint.cancelSearchWiFiPrinter();
await BrotherPrint.cancelSearchBluetoothPrinter();

See API for the cancellation signatures.