StarXpand SDK for React Native Developer's Manual Ver. 1.6.0

Last update: February 9, 2024

Search for Printer

Use StarDeviceDiscoveryManager to asynchronously search for the printer.
When you start a search, events occur in the order where they are found, and you can acquire the StarPrinter instance instantly.

Memo
The StarPrinter instance provides the functions required to communicate with the printer.
There are two methods, including searching, to acquire the StarPrinter method.

  • Method to acquire by searching (Step 8 on this page)
  • Method to generate by passing interfaceType and identifier to the constructor of the StarPrinter class

By using this StarPrinter instance, you can send printing data to the printer and control POS peripheral devices.

Send the printing data using the procedure below.
The sample code is available at the bottom of the page. Also, refer to the sample application in the SDK.

1 Prepare a StarDeviceDiscoveryManager type variable used for searching.

private _manager?: StarDeviceDiscoveryManager;

2 Select the target interface. Generate the InterfaceType array you would like to search for.

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

3 Acquire the StarDeviceDiscoveryManager instance using the array generated in Step2 as an argument.

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

4 Set the search time.

this._manager.discoveryTime = 10000;

5 Implement the process for when the printer is found. Set the delegate method for when the printer is found in the onPrinterFound property.

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

6 Implement the process for when the search is finished. Set the delegate method for when the search is finished in the onDiscoveryFinished property.

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

7 Call the startDiscovery method to start searching.
When the printer is found, the delegate method implemented in Step5 is called. When the search is finished, the delegate method implemented in Step6 is called.

await this._manager.startDiscovery();

8 Implement the process for when an error occurs. You can identify the error cause based on the error instance type.
Possible errors are described in the API reference of the create method of the StarDeviceDiscoveryManagerFactory class and the startDiscovery method of the StarDeviceDiscoveryManager class.

}
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)}`);
}
Sample code

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)}`);
  }
}