プリンターを検索する
StarDeviceDiscoveryManagerを使って、非同期にプリンター検索を行います。
検索を開始すると見つかった順にイベントが発生し、StarPrinterインスタンスを即時取得することができます。
Memo
StarPrinterインスタンスは、プリンタと通信するために必要な機能を提供するインスタンスです。
StarPrinterインスタンスを取得するには、検索を含み二通りの方法があります。
- 検索によって取得する方法 (このページの手順8)
- StarPrinterクラスのコンストラクタにinterfaceTypeとidentifierを渡して生成する方法
このStarPrinterインスタンスを使って、印刷データをプリンターに送信したり、POS周辺機器の制御を行ったりすることができます。
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)}`);
}
}
Bluetooth LEデバイスのRSSI(電波強度)を観測する
ホストデバイス近傍のスター精密製Bluetooth LEデバイス(*1)のRSSI値を、StarDeviceDiscoveryManager APIを使⽤して取得できます。
*1: 本機能に対応するスター精密製Bluetooth LEデバイスは、StarPrinterInformationBluetoothLE.rssi APIリファレンスをご参照ください。
ユースケース
- スター精密製Bluetooth LEデバイスからのRSSI値を利用した、ホストデバイスが対象Bluetooth LEデバイス(およびこれに接続されたキャッシュドロアーなどの周辺機器)の近傍に存在するかの参考判定
関連API
サンプルコード
private _manager?: StarDeviceDiscoveryManager;
private startDiscovery = async() => {
try {
var interfaceTypes: Array<InterfaceType> = [InterfaceType.BluetoothLE];
this._manager = await StarDeviceDiscoveryManagerFactory.create(interfaceTypes);
manager.isEnabledFindSameDevice = true; // 本プロパティをtrueにし、同一デバイスの連続検出を有効にします
this._manager.discoveryTime = 10000;
this._manager.onPrinterFound = (printer: StarPrinter) => { // 検出されたBluetooth LEデバイスは、プロパティを通して利用できます
var rssi = printer.information?.detail.bluetoothLE?.rssi;
if (rssi != undefined) {
console.log(`RSSI: ${rssi}`); // RSSI値はこのように取得します
}
};
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)}`);
}
}
留意事項
- RSSI値は一般的にデバイス間の距離が近いほど高くなりますが、使用する各種デバイス(ホストデバイスおよびBluetooth LEデバイス)の機種やそれぞれの固有の電波特性、Bluetooth LEデバイスの向き、また電波干渉や遮蔽等の周辺環境といった様々な要因によりその値は変動します。
- 本APIを利用したアプリケーションを開発される場合、実際に導入される環境を基にした十分な検証をお願いします。
- ホストデバイスがキャッシュドロアー近傍に存在するかの判定としてRSSI値を使用する場合、その判定結果だけに基づいて、ユーザー操作を伴わずにキャッシュドロアーをオープンすることは、セキュリティの観点から推奨しません。
- 近傍に存在するかの判定において、電波干渉や遮蔽等により対象Bluetooth LEデバイスを検知できない場合を想定した実装の考慮を推奨いたします。

