通过继承StandardFeature类自定义的插件调用第三方读取rfid的包读取rfid信息,始终无法打开设备;用原生android程序调用正常。哪位高手给指点一下?
插件代码如下:
package com.itlamp.android.plugin;
import io.dcloud.common.DHInterface.AbsMgr;
import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.hardware.rfid.reader;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class PluginTest extends StandardFeature {
static Map<Integer,String> errorcode=new HashMap<Integer,String>();
static{
errorcode.put(-20, "产品不对");
errorcode.put(-1, "设备无法打开");
errorcode.put(1, "设备已打开");
errorcode.put(0, "打开设备成功");
errorcode.put(-2, "设备参数无法设置");
}
private Handler mHandler = new MainHandler();
private String m_strresult=null;
boolean isLoop=false;
boolean isScan=false;
boolean msgEnd=false;//消息是否结束
private List<String> cardList=null;
/**
* 控件释放时调用此函数
*/
@Override
public void dispose(String arg0) {
// TODO Auto-generated method stub
super.dispose(arg0);
//关闭读卡器
reader.Close();
}
/**
* 控件被调用之前调用此函数
*/
@Override
public void init(AbsMgr arg0, String arg1) {
// TODO Auto-generated method stub
super.init(arg0, arg1);
int ret=reader.Open("/dev/ttyS4");
if(ret!=0){
Log.e("设备信息", "打开设备失败:"+ret);
}else{
//初始化对象
reader.m_handler=mHandler;
cardList=new ArrayList<String>();
}
}
/**
* 打开设备信息
*/
public void openDevice(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
JSONObject retobj=new JSONObject();
try {
int ret=reader.Open("/dev/ttyS4");
retobj.put("flag", ret);
retobj.put("msg",errorcode.get(ret));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSUtil.execCallback(pWebview, CallBackID, retobj, JSUtil.OK, false);
}
/**
* 关闭设备
* @param pWebview
* @param array
*/
public void closeDevice(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
JSONObject retobj=new JSONObject();
try {
reader.Close();
retobj.put("flag", 1);
retobj.put("msg","关闭成功");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSUtil.execCallback(pWebview, CallBackID, retobj, JSUtil.OK, false);
}
/**
* 搜索rfid卡
* @param pWebview
* @param array
*/
public void searchCard(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
m_strresult="";
cardList.clear();
msgEnd=false;
isLoop=false;
isScan=true;
reader.SearchLables((byte)0xff,(byte)1);
Map<String,String> data=getData();
if(data.containsKey("ERROR")){
JSUtil.execCallback(pWebview, CallBackID, "扫描RFID失败", JSUtil.ERROR, false);
}else{
JSONArray retobj=new JSONArray();
for(Map.Entry<String, String> ent:data.entrySet()){
retobj.put(ent.getKey());
}
JSUtil.execCallback(pWebview, CallBackID, retobj, JSUtil.OK, false);
}
}
/**
* 循环搜索rfid卡
* @param pWebview
* @param array
*/
public void loopSearchCard(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
m_strresult="";
cardList.clear();
msgEnd=false;
isLoop=true;
isScan=true;
reader.SearchLables((byte)0xff,(byte)1);
JSUtil.execCallback(pWebview, CallBackID, "startLoop", JSUtil.OK, false);
}
/**
* 停止循环搜索rfid卡
* @param pWebview
* @param array
*/
public void loopSearchEnd(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
isLoop=false;
Map<String,String> data=getData();
if(data.containsKey("ERROR")){
JSUtil.execCallback(pWebview, CallBackID, "扫描RFID失败", JSUtil.ERROR, false);
}else{
JSONArray retobj=new JSONArray();
for(Map.Entry<String, String> ent:data.entrySet()){
retobj.put(ent.getKey());
}
JSUtil.execCallback(pWebview, CallBackID, retobj, JSUtil.OK, false);
}
}
/**
* 选择rfid卡
* @param pWebview
* @param array [回调ID,rfid卡ID]
*/
public void selectCard(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
//卡ID
String cardId=array.optString(1);
byte[]epc=reader.stringToBytes(cardId);
int ret=reader.SetAccessEpcMatch((byte)0xff, (byte)0, (byte)epc.length, epc);
if(ret==0){
JSUtil.execCallback(pWebview, CallBackID, "选择成功", JSUtil.OK, false);
}else{
JSUtil.execCallback(pWebview, CallBackID, "选择失败:"+ret, JSUtil.ERROR, false);
}
}
/**
* 读取制定rfid的数据
* @param pWebview
* @param array [回调函数,起始地址,数据长度,卡密码,rfid]
*/
public void readCard(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
//卡ID
String cardId=array.optString(4);
byte[]epc=reader.stringToBytes(cardId);
int ret=reader.SetAccessEpcMatch((byte)0xff, (byte)0, (byte)epc.length, epc);
int nadd=array.optInt(1);//读取起始地址,默认:0;
int ndatalen=array.optInt(2);//读取数据的长度
String mimaStr = array.optString(3);//读取卡的密码
if (mimaStr == null || mimaStr.equals("")) {
JSUtil.execCallback(pWebview, CallBackID, "必须设置8位数字的读取密码", JSUtil.ERROR, false);
return;
}
byte[] passw = reader.stringToBytes(mimaStr);
byte btMemBank=3;//1-EPC 0-密码 2-TID 3-user
m_strresult="";
cardList.clear();
msgEnd=false;
isScan=false;
reader.ReadLables((byte)0xff, btMemBank, (byte)nadd, (byte)ndatalen, passw);
//获取数据
Map<String,String> data=getData();
if(data.containsKey("ERROR")){
JSUtil.execCallback(pWebview, CallBackID, "读数据失败", JSUtil.ERROR, false);
}else{
JSONArray retobj=new JSONArray();
for(Map.Entry<String, String> ent:data.entrySet()){
Map<String,String> item=new HashMap<String, String>();
item.put("rfid", ent.getKey());
item.put("data", ent.getValue());
retobj.put(item);
}
JSUtil.execCallback(pWebview, CallBackID, retobj, JSUtil.OK, false);
}
}
/**
* 读取范围内的所有rfid的信息
* @param pWebview
* @param array [回调函数,起始地址,数据长度,卡密码]
*/
public void readAllCard(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
int nadd=array.optInt(1);//读取起始地址,默认:0;
int ndatalen=array.optInt(2);//读取数据的长度
String mimaStr = array.optString(3);//读取卡的密码
if (mimaStr == null || mimaStr.equals("")) {
JSUtil.execCallback(pWebview, CallBackID, "必须设置8位数字的读取密码", JSUtil.ERROR, false);
return;
}
byte[] passw = reader.stringToBytes(mimaStr);
byte btMemBank=3;//1-EPC 0-密码 2-TID 3-user
m_strresult="";
cardList.clear();
msgEnd=false;
isScan=false;
reader.ReadLables((byte)0xff, btMemBank, (byte)nadd, (byte)ndatalen, passw);
//获取数据
Map<String,String> data=getData();
if(data.containsKey("ERROR")){
JSUtil.execCallback(pWebview, CallBackID, "读数据失败", JSUtil.ERROR, false);
}else{
JSONArray retobj=new JSONArray();
for(Map.Entry<String, String> ent:data.entrySet()){
Map<String,String> item=new HashMap<String, String>();
item.put("rfid", ent.getKey());
item.put("data", ent.getValue());
retobj.put(item);
}
JSUtil.execCallback(pWebview, CallBackID, retobj, JSUtil.OK, false);
}
}
/**
* 写入rfid
* @param pWebview
* @param array [回调函数,起始地址,数据长度,卡密码,写入数据,rfid]
*/
public void writeCard(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
//卡ID
String cardId=array.optString(5);
byte[]epc=reader.stringToBytes(cardId);
int ret=reader.SetAccessEpcMatch((byte)0xff, (byte)0, (byte)epc.length,epc);
int nadd=array.optInt(1);//读取起始地址,默认:0;
int ndatalen=array.optInt(2);//数据的长度
String mimaStr = array.optString(3);//卡的密码
if (mimaStr == null || mimaStr.equals("")) {
JSUtil.execCallback(pWebview, CallBackID, "必须设置8位数字的读取密码", JSUtil.ERROR, false);
return;
}
byte[] passw = reader.stringToBytes(mimaStr);
byte btMemBank=3;//1-EPC 0-密码 2-TID 3-user
byte[] pwrite=new byte[ndatalen];
//String dataE = m_editInput.getText().toString().trim();
//获取输入字符串转成hex16进制
String idata=array.optString(4);//写入数据
String dataE=reader.encodeHex(idata);
byte[] myByte = reader.stringToBytes(dataE);
System.arraycopy(myByte, 0, pwrite, 0, myByte.length > ndatalen ? ndatalen: myByte.length);
m_strresult="";
cardList.clear();
//设置消息读取标识
msgEnd=false;
isScan=false;
reader.Writelables((byte)0xff, passw, btMemBank, (byte)nadd, (byte)ndatalen, pwrite);
Map<String,String> data=getData();
if(data.containsKey("ERROR")){
JSUtil.execCallback(pWebview, CallBackID, "写数据失败", JSUtil.ERROR, false);
}else{
JSONArray retobj=new JSONArray();
for(Map.Entry<String, String> ent:data.entrySet()){
Map<String,String> item=new HashMap<String, String>();
item.put("rfid", ent.getKey());
item.put("data", ent.getValue());
retobj.put(item);
}
JSUtil.execCallback(pWebview, CallBackID, retobj, JSUtil.OK, false);
}
}
/**
* 写入所有范围内的RFID卡
* @param pWebview
* @param array [回调函数,起始地址,数据长度,卡密码,写入数据]
*/
public void writeAllCard(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
int nadd=array.optInt(1);//读取起始地址,默认:0;
int ndatalen=array.optInt(2);//数据的长度
String mimaStr = array.optString(3);//卡的密码
if (mimaStr == null || mimaStr.equals("")) {
JSUtil.execCallback(pWebview, CallBackID, "必须设置8位数字的读取密码", JSUtil.ERROR, false);
return;
}
byte[] passw = reader.stringToBytes(mimaStr);
byte btMemBank=3;//1-EPC 0-密码 2-TID 3-user
byte[] pwrite=new byte[ndatalen];
//String dataE = m_editInput.getText().toString().trim();
//获取输入字符串转成hex16进制
String idata=array.optString(4);//写入数据
String dataE=reader.encodeHex(idata);
byte[] myByte = reader.stringToBytes(dataE);
System.arraycopy(myByte, 0, pwrite, 0, myByte.length > ndatalen ? ndatalen: myByte.length);
m_strresult="";
cardList.clear();
//设置消息读取标识
msgEnd=false;
isScan=false;
reader.Writelables((byte)0xff, passw, btMemBank, (byte)nadd, (byte)ndatalen, pwrite);
Map<String,String> data=getData();
if(data.containsKey("ERROR")){
JSUtil.execCallback(pWebview, CallBackID, "写数据失败", JSUtil.ERROR, false);
}else{
JSONArray retobj=new JSONArray();
for(Map.Entry<String, String> ent:data.entrySet()){
Map<String,String> item=new HashMap<String, String>();
item.put("rfid", ent.getKey());
item.put("data", ent.getValue());
retobj.put(item);
}
JSUtil.execCallback(pWebview, CallBackID, retobj, JSUtil.OK, false);
}
}
/**
* 获取消息读取结束后的数据
* @return
*/
private Map<String,String> getData(){
while(msgEnd==false){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Map<String,String> ret=new HashMap<String, String>();
for(String str1:cardList){
if(str1.indexOf("ERROR")>-1){
ret.put("ERROR", "ERROR");
}else if(str1.length()>32&&isScan==false){
String rdata=str1.substring(32);
String cardid=str1.substring(4, 28);
ret.put(cardid, reader.decodeHex(rdata));
}else{
ret.put(str1,str1);
}
}
return ret;
}
public void testMethod(IWebview pWebview, JSONArray array){
String CallBackID = array.optString(0);
JSONArray newArray = new JSONArray();
newArray.put(array.optString(1));
newArray.put(array.optString(2));
newArray.put(array.optString(3));
JSUtil.execCallback(pWebview, CallBackID, newArray, JSUtil.OK, false);
}
public void PluginTestFunctionArrayArgu(IWebview pWebview, JSONArray array)
{
String ReturnString = null;
String CallBackID = array.optString(0);
JSONArray newArray = null;
try {
newArray = new JSONArray(array.optString(1));
String inValue1 = newArray.getString(0);
String inValue2 = newArray.getString(1);
String inValue3 = newArray.getString(2);
String inValue4 = newArray.getString(3);
ReturnString = inValue1 + "-" + inValue2 + "-" + inValue3 + "-" + inValue4;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSUtil.execCallback(pWebview, CallBackID, ReturnString, JSUtil.OK, false);
}
public String testMethodSync(IWebview pWebview, JSONArray array)
{
JSONArray newArray = null;
JSONObject retJSONObj = null;
try {
newArray = array.optJSONArray(0);
String inValue1 = newArray.getString(0);
String inValue2 = newArray.getString(1);
String inValue3 = newArray.getString(2);
retJSONObj = new JSONObject();
retJSONObj.putOpt("RetArgu1", inValue1);
retJSONObj.putOpt("RetArgu2", inValue2);
retJSONObj.putOpt("RetArgu3", inValue3);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return JSUtil.wrapJsVar(retJSONObj);
}
public String PluginTestFunctionSync(IWebview pWebview, JSONArray array)
{
String inValue1 = array.optString(0);
String inValue2 = array.optString(1);
String inValue3 = array.optString(2);
String inValue4 = array.optString(3);
String ReturnValue = inValue1 + "-" + inValue2 + "-" + inValue3 + "-" + inValue4;
return JSUtil.wrapJsVar(ReturnValue,true);
}
/**
* 读取卡信息Handler类
* @author xql
*
*/
private class MainHandler extends Handler {
@Override
public void handleMessage(Message msg) {
//记录读取的数据
if(msg.what!=0)
{
if(m_strresult.indexOf((String)msg.obj)<0)
{
String strtmp=(String)msg.obj;
m_strresult+=strtmp;
m_strresult+="\r\n";
cardList.add(strtmp);
}
}
if(msg.what==0)
{
if(isLoop){
//读取rfid卡列表结束
if(((String)msg.obj).equals("1"))
{
msgEnd=true;
}else if(((String)msg.obj).equals("endmsg")){
msgEnd=true;
}
reader.SearchLables((byte)0xff,(byte)1);
return;
}else{
if(((String)msg.obj).equals("1"))
{
msgEnd=true;
}else if(((String)msg.obj).equals("endmsg")){
msgEnd=true;
}
}
}
else if(msg.what==-1)
{
if(isLoop)
{
reader.SearchLables((byte)0xff,(byte)1);
return;
}
cardList.add("ERROR");
}
}
};
}
2 个回复
磊子 (作者)
这个问题大家都没有遇到过,看看只有自己研究啦。
l***@hotmail.com
请问你解决这个问题了么?