本文へ移動
rdlabo.devdocs

Search

Search は近くの Brother プリンターを探します。結果は onPrinterAvailable で届きます。インストール のあとで呼び出します。search より前にリスナーを登録し、見つかった BRLMChannelResult(特に channelInfoport)を保持してから Print へ進みます。イベント一覧は Events です。

onPrinterAvailable を登録してチャネルを保持し、Wi-Fi 探索を開始します。search 自体の戻り値は 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;
  }
};

探索ボタンから searchWifiPrinters を呼び、画面を離れるときに stopSearching を待って監視を解放します。

BluetoothとBLE

iOSのbluetoothは、最初に接続済みのMFiプリンターを一覧にします。接続済みの機器がない場合はシステムのBluetoothアクセサリ選択画面を表示し、プリンターを選択・ペアリングできます。探索のPromiseは選択画面のコールバック後に完了し、選択画面のエラーではrejectします。

BLE対応プリンターにはport: BRLMPrinterPort.bluetoothLowEnergyを使います。iOSではBluetoothアクセサリ選択画面を使わず、startBLESearchで探索します。探索結果のchannelInfo(BLEローカル名)を変更せず、isChannelAvailableprintImageへ渡してください。BLE探索エラーではPromiseがrejectします。QL-820NWB/QL-820NWBcはBLE印刷に対応していないため、bluetoothまたはwifiを使います。

Androidでは、bluetoothで探索する前にシステム設定でペアリングしてください。SDKはペアリング済みプリンターを一覧にし、iOSのようなアクセサリ選択画面は提供しません。Bluetooth・BLEの探索は完了時にresolveし、SDKエラーではrejectします。Android 12以降は「付近のデバイス」、Android 11以前のBLEでは位置情報の権限を要求します。Bluetoothの権限がない場合、isChannelAvailablefalseを返します。

searchDurationwifibluetoothLowEnergy で使います。usb は Android のみです。見つからない場合はエラーにはならず、プリンターも届きません。

method search(...)

Search for printers. If not found, it will return an empty array.(not error)

search(option: BRLMSearchOption) => Promise<void>

type alias BRLMSearchOption

{ /** * 'usb' is android only, and now developing. */ port: BRLMPrinterPort; /** * searchDuration is the time to end search for devices. * default is 15 seconds. * use only port is 'wifi' or 'bluetoothLowEnergy'. */ searchDuration: number; /** * Android Bluetooth Classic only. Include only devices whose Bluetooth class * reports a printer. Defaults to false; ignored for other ports and on iOS. * This does not identify Brother devices. Devices with an unknown class are excluded when true. */ bluetoothPrintersOnly?: boolean; }

Androidでプリンターのクラスに絞る

AndroidのBluetooth Classic探索はペアリング済み端末を返します。Bluetooth Imaging/Printerクラスを報告する端末だけに絞るには、次のように指定します。

await BrotherPrint.search({
  port: BRLMPrinterPort.bluetooth,
  searchDuration: 15,
  bluetoothPrintersOnly: true,
});

bluetoothPrintersOnlyの既定値はfalseで、従来どおり絞り込まずに返します。iOSやBLEを含む他のポートでは無視します。このフィルターは端末名を使わず、Brother製品を特定するものでもないため、他社のプリンターが含まれる場合があります。有効にすると、Bluetoothクラスが不明またはプリンター以外の端末は除外します。

isChannelAvailable

最後の BRLMChannelResult を保存している場合、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

画面を離れるときなど、タイムアウト前に実行中の探索を停止するときに使います。

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

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

停止メソッドのシグネチャは API を参照してください。