123 lines
3.7 KiB
Java
123 lines
3.7 KiB
Java
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;
|
|
private boolean receiverRegisted = false;
|
|
|
|
// BroadcastReceiver to receiver scan data
|
|
private BroadcastReceiver receiver = new BroadcastReceiver() {
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
if (callbackContext == null) {
|
|
return;
|
|
}
|
|
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() {
|
|
Log.d(TAG, "RedLightScan registerReceiver");
|
|
scanUtil = new ScanUtil(cordova.getActivity());
|
|
//we must set mode to 0 : BroadcastReceiver mode
|
|
scanUtil.setScanMode(0);
|
|
IntentFilter filter = new IntentFilter();
|
|
filter.addAction("com.rfid.SCAN");
|
|
webView.getContext().registerReceiver(receiver, filter);
|
|
this.receiverRegisted = true;
|
|
this.startScan();
|
|
Log.d(TAG, "RedLightScan receiver registed");
|
|
}
|
|
|
|
private void unregisterReceiver() {
|
|
Log.d(TAG, "RedLightScan unregisterReceiver");
|
|
if (scanUtil != null) {
|
|
scanUtil.setScanMode(1);
|
|
scanUtil.close();
|
|
scanUtil = null;
|
|
}
|
|
if (this.receiverRegisted) {
|
|
webView.getContext().unregisterReceiver(receiver);
|
|
this.receiverRegisted = false;
|
|
Log.d(TAG, "RedLightScan receiver unregisted");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
Log.d(TAG, "RedLightScan Destroy");
|
|
super.onDestroy();
|
|
this.unregisterReceiver();
|
|
}
|
|
|
|
}
|