diff --git a/README.md b/README.md index ab55d56..42e3810 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,10 @@ DataWedge.addListener('scan', event => { * [`disableScanner()`](#disablescanner) * [`startScanning()`](#startscanning) * [`stopScanning()`](#stopscanning) +* [`isScannerAvailable()`](#isscanneravailable) * [`addListener('scan', ...)`](#addlistenerscan-) * [`__registerReceiver()`](#__registerreceiver) +* [`__enumerateScanners()`](#__enumeratescanners) * [Interfaces](#interfaces) * [Type Aliases](#type-aliases) @@ -139,6 +141,21 @@ Broadcasts intent action with `.SOFT_SCAN_TRIGGER` extra set to `STOP_SCANNING` -------------------- +### isScannerAvailable() + +```typescript +isScannerAvailable() => Promise +``` + +Returns boolean if scanner is connected + +**Returns:** Promise<boolean> + +**Since:** 0.1.3 + +-------------------- + + ### addListener('scan', ...) ```typescript @@ -167,7 +184,7 @@ Listen for successful barcode readings __registerReceiver() => Promise ``` -Internal method to register intent broadcast receiver +Internal method to register intent broadcast receiver THIS METHOD IS FOR INTERNAL USE ONLY **Since:** 0.1.3 @@ -175,6 +192,20 @@ THIS METHOD IS FOR INTERNAL USE ONLY -------------------- +### __enumerateScanners() + +```typescript +__enumerateScanners() => Promise +``` + +Internal method to enumerate through the receivers +THIS METHOD IS FOR INTERNAL USE ONLY + +**Since:** 0.1.4 + +-------------------- + + ### Interfaces diff --git a/android/src/main/java/com/jkbz/capacitor/datawedge/DataWedge.java b/android/src/main/java/com/jkbz/capacitor/datawedge/DataWedge.java index b6580ea..d76d0fb 100644 --- a/android/src/main/java/com/jkbz/capacitor/datawedge/DataWedge.java +++ b/android/src/main/java/com/jkbz/capacitor/datawedge/DataWedge.java @@ -54,5 +54,13 @@ public Intent stopScanning() { return intent; } + + public Intent enumerateScanners(){ + Intent intent = new Intent(); + intent.setAction("com.symbol.datawedge.api.ACTION"); + intent.putExtra("com.symbol.datawedge.api.ENUMERATE_SCANNERS", ""); + + return intent; + } } diff --git a/android/src/main/java/com/jkbz/capacitor/datawedge/DataWedgePlugin.java b/android/src/main/java/com/jkbz/capacitor/datawedge/DataWedgePlugin.java index 6f03964..a6ad254 100644 --- a/android/src/main/java/com/jkbz/capacitor/datawedge/DataWedgePlugin.java +++ b/android/src/main/java/com/jkbz/capacitor/datawedge/DataWedgePlugin.java @@ -14,6 +14,9 @@ import android.content.ActivityNotFoundException; import android.util.Log; +import android.os.Bundle; + +import java.util.ArrayList; @CapacitorPlugin(name = "DataWedge") @@ -66,13 +69,13 @@ public void disableScanner(PluginCall call) { @PluginMethod public void startScanning(PluginCall call) { - Intent intent = implementation.startScanning(); + Intent intent = implementation.startScanning(); - try { + try { broadcast(intent); - } catch (ActivityNotFoundException e) { + } catch (ActivityNotFoundException e) { call.reject("DataWedge is not installed or not running"); - } + } } @PluginMethod @@ -87,7 +90,24 @@ public void stopScanning(PluginCall call) { } @PluginMethod - public void __registerReceiver(PluginCall call) { + public boolean isScannerAvailable(PluginCall call) { + return isDeviceAvailable; + } + + + @PluginMethod + public void __enumerateScanners(PluginCall call) { + Intent intent = implementation.enumerateScanners(); + + try { + broadcast(intent); + } catch(ActivityNotFoundException e) { + call.reject("DataWedge is not installed or not running"); + } + } + + @PluginMethod + public void __registerReceiver(PluginCall call) { if (isReceiverRegistered) return; Context context = getBridge().getContext(); @@ -105,6 +125,8 @@ private void broadcast(Intent intent) { context.sendBroadcast(intent); } + private static final String TAG = "MyActivity"; + public boolean isDeviceAvailable = false; private boolean isReceiverRegistered = false; private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override @@ -113,6 +135,30 @@ public void onReceive(Context context, Intent intent) { if (!action.equals(DataWedge.DATAWEDGE_INPUT_FILTER)) return; + if(action.equals("com.symbol.datawedge.api.RESULT_ACTION")){ + // + // enumerate scanners + // + if(intent.hasExtra("com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS")) { + ArrayList scannerList = (ArrayList) intent.getSerializableExtra("com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS"); + if((scannerList != null) && (scannerList.size() > 0)) { + for (Bundle bunb : scannerList){ + String[] entry = new String[4]; + entry[0] = bunb.getString("SCANNER_NAME"); + entry[1] = bunb.getBoolean("SCANNER_CONNECTION_STATE")+""; + entry[2] = bunb.getInt("SCANNER_INDEX")+""; + + entry[3] = bunb.getString("SCANNER_IDENTIFIER"); + + Log.d(TAG, "Scanner:" + entry[0] + " Connection:" + entry[1] + " Index:" + entry[2] + " ID:" + entry[3]); + if(entry[1] == "true") { + isDeviceAvailable = true; + } + } + } + } + } + try { String data = intent.getStringExtra("com.symbol.datawedge.data_string"); diff --git a/src/definitions.ts b/src/definitions.ts index ab4cb2b..f7ab97b 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -3,7 +3,7 @@ import type { PluginListenerHandle } from '@capacitor/core'; export interface ScanListenerEvent { /** * Data of barcode - * + * * @since 0.1.0 */ data: string; @@ -12,12 +12,11 @@ export interface ScanListenerEvent { export type ScanListener = (state: ScanListenerEvent) => void; export interface DataWedgePlugin { - /** * Enables DataWedge * - * Broadcasts intent action with `.ENABLE_DATAWEDGE` extra set to `true` - * + * Broadcasts intent action with `.ENABLE_DATAWEDGE` extra set to `true` + * * @since 0.0.3 */ enable(): Promise; @@ -26,7 +25,7 @@ export interface DataWedgePlugin { * Disables DataWedge * * Broadcasts intent action with `.ENABLE_DATAWEDGE` extra set to `false` - * + * * @since 0.0.3 */ disable(): Promise; @@ -35,7 +34,7 @@ export interface DataWedgePlugin { * Enables physical scanner * * Broadcasts intent action with `.SCANNER_INPUT_PLUGIN` extra set to `ENABLE_PLUGIN` - * + * * @since 0.0.3 */ enableScanner(): Promise; @@ -44,7 +43,7 @@ export interface DataWedgePlugin { * Disables physical scanner * * Broadcasts intent action with `.SCANNER_INPUT_PLUGIN` extra set to `DISABLE_PLUGIN` - * + * * @since 0.0.3 */ disableScanner(): Promise; @@ -68,23 +67,39 @@ export interface DataWedgePlugin { stopScanning(): Promise; /** - * Listen for successful barcode readings - * - * ***Notice:*** Requires intent action to be set to `com.capacitor.datawedge.RESULT_ACTION` in current DataWedge profile (it may change in the future) - * - * @since 0.1.0 - */ + * Returns boolean if scanner is connected + * + * @since 0.1.3 + */ + isScannerAvailable(): Promise; + + /** + * Listen for successful barcode readings + * + * ***Notice:*** Requires intent action to be set to `com.capacitor.datawedge.RESULT_ACTION` in current DataWedge profile (it may change in the future) + * + * @since 0.1.0 + */ addListener( eventName: 'scan', - listenerFunc: ScanListener + listenerFunc: ScanListener, ): Promise & PluginListenerHandle; /** - * Internal method to register intent broadcast receiver + * Internal method to register intent broadcast receiver * THIS METHOD IS FOR INTERNAL USE ONLY - * + * * @since 0.1.3 * @private */ __registerReceiver(): Promise; + + /** + * Internal method to enumerate through the receivers + * THIS METHOD IS FOR INTERNAL USE ONLY + * + * @since 0.1.4 + * @private + */ + __enumerateScanners(): Promise; } diff --git a/src/index.ts b/src/index.ts index e8a9262..80c83c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ const DataWedge = registerPlugin('DataWedge', { if (Capacitor.isNativePlatform()) { DataWedge.__registerReceiver(); + DataWedge.__enumerateScanners(); } export * from './definitions'; diff --git a/src/web.ts b/src/web.ts index 1e74ab6..1ccd8bb 100644 --- a/src/web.ts +++ b/src/web.ts @@ -27,7 +27,15 @@ export class DataWedgeWeb extends WebPlugin implements DataWedgePlugin { throw 'DataWedge is not supported on web'; } + async isScannerAvailable(): Promise { + throw 'DataWedge is not supported on web'; + } + async __registerReceiver(): Promise { // no-op } + + async __enumerateScanners(): Promise { + //no-op + } }