commit c3767c674b3493e42f8cd262454c456123a23705 Author: wuxiang Date: Fri Sep 24 11:50:53 2021 +0800 init diff --git a/package.json b/package.json new file mode 100644 index 0000000..f11e05c --- /dev/null +++ b/package.json @@ -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" +} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..bb3a343 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,21 @@ + + + RedLightScan Cordova Plugin + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/RedLightScan.java b/src/android/RedLightScan.java new file mode 100644 index 0000000..b3cd5df --- /dev/null +++ b/src/android/RedLightScan.java @@ -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(); + } + +} diff --git a/src/android/ScanUtil.java b/src/android/ScanUtil.java new file mode 100644 index 0000000..5fd81dd --- /dev/null +++ b/src/android/ScanUtil.java @@ -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); + } +} diff --git a/www/redlightscan.js b/www/redlightscan.js new file mode 100644 index 0000000..433c3a1 --- /dev/null +++ b/www/redlightscan.js @@ -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', []); + } +};