commit cdf0fb90231f5b69005188ca147772fd262a5710 Author: wanxf Date: Thu Nov 16 09:47:42 2023 +0800 初始化插件 diff --git a/package.json b/package.json new file mode 100644 index 0000000..b789218 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "ubxredscan", + "version": "1.0.0", + "description": "ubx redscan", + "main": "index.js", + "scripts": { + "test": "" + }, + "author": "", + "license": "ISC" +} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..c57665f --- /dev/null +++ b/plugin.xml @@ -0,0 +1,19 @@ + + + ubxredscan + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/build.gradle b/src/android/build.gradle new file mode 100644 index 0000000..fc06503 --- /dev/null +++ b/src/android/build.gradle @@ -0,0 +1,3 @@ +dependencies { + implementation files('libs/platform_sdk_v4.1.0326.jar') +} diff --git a/src/android/platform_sdk_v4.1.0326.jar b/src/android/platform_sdk_v4.1.0326.jar new file mode 100644 index 0000000..940bfb0 Binary files /dev/null and b/src/android/platform_sdk_v4.1.0326.jar differ diff --git a/src/android/ubxredscan.java b/src/android/ubxredscan.java new file mode 100644 index 0000000..ebae938 --- /dev/null +++ b/src/android/ubxredscan.java @@ -0,0 +1,247 @@ +package cordova.plugin.ubx.redscan; +import static com.ubx.usdk.util.ConvertUtils.bytesToHexString; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.IntentFilter; +import android.device.scanner.configuration.PropertyID; +import android.device.scanner.configuration.Symbology; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Handler; +import android.os.Message; +import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; +import android.preference.ListPreference; +import android.util.Log; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.Toast; +import android.device.ScanManager; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.PluginResult; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * This class echoes a string called from JavaScript. + */ +public class ubxredscan extends CordovaPlugin { + private static final String TAG = "ScanManager"; + + private static final String ACTION_DECODE = ScanManager.ACTION_DECODE; // default action + private static final String ACTION_DECODE_IMAGE_REQUEST = "action.scanner_capture_image"; + private static final String ACTION_CAPTURE_IMAGE = "scanner_capture_image_result"; + private static final String BARCODE_LENGTH_TAG = ScanManager.BARCODE_LENGTH_TAG; + private static final String DECODE_DATA_TAG = ScanManager.DECODE_DATA_TAG; + + private ScanManager mScanManager = null; + private static boolean mScanEnable = true; + private static boolean mScanCaptureImageShow = false; + + private static final int[] SCAN_KEYCODE = {520, 521, 522, 523}; + private CallbackContext callbackContext; + private BroadcastReceiver mReceiver = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + try{ + String action = intent.getAction(); + LogI("onReceive , action:" + action); + byte[] barcode = intent.getByteArrayExtra(DECODE_DATA_TAG); + int barcodeLen = intent.getIntExtra(BARCODE_LENGTH_TAG, 0); + if (mScanCaptureImageShow) { + context.sendBroadcast(new Intent(ACTION_DECODE_IMAGE_REQUEST)); + } + String scanResult = new String(barcode, 0, barcodeLen); + JSONObject content = new JSONObject(); + content.put("result", scanResult); + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK,content); + pluginResult.setKeepCallback(false); // Keep callback + callbackContext.sendPluginResult(pluginResult); + } catch (JSONException e) { + Log.w(TAG, e); + callbackContext.error(e.getMessage()); + } + + } + }; + + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + if (action.equals("initredscan")) { + this.initScan(callbackContext); + return true; + } + if (action.equals("startredscan")) { + this.startDecode(callbackContext); + return true; + } + if (action.equals("stopredscan")) { + this.stopDecode(callbackContext); + return true; + } + if (action.equals("closeScanner")) { + this.closeScanner(callbackContext); + return true; + } + if (action.equals("lockTrigger")) { + this.lockTrigger(callbackContext); + return true; + } + if (action.equals("unlockTrigger")) { + this.unlockTrigger(callbackContext); + return true; + } + if (action.equals("resetScannerParameters")) { + this.resetScannerParameters(callbackContext); + return true; + } + + return false; + } + + @Override + public void onDestroy() { + if (mScanManager != null) { + mScanManager = null; + } + cordova.getContext().unregisterReceiver(mReceiver); + } + + private void initScan(CallbackContext callbackContext) { + JSONObject content = new JSONObject(); + try { + mScanManager = new ScanManager(); + boolean powerOn = mScanManager.getScannerState(); + if (!powerOn) { + powerOn = mScanManager.openScanner(); + + if (!powerOn) { + content.put("result", "init fail"); + PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR,content); + pluginResult.setKeepCallback(false); // Keep callback + callbackContext.sendPluginResult(pluginResult); + return; + } + } + mScanManager.switchOutputMode(0); + IntentFilter filter = new IntentFilter(); + int[] idbuf = new int[]{PropertyID.WEDGE_INTENT_ACTION_NAME, PropertyID.WEDGE_INTENT_DATA_STRING_TAG}; + String[] value_buf = mScanManager.getParameterString(idbuf); + if (value_buf != null && value_buf[0] != null && !value_buf[0].equals("")) { + filter.addAction(value_buf[0]); + } else { + filter.addAction(ACTION_DECODE); + } + filter.addAction(ACTION_CAPTURE_IMAGE); + cordova.getContext().registerReceiver(mReceiver , filter); + content.put("result", "init success"); + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK,content); + pluginResult.setKeepCallback(false); // Keep callback + callbackContext.sendPluginResult(pluginResult); + } catch (JSONException e) { + Log.w(TAG, e); + callbackContext.error(e.getMessage()); + } + + + } + + private void startDecode(CallbackContext callbackContext) { + if (!mScanEnable) { + LogI("startDecode ignore, Scan enable:" + mScanEnable); + return; + } + boolean lockState = getlockTriggerState(); + if (lockState) { + LogI("startDecode ignore, Scan lockTrigger state:" + lockState); + return; + } + if (mScanManager != null) { + this.callbackContext = callbackContext; + mScanManager.openScanner(); + mScanManager.startDecode(); + } + } + + private void stopDecode(CallbackContext callbackContext) { + if (!mScanEnable) { + LogI("stopDecode ignore, Scan enable:" + mScanEnable); + return; + } + if (mScanManager != null) { + mScanManager.stopDecode(); + callbackContext.success("OK"); + } + } + + private void closeScanner(CallbackContext callbackContext) { + if (!mScanEnable) { + LogI("stopDecode ignore, Scan enable:" + mScanEnable); + return; + } + if (mScanManager != null) { + mScanManager.closeScanner(); + callbackContext.success("OK"); + } + } + + private void lockTrigger(CallbackContext callbackContext) { + if (!mScanEnable) { + LogI("stopDecode ignore, Scan enable:" + mScanEnable); + return; + } + if (mScanManager != null) { + mScanManager.lockTrigger(); + callbackContext.success("OK"); + } + } + + private void unlockTrigger(CallbackContext callbackContext) { + if (!mScanEnable) { + LogI("stopDecode ignore, Scan enable:" + mScanEnable); + return; + } + if (mScanManager != null) { + mScanManager.unlockTrigger(); + callbackContext.success("OK"); + } + } + + private void resetScannerParameters(CallbackContext callbackContext) { + if (!mScanEnable) { + LogI("stopDecode ignore, Scan enable:" + mScanEnable); + return; + } + if (mScanManager != null) { + mScanManager.resetScannerParameters(); + callbackContext.success("OK"); + } + } + + private boolean getlockTriggerState() { + boolean state = mScanManager.getTriggerLockState(); + return state; + } + + private void LogI(String msg) { + android.util.Log.i(TAG, msg); + } + +} diff --git a/www/ubxredscan.js b/www/ubxredscan.js new file mode 100644 index 0000000..4f2c658 --- /dev/null +++ b/www/ubxredscan.js @@ -0,0 +1,27 @@ +var exec = require('cordova/exec'); + +exports.startredscan = function(success, error) { + exec(success, error, "ubxredscan", "startredscan", []); +}; + +exports.stopredscan = function(success, error) { + exec(success, error, "ubxredscan", "stopredscan", []); +}; + +exports.initredscan = function(success, error) { + exec(success, error, "ubxredscan", "initredscan", []); +}; + +exports.closeScanner = function(success, error) { + exec(success, error, "ubxredscan", "closeScanner", []); +}; + +exports.lockTrigger = function(success, error) { + exec(success, error, "ubxredscan", "lockTrigger", []); +}; +exports.unlockTrigger = function(success, error) { + exec(success, error, "ubxredscan", "unlockTrigger", []); +}; +exports.resetScannerParameters = function(success, error) { + exec(success, error, "ubxredscan", "resetScannerParameters", []); +};