340 lines
9.1 KiB
Java
340 lines
9.1 KiB
Java
package cordova.plugin.rfid;
|
||
|
||
import android.util.Log;
|
||
|
||
import com.ubx.usdk.RFIDSDKManager;
|
||
import com.ubx.usdk.rfid.RfidManager;
|
||
import com.ubx.usdk.rfid.aidl.IRfidCallback;
|
||
import com.ubx.usdk.util.SoundTool;
|
||
|
||
import org.apache.cordova.CallbackContext;
|
||
import org.apache.cordova.CordovaPlugin;
|
||
import org.apache.cordova.PluginResult;
|
||
import org.json.JSONArray;
|
||
import org.json.JSONException;
|
||
import org.json.JSONObject;
|
||
|
||
|
||
/**
|
||
* 优博讯超高频(UHF)RFID 读 EPC 插件
|
||
*/
|
||
public class UbxRfid extends CordovaPlugin {
|
||
|
||
private final String TAG = "UbxUHF";
|
||
private RfidManager rfidManager;
|
||
private ScanCallback callback;
|
||
public boolean RFID_INIT_STATUS = false;
|
||
public int readerType = 0;
|
||
private byte session = 0; // 值有 0、1、2、3 ,一般用 0或者1.
|
||
private SoundTool soundTool;
|
||
private CallbackContext callbackContext;
|
||
private boolean beep = true; // 是否响铃
|
||
|
||
@Override
|
||
protected void pluginInitialize() {
|
||
super.pluginInitialize();
|
||
soundTool = SoundTool.getInstance(cordova.getContext());
|
||
this.callbackContext = null;
|
||
// openScanDevice(); // 放在这里,应用加载时会很慢
|
||
}
|
||
|
||
private boolean openScanDevice() {
|
||
boolean connect = RFIDSDKManager.getInstance().connect();
|
||
|
||
if (connect) {
|
||
Log.d(TAG, "initRfid() success.");
|
||
RFID_INIT_STATUS = true;
|
||
rfidManager = RFIDSDKManager.getInstance().getRfidManager();
|
||
readerType = rfidManager.getReaderType();//80为短距,其他为长距
|
||
Log.d(TAG, "initRfid: firmware = " + rfidManager.getFirmwareVersion());
|
||
Log.d(TAG, "initRfid: ReaderType = " + readerType);
|
||
return true;
|
||
} else {
|
||
Log.d(TAG, "initRfid fail.");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
class ScanCallback implements IRfidCallback {
|
||
@Override
|
||
public void onInventoryTag(String epc, final String tid, final String rssi) {
|
||
Log.e(TAG, "onInventoryTag:............... epc:" + epc + ", tid:" + tid + ", rssi:" + rssi);
|
||
if (callbackContext == null) {
|
||
Log.w(TAG, "callbackContext is NULL");
|
||
return;
|
||
}
|
||
if (beep) {
|
||
soundTool.playBeep(1);
|
||
}
|
||
JSONObject content = new JSONObject();
|
||
try {
|
||
content.put("epc", epc);
|
||
content.put("tid", tid);
|
||
content.put("rssi", rssi);
|
||
} catch (JSONException e) {
|
||
Log.w(TAG, e);
|
||
callbackContext.error(e.getMessage());
|
||
}
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, content);
|
||
pluginResult.setKeepCallback(true);
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
}
|
||
|
||
/**
|
||
* 盘存结束回调(Inventory Command Operate End)
|
||
*/
|
||
@Override
|
||
public void onInventoryTagEnd() {
|
||
Log.d(TAG, "onInventoryTagEnd()");
|
||
}
|
||
}
|
||
|
||
private void setCallback() {
|
||
if (this.rfidManager != null) {
|
||
if (this.callback == null) {
|
||
this.callback = new ScanCallback();
|
||
this.rfidManager.registerCallback(callback);
|
||
}
|
||
} else {
|
||
Log.e(TAG, "rfidManager NOT exist");
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||
this.callbackContext = callbackContext;
|
||
if (action.equals("searchCard")) {
|
||
this.searchCard();
|
||
return true;
|
||
}
|
||
if (action.equals("startSearchCard")) {
|
||
this.startSearchCard();
|
||
return true;
|
||
}
|
||
if (action.equals("stopSearchCard")) {
|
||
this.stopSearchCard();
|
||
return true;
|
||
}
|
||
if (action.equals("getPower")) {
|
||
this.getPower();
|
||
return true;
|
||
}
|
||
if (action.equals("setPower")) {
|
||
this.setPower(args);
|
||
return true;
|
||
}
|
||
if (action.equals("getDeviceInfo")) {
|
||
this.getDeviceInfo();
|
||
return true;
|
||
}
|
||
if (action.equals("getScanInterval")) {
|
||
this.getScanInterval();
|
||
return true;
|
||
}
|
||
if (action.equals("setScanInterval")) {
|
||
this.setScanInterval(args);
|
||
return true;
|
||
}
|
||
if (action.equals("getBeep")) {
|
||
this.getBeep();
|
||
return true;
|
||
}
|
||
if (action.equals("beepOn")) {
|
||
this.beepOn();
|
||
return true;
|
||
}
|
||
if (action.equals("beepOff")) {
|
||
this.beepOff();
|
||
return true;
|
||
}
|
||
if (action.equals("setBeep")) {
|
||
this.setBeep(args.getBoolean(0));
|
||
return true;
|
||
}
|
||
Log.w(TAG, "Unrecognized action: " + action);
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 开始巡卡
|
||
*/
|
||
private void startSearchCard() {
|
||
if (!this.tryConnect()) {
|
||
return;
|
||
}
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
rfidManager.startInventory(session);
|
||
}
|
||
|
||
private boolean tryConnect() {
|
||
if (!RFID_INIT_STATUS) {
|
||
if (!openScanDevice()) {
|
||
callbackContext.error("RFID 未初始化,可能未连接或不可使用");
|
||
return false;
|
||
}
|
||
}
|
||
setCallback();
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 停止巡卡
|
||
*/
|
||
private void stopSearchCard() {
|
||
if (!this.tryConnect()) {
|
||
return;
|
||
}
|
||
rfidManager.stopInventory();
|
||
callbackContext.success();
|
||
}
|
||
|
||
/**
|
||
* 巡一次卡
|
||
*/
|
||
private void searchCard() {
|
||
if (!this.tryConnect()) {
|
||
return;
|
||
}
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
rfidManager.inventorySingle();
|
||
}
|
||
|
||
/**
|
||
* 获取天线功率
|
||
*/
|
||
private void getPower() {
|
||
if (!this.tryConnect()) {
|
||
return;
|
||
}
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, rfidManager.getOutputPower());
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
}
|
||
|
||
/**
|
||
* 设置天线功率
|
||
*
|
||
* @param message 功率,长距功率范围:0-33,短距版功率范围:0-26
|
||
* @throws JSONException
|
||
*/
|
||
private void setPower(JSONArray message) throws JSONException {
|
||
if (!this.tryConnect()) {
|
||
return;
|
||
}
|
||
int power = message.getInt(0);
|
||
int readerType = rfidManager.getReaderType();
|
||
int maxPower = readerType == 80 ? 26 : 33; // 80 短矩,其它 长矩
|
||
if (power > maxPower) {
|
||
power = maxPower;
|
||
} else if (power < 0) {
|
||
power = 0;
|
||
}
|
||
rfidManager.setOutputPower((byte) power);
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
}
|
||
|
||
/**
|
||
* 获取巡卡间隔
|
||
*/
|
||
private void getScanInterval() {
|
||
if (!this.tryConnect()) {
|
||
return;
|
||
}
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, rfidManager.getScanInterval());
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
}
|
||
|
||
/**
|
||
* 设置巡卡间隔
|
||
* @param message 巡卡间隔,一般在 0-60 之间
|
||
* @throws JSONException
|
||
*/
|
||
private void setScanInterval(JSONArray message) throws JSONException {
|
||
if (!this.tryConnect()) {
|
||
return;
|
||
}
|
||
int interval = message.getInt(0);
|
||
if (interval > 60) {
|
||
interval = 60;
|
||
} else if (interval < 0) {
|
||
interval = 0;
|
||
}
|
||
rfidManager.setScanInterval(interval);
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
}
|
||
|
||
/**
|
||
* 设置读卡响铃
|
||
*/
|
||
private void beepOn() {
|
||
this.setBeep(true);
|
||
}
|
||
|
||
/**
|
||
* 关闭读卡响铃
|
||
*/
|
||
private void beepOff() {
|
||
this.setBeep(false);
|
||
}
|
||
|
||
private void setBeep(boolean beep) {
|
||
this.beep = beep;
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
}
|
||
|
||
/**
|
||
* 获取是否响铃
|
||
*/
|
||
private void getBeep() {
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, this.beep);
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
}
|
||
|
||
/**
|
||
* 获取设备信息
|
||
*/
|
||
private void getDeviceInfo() {
|
||
if (!this.tryConnect()) {
|
||
return;
|
||
}
|
||
JSONObject content = new JSONObject();
|
||
try {
|
||
content.put("type", rfidManager.getReaderType() == 80 ? "短矩" : "长矩");
|
||
content.put("firmware", rfidManager.getFirmwareVersion());
|
||
content.put("power", rfidManager.getOutputPower());
|
||
content.put("scanInterval", rfidManager.getScanInterval());
|
||
content.put("beep", this.beep);
|
||
} catch (JSONException e) {
|
||
Log.w(TAG, e);
|
||
callbackContext.error(e.getMessage());
|
||
return;
|
||
}
|
||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, content);
|
||
pluginResult.setKeepCallback(true); // Keep callback
|
||
callbackContext.sendPluginResult(pluginResult);
|
||
}
|
||
|
||
@Override
|
||
public void onDestroy() {
|
||
if (rfidManager != null) {
|
||
rfidManager.unregisterCallback(callback);
|
||
rfidManager.disConnect();
|
||
rfidManager.release();
|
||
callback = null;
|
||
}
|
||
}
|
||
|
||
}
|