UHF 插件
This commit is contained in:
97
README.md
Normal file
97
README.md
Normal file
@@ -0,0 +1,97 @@
|
||||
# RFID 插件
|
||||
应用后台运行时,UHF会调用stopInventory()停止读卡.
|
||||
|
||||
## 安装插件
|
||||
`cordova plugin add git+https://m.shuto.cn:8681/center/cordova-plugin-uhf.git`
|
||||
|
||||
## 引入插件
|
||||
`declare const cordova: any;`
|
||||
|
||||
## 方法
|
||||
|
||||
+ **open()**: 打开UHF
|
||||
```
|
||||
cordova.plugin.uhf.open({
|
||||
success: (res) => {
|
||||
console.log(res);
|
||||
},
|
||||
error: err => {
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
+ **close()**: 关闭UHF
|
||||
```
|
||||
cordova.plugin.uhf.close({
|
||||
success: (res) => {
|
||||
console.log(res);
|
||||
},
|
||||
error: err => {
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
+ **getPower()**: 获取UHF功率
|
||||
```
|
||||
cordova.plugin.uhf.getPower({
|
||||
success: (res) => {
|
||||
console.log(res);
|
||||
},
|
||||
error: err => {
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
+ **setPower()**: 设置UHF功率,power有效值:5~33
|
||||
```
|
||||
cordova.plugin.uhf.setPower({
|
||||
power: 10,
|
||||
success: (res) => {
|
||||
console.log(res);
|
||||
},
|
||||
error: err => {
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
+ **inventory()**: 开始UHF寻卡.sound:是否开启寻卡声音,true或false,默认开启
|
||||
```
|
||||
cordova.plugin.uhf.inventory({
|
||||
sound: false,
|
||||
success: (res) => {
|
||||
console.log(res);
|
||||
},
|
||||
error: err => {
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
+ **stopInventory()**: 停止UHF寻卡
|
||||
```
|
||||
cordova.plugin.uhf.stopInventory({
|
||||
success: (res) => {
|
||||
console.log(res);
|
||||
},
|
||||
error: err => {
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
+ **inventoryOnce()**: 单次UHF寻卡.sound:是否开启寻卡声音,true或false,默认开启
|
||||
```
|
||||
cordova.plugin.uhf.inventoryOnce({
|
||||
sound: false,
|
||||
success: (res) => {
|
||||
console.log(res);
|
||||
},
|
||||
error: err => {
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
```
|
||||
17
package.json
Normal file
17
package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "cordova-plugin-uhf",
|
||||
"version": "1.0.0",
|
||||
"description": "RFID reader plugin",
|
||||
"cordova": {
|
||||
"id": "cordova-plugin-uhf",
|
||||
"platforms": [
|
||||
"android"
|
||||
]
|
||||
},
|
||||
"keywords": [
|
||||
"cordova",
|
||||
"plugin"
|
||||
],
|
||||
"author": "yc",
|
||||
"license": "Apache-2.0"
|
||||
}
|
||||
24
plugin.xml
Normal file
24
plugin.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<plugin id="cordova-plugin-uhf" version="0.0.1"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://apache.org/cordova/ns/plugins/1.0">
|
||||
<name>RFID UHF Plugin</name>
|
||||
<js-module name="cordova-plugin-uhf" src="www/uhf.js">
|
||||
<clobbers target="cordova.plugin.uhf"/>
|
||||
</js-module>
|
||||
<platform name="android">
|
||||
<config-file parent="/*" target="res/xml/config.xml">
|
||||
<feature name="UHF">
|
||||
<param name="android-package" value="cordova.plugin.uhf.UHF"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
<resource-file src="src/android/res/raw/scan.ogg"
|
||||
target="res/raw/scan.ogg" />
|
||||
|
||||
<source-file src="src/android/cordova/plugin/uhf/UHF.java"
|
||||
target-dir="cordova/plugin/uhf"/>
|
||||
|
||||
<lib-file src="src/android/libsref/uhf.jar" />
|
||||
|
||||
</platform>
|
||||
</plugin>
|
||||
213
src/android/cordova/plugin/uhf/UHF.java
Normal file
213
src/android/cordova/plugin/uhf/UHF.java
Normal file
@@ -0,0 +1,213 @@
|
||||
package cordova.plugin.uhf;
|
||||
|
||||
import android.content.Context;
|
||||
import android.media.SoundPool;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
import com.seuic.uhf.EPC;
|
||||
import com.seuic.uhf.UHFService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UHF extends CordovaPlugin {
|
||||
private UHFService uhfService;
|
||||
private boolean isOpen = true;
|
||||
private boolean inventoryFlag = false;
|
||||
private static SoundPool soundPool;
|
||||
private static int soundID;
|
||||
|
||||
private int getRawResourceId(String resourceName) {
|
||||
Context context = cordova.getActivity().getApplicationContext();
|
||||
return context.getResources().getIdentifier(resourceName, "raw", context.getPackageName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void pluginInitialize() {
|
||||
super.pluginInitialize();
|
||||
this.uhfService = UHFService.getInstance();
|
||||
this.isOpen = this.uhfService.isOpen();
|
||||
soundPool = new SoundPool.Builder().setMaxStreams(1).build();
|
||||
soundID = soundPool.load(this.cordova.getContext(), getRawResourceId("scan"), 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
switch (action) {
|
||||
case "open":
|
||||
this.open(callbackContext);
|
||||
return true;
|
||||
case "close":
|
||||
this.close(callbackContext);
|
||||
return true;
|
||||
case "getPower":
|
||||
this.getPower(callbackContext);
|
||||
return true;
|
||||
case "setPower":
|
||||
this.setPower(args, callbackContext);
|
||||
return true;
|
||||
case "inventory":
|
||||
this.inventory(callbackContext, args);
|
||||
return true;
|
||||
case "stopInventory":
|
||||
this.stopInventory(callbackContext);
|
||||
return true;
|
||||
case "inventoryOnce":
|
||||
this.inventoryOnce(callbackContext, args);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void open() {
|
||||
if(this.uhfService == null) {
|
||||
this.uhfService = UHFService.getInstance();
|
||||
}
|
||||
this.isOpen = this.uhfService.open();
|
||||
}
|
||||
|
||||
private void open(CallbackContext callbackContext) {
|
||||
this.open();
|
||||
if(this.isOpen) {
|
||||
callbackContext.success("打开UHF成功!");
|
||||
} else {
|
||||
callbackContext.error("打开UHF失败!");
|
||||
}
|
||||
}
|
||||
|
||||
private void close(CallbackContext callbackContext) {
|
||||
if(this.uhfService != null) {
|
||||
this.uhfService.close();
|
||||
this.uhfService = null;
|
||||
this.isOpen = false;
|
||||
}
|
||||
callbackContext.success("UHF已关闭!");
|
||||
}
|
||||
|
||||
private void getPower(CallbackContext callbackContext) {
|
||||
if(!this.isOpen) {
|
||||
this.open();
|
||||
}
|
||||
int power = this.uhfService.getPower();
|
||||
callbackContext.success(power);
|
||||
}
|
||||
|
||||
private void setPower(JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if(!this.isOpen) {
|
||||
this.open();
|
||||
}
|
||||
int power = args.getInt(0);
|
||||
if(power < 5) {
|
||||
power = 5;
|
||||
} else if(power > 33) {
|
||||
power = 33;
|
||||
}
|
||||
if (this.uhfService.setPower(power)) {
|
||||
callbackContext.success("功率设置成功:" + power);
|
||||
} else {
|
||||
callbackContext.error("功率设置失败:" + power);
|
||||
}
|
||||
}
|
||||
|
||||
private void inventoryOnce(CallbackContext callbackContext, JSONArray args) throws JSONException {
|
||||
boolean sound = args.getBoolean(0);
|
||||
if(!this.isOpen) {
|
||||
this.open();
|
||||
}
|
||||
EPC epc = new EPC();
|
||||
this.uhfService.inventoryOnce(epc, 500);
|
||||
if(!"".equals(epc.getId()) && sound) {
|
||||
playSound();
|
||||
}
|
||||
callbackContext.success(epc.getId());
|
||||
}
|
||||
|
||||
private void inventory(CallbackContext callbackContext, JSONArray args) throws JSONException {
|
||||
boolean sound = args.getBoolean(0);
|
||||
if(!this.isOpen) {
|
||||
this.open();
|
||||
}
|
||||
this.inventoryFlag = true;
|
||||
this.uhfService.inventoryStart();
|
||||
Thread thread = new InventoryThread(callbackContext, sound);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private void stopInventory(CallbackContext callbackContext) {
|
||||
this.inventoryFlag = false;
|
||||
this.uhfService.inventoryStop();
|
||||
callbackContext.success("停止读卡...");
|
||||
}
|
||||
|
||||
private class InventoryThread extends Thread {
|
||||
|
||||
private CallbackContext cb;
|
||||
private boolean sound;
|
||||
|
||||
public InventoryThread(CallbackContext cb, boolean sound) {
|
||||
super();
|
||||
this.cb = cb;
|
||||
this.sound = sound;
|
||||
}
|
||||
|
||||
public InventoryThread() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
super.run();
|
||||
try {
|
||||
while (inventoryFlag) {
|
||||
List<EPC> epcList = uhfService.getTagIDs();
|
||||
if(!epcList.isEmpty() && this.sound) {
|
||||
playSound();
|
||||
}
|
||||
JSONArray data = new JSONArray();
|
||||
for(EPC epc : epcList) {
|
||||
data.put(epc.getId());
|
||||
}
|
||||
PluginResult pr = new PluginResult(PluginResult.Status.OK, data);
|
||||
pr.setKeepCallback(true);
|
||||
cb.sendPluginResult(pr);
|
||||
Thread.sleep(200);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
cb.error(e.getMessage());
|
||||
} finally {
|
||||
cb.success("已停止读卡!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void playSound() {
|
||||
if(soundPool == null) {
|
||||
soundPool = new SoundPool.Builder().setMaxStreams(1).build();
|
||||
soundID = soundPool.load(this.cordova.getContext(), getRawResourceId("scan"), 1);
|
||||
}
|
||||
soundPool.play(soundID, 1.0f, 1.0f, 0, 0, 1.0f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause(boolean multitasking) {
|
||||
super.onPause(multitasking);
|
||||
if(this.inventoryFlag) {
|
||||
this.inventoryFlag = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume(boolean multitasking) {
|
||||
super.onResume(multitasking);
|
||||
this.isOpen = this.uhfService.isOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
// 不关闭也不会影响其他应用使用RFID功能,所有没有调用close()
|
||||
}
|
||||
}
|
||||
BIN
src/android/libsref/uhf.jar
Normal file
BIN
src/android/libsref/uhf.jar
Normal file
Binary file not shown.
BIN
src/android/res/raw/scan.ogg
Normal file
BIN
src/android/res/raw/scan.ogg
Normal file
Binary file not shown.
40
www/uhf.js
Normal file
40
www/uhf.js
Normal file
@@ -0,0 +1,40 @@
|
||||
var exec = require('cordova/exec');
|
||||
|
||||
var uhf = function Uhf() {};
|
||||
|
||||
uhf.open = function({success, error}) {
|
||||
exec(success, error, 'UHF', 'open', []);
|
||||
};
|
||||
|
||||
uhf.close = function({success, error}) {
|
||||
exec(success, error, 'UHF', 'close', []);
|
||||
};
|
||||
|
||||
uhf.getPower = function({success, error}) {
|
||||
exec(success, error, 'UHF', 'getPower', []);
|
||||
};
|
||||
|
||||
uhf.setPower = function({power, success, error}) {
|
||||
exec(success, error, 'UHF', 'setPower', [power]);
|
||||
};
|
||||
|
||||
uhf.inventory = function({success, error, sound}) {
|
||||
if(sound == undefined) {
|
||||
sound = true;
|
||||
}
|
||||
exec(success, error, 'UHF', 'inventory', [sound]);
|
||||
};
|
||||
|
||||
uhf.stopInventory = function({success, error}) {
|
||||
exec(success, error, 'UHF', 'stopInventory', []);
|
||||
};
|
||||
|
||||
|
||||
uhf.inventoryOnce = function({success, error, sound}) {
|
||||
if(sound == undefined) {
|
||||
sound = true;
|
||||
}
|
||||
exec(success, error, 'UHF', 'inventoryOnce', [sound]);
|
||||
};
|
||||
|
||||
module.exports = uhf;
|
||||
Reference in New Issue
Block a user