|
后台自定义一个action用来进行16进制的转换
@SuppressWarnings("unused")
public static JSONObject getCardId(JSONObject params, ActionContext context) throws SQLException, NamingException, java.sql.SQLException, UnsupportedEncodingException {
JSONObject ret = new JSONObject();
String id1 = params.getString("id1");
String id2 = params.getString("id2");
String id3 = params.getString("id3");
String id4 = params.getString("id4");
byte a1= Byte.parseByte(id1);
byte a2 =Byte.parseByte(id2);
byte a3=Byte.parseByte(id3);
byte a4 =Byte.parseByte(id4);
byte[] src = new byte[]{a1,a2,a3,a4};
//byte[] src = new byte[]{-128,44,1,81};
//byte[] src =cardId.getBytes();
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
char[] buffer = new char[2];
for (int i = 0; i < src.length; i++) {
buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
stringBuilder.append(buffer);
}
String cardNo = stringBuilder.toString();
ret.put("cardNo", cardNo);
return ret;
}
Model.prototype.button1Click = function(event){
nfc.addTagDiscoveredListener(myNfcListener,success, fail);
};
function myNfcListener(NfcEvent,intent){
var params ={};
justep.Baas.sendRequest({
"url" : "/hhxxfs/khxxaction",
"action" : "getCardId",
"async" : false,
"params" : {id1:NfcEvent.tag.id[0],id2:NfcEvent.tag.id[1],
id3:NfcEvent.tag.id[2],id4:NfcEvent.tag.id[3]},
"success" : function(ret) {
alert(ret.cardNo);
}
});
}
function success(result){
alert("lyq"+result);
}
function fail(result){
alert("failed"+fail);
}
这是前台调用的方法
define(function(require){
var $ = require("jquery");
var justep = require("$UI/system/lib/justep");
require("cordova!com.justep.cordova.plugin.nfc");--别忘了引用这句
|
|