StarXpand SDK for React Native 開発者向けマニュアル Ver. 1.7.0

最終更新日: 2024年 6月 24日

プリンターを検索する

StarDeviceDiscoveryManagerを使って、非同期にプリンター検索を行います。
検索を開始すると見つかった順にイベントが発生し、StarPrinterインスタンスを即時取得することができます。

Memo
StarPrinterインスタンスは、プリンタと通信するために必要な機能を提供するインスタンスです。
StarPrinterインスタンスを取得するには、検索を含み二通りの方法があります。

  • 検索によって取得する方法 (このページの手順8
  • StarPrinterクラスのコンストラクタにinterfaceTypeとidentifierを渡して生成する方法

このStarPrinterインスタンスを使って、印刷データをプリンターに送信したり、POS周辺機器の制御を行ったりすることができます。

以下の手順を参考に印刷データを送信してください。
ページ下部にサンプルコードがあります。 また、SDKのサンプルアプリも参考にしてください。

1検索を行うために使用するStarDeviceDiscoveryManager型の変数を用意します。

private _manager?: StarDeviceDiscoveryManager;

2検索するインターフェイスを選択します。検索したいInterfaceTypeの配列を生成してください。

private startDiscovery = async() => {
try { 
            var interfaceTypes: Array<InterfaceType> = [InterfaceType.Lan, InterfaceType.Bluetooth];

3 2で生成した配列を引数にして、StarDeviceDiscoveryManagerのインスタンスを取得します。

this._manager = await StarDeviceDiscoveryManagerFactory.create(interfaceTypes);

4検索を実行する時間を設定します。

this._manager.discoveryTime = 10000;

5プリンターが見つかったときの処理を実装します。onPrinterFoundプロパティにプリンターが見つかったときのデリゲートメソッドをセットしてください。

this._manager.onPrinterFound = (printer: StarPrinter) => {
        console.log(`Found printer: ${printer.connectionSettings.identifier}.`);
};

6検索が終了したときの処理を実装します。onDiscoveryFinishedプロパティに検索が終了したときのデリゲートメソッドをセットしてください。

this._manager.onDiscoveryFinished = () => {
        console.log(`Discovery finished.`);
};

7検索を開始するためにstartDiscoveryメソッドを呼び出します。
プリンターが見つかったときに 5でセットしたデリゲートメソッドが、 検索が終了したときに 6でセットしたデリゲートメソッドが呼ばれます。

await this._manager.startDiscovery();

8エラー時の処理を実装します。errorインスタンスの型によりエラーを判別できます。
発生する可能性のあるエラーはStarDeviceDiscoveryManagerFactoryクラスのcreateメソッド、 StarDeviceDiscoveryManagerクラスのstartDiscoveryメソッドのAPIリファレンスに記載されています。

}
catch(error) {
  if (error instanceof StarIO10IllegalDeviceStateException) {
    if (error.codeCode == StarIO10ErrorCode.BluetoothUnavailable) {
      // Example of error: Bluetooth capability of host device is disabled.
      // This may be due to the host device's Bluetooth being off, the host device not having Bluetooth, etc. 
    }
  }
  console.log(`Error: ${String(error)}`);
}
サンプルコード

private _manager?: StarDeviceDiscoveryManager;

private startDiscovery = async() => {
  try {
    var interfaceTypes: Array<InterfaceType> = [InterfaceType.Lan, InterfaceType.Bluetooth];
    this._manager = await StarDeviceDiscoveryManagerFactory.create(interfaceTypes);
    this._manager.discoveryTime = 10000;
    this._manager.onPrinterFound = (printer: StarPrinter) => {
      console.log(`Found printer: ${printer.connectionSettings.interfaceType } ${printer.connectionSettings.identifier}.`);
    };

    this._manager.onDiscoveryFinished = () => {
        console.log(`Discovery finished.`);
    };
    await this._manager.startDiscovery();
  }
  catch(error) {
    if (error instanceof StarIO10IllegalDeviceStateException) {
      if (error.codeCode == StarIO10ErrorCode.BluetoothUnavailable) {
          // Example of error: Bluetooth capability of host device is disabled.
          // This may be due to the host device's Bluetooth being off, the host device not having Bluetooth, etc. 
      }
    }
    console.log(`Error: ${String(error)}`);
  }
}