初始化插件

This commit is contained in:
万昕放 2023-11-16 09:47:42 +08:00
commit cdf0fb9023
6 changed files with 307 additions and 0 deletions

11
package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "ubxredscan",
"version": "1.0.0",
"description": "ubx redscan",
"main": "index.js",
"scripts": {
"test": ""
},
"author": "",
"license": "ISC"
}

19
plugin.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-ubx-redscan" version="1.0.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>ubxredscan</name>
<js-module name="ubxredscan" src="www/ubxredscan.js">
<clobbers target="cordova.plugins.ubxredscan" />
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="ubxredscan">
<param name="android-package" value="cordova.plugin.ubx.redscan.ubxredscan" />
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml"></config-file>
<lib-file src="src/android/platform_sdk_v4.1.0326.jar"/>
<source-file src="src/android/ubxredscan.java" target-dir="src/cordova/plugin/ubx/redscan" />
</platform>
</plugin>

3
src/android/build.gradle Normal file
View File

@ -0,0 +1,3 @@
dependencies {
implementation files('libs/platform_sdk_v4.1.0326.jar')
}

Binary file not shown.

247
src/android/ubxredscan.java Normal file
View File

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

27
www/ubxredscan.js Normal file
View File

@ -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", []);
};