This commit is contained in:
巫翔 2021-09-24 11:50:53 +08:00
commit c3767c674b
5 changed files with 279 additions and 0 deletions

18
package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "redlightscan-cordova-plugin",
"version": "1.0.0",
"description": "red light scanner plugin",
"cordova": {
"id": "redlightscan-cordova-plugin",
"platforms": []
},
"repository": {
"type": "git",
"url": "http://m.shuto.cn:8680/center/redlightscan"
},
"keywords": [
"ecosystem:cordova"
],
"author": "wux",
"license": "ISC"
}

21
plugin.xml Normal file
View File

@ -0,0 +1,21 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="redlightscan-cordova-plugin" version="1.0.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>RedLightScan Cordova Plugin</name>
<js-module name="redlightscan-cordova-plugin" src="www/redlightscan.js">
<clobbers target="cordova.plugins.redlightscan" />
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="RedLightScan">
<param name="android-package" value="cn.shuto.plugin.redlightscan.RedLightScan" />
<param name="onload" value="true" />
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml">
</config-file>
<source-file src="src/android/RedLightScan.java" target-dir="src/cn/shuto/plugin/redlightscan" />
<source-file src="src/android/ScanUtil.java" target-dir="src/com/hdhe/scan" />
</platform>
</plugin>

View File

@ -0,0 +1,125 @@
package cn.shuto.plugin.redlightscan;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import com.hdhe.scan.ScanUtil;
/**
* 红光扫码
*/
public class RedLightScan extends CordovaPlugin {
private static String TAG = RedLightScan.class.getSimpleName();
private ScanUtil scanUtil;
private CallbackContext callbackContext;
// BroadcastReceiver to receiver scan data
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
byte[] data = intent.getByteArrayExtra("data");
if (data != null) {
String barcode = new String(data);
PluginResult result = new PluginResult(PluginResult.Status.OK, barcode);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
}
};
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
switch (action) {
case "startScan":
this.startScan();
break;
case "stopScan":
this.stopScan();
callbackContext.success();
break;
case "registerReceiver":
this.registerReceiver();
break;
case "unregisterReceiver":
this.unregisterReceiver();
callbackContext.success();
break;
default:
callbackContext.error("UNKNOWN command: " + action);
return false;
}
return true;
}
private void startScan() {
if (scanUtil != null) {
scanUtil.scan();
}
}
private void stopScan() {
if (scanUtil != null) {
scanUtil.stopScan();
}
}
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction("com.rfid.SCAN");
webView.getContext().registerReceiver(receiver, filter);
}
private void unregisterReceiver() {
webView.getContext().unregisterReceiver(receiver);
}
@Override
public void onResume(boolean multitasking) {
Log.d(TAG, "RedLightScan Resume");
super.onResume(multitasking);
if (scanUtil == null) {
scanUtil = new ScanUtil(cordova.getActivity());
//we must set mode to 0 : BroadcastReceiver mode
scanUtil.setScanMode(0);
}
this.registerReceiver();
}
@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
Log.d(TAG, "RedLightScan Pause");
if (scanUtil != null) {
scanUtil.setScanMode(1);
scanUtil.close();
scanUtil = null;
}
this.unregisterReceiver();
}
@Override
public void onDestroy() {
Log.d(TAG, "RedLightScan Destroy");
super.onDestroy();
this.unregisterReceiver();
}
}

99
src/android/ScanUtil.java Normal file
View File

@ -0,0 +1,99 @@
package com.hdhe.scan;
import android.content.Context;
import android.content.Intent;
/**
* @author Administrator
* @date 2018/4/19
*/
public class ScanUtil {
/**
* Open scan service
*/
private final String ACTION_SCAN_INIT = "com.rfid.SCAN_INIT";
/**
* Scanning
*/
private final String ACTION_SCAN = "com.rfid.SCAN_CMD";
/**
* Stop Scanning
*/
private static final String ACTION_STOP_SCAN = "com.rfid.STOP_SCAN";
/**
* Close scan service
*/
private final String ACTION_CLOSE_SCAN = "com.rfid.CLOSE_SCAN";
/**
* Scan result output mode, 0 -- BroadcastReceiver mode; 1 -- Focus input mode (default)
*/
private final String ACTION_SET_SCAN_MODE = "com.rfid.SET_SCAN_MODE";
/**
* Scan timeout (Value:1000,2000,3000,4000,5000,6000,7000,8000,9000,10000)
*/
private final String ACTION_SCAN_TIME = "com.rfid.SCAN_TIME";
private Context context;
/**
* Initialize ScanUtil and open scan service
* @param context Context
*/
public ScanUtil(Context context) {
this.context = context;
Intent intent = new Intent();
intent.setAction(ACTION_SCAN_INIT);
context.sendBroadcast(intent);
}
/**
* Start Scanning
*/
public void scan() {
Intent intent = new Intent();
intent.setAction(ACTION_SCAN);
context.sendBroadcast(intent);
}
/**
* Stop Scanning
*/
public void stopScan() {
Intent intent = new Intent();
intent.setAction(ACTION_STOP_SCAN);
context.sendBroadcast(intent);
}
/**
* Set the scan result output mode
* @param mode 0 -- BroadcastReceiver mode; 1 -- Focus input mode (default)
*/
public void setScanMode(int mode) {
Intent intent = new Intent();
intent.setAction(ACTION_SET_SCAN_MODE);
intent.putExtra("mode", mode);
context.sendBroadcast(intent);
}
/**
* Close scan service
*/
public void close() {
Intent toKillService = new Intent();
toKillService.setAction(ACTION_CLOSE_SCAN);
context.sendBroadcast(toKillService);
}
/**
* Set scan timeout
* @param timeout Value:1000,2000,3000,4000,5000(default),6000,7000,8000,9000,10000
*/
public void setTimeout(String timeout){
Intent intent = new Intent();
intent.setAction(ACTION_SCAN_TIME);
intent.putExtra("time", timeout);
context.sendBroadcast(intent);
}
}

16
www/redlightscan.js Normal file
View File

@ -0,0 +1,16 @@
var exec = require('cordova/exec');
module.exports = {
startScan(success, error) {
exec(success, error, 'RedLightScan', 'startScan', []);
},
stopScan(success, error) {
exec(success, error, 'RedLightScan', 'stopScan', []);
},
registerReceiver(success, error) {
exec(success, error, 'RedLightScan', 'registerReceiver', []);
},
unregisterReceiver(success, error) {
exec(success, error, 'RedLightScan', 'unregisterReceiver', []);
}
};