本人第一次用hbuilderx 开发手机app 因需求,,需要读写IC卡及获取IC卡的唯一ID
在论坛转了转,参照相关的帖子,如下: 原文地址1 原文地址2以及查看了android.nfc的相关api说明.经验分享出来.
var NfcAdapter;
var NdefRecord;
var NdefMessage;
var nfcAdapter;
function listenNFCStatus() {
try {
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var Activity = plus.android.importClass('android.app.Activity');
var PendingIntent = plus.android.importClass('android.app.PendingIntent');
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
nfcAdapter = NfcAdapter.getDefaultAdapter(main);
var intent = new Intent(main, main.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
var pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
var ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
ndef.addDataType("*/*");
var intentFiltersArray = [ndef];
var techListsArray = [
["android.nfc.tech.IsoDep"],
["android.nfc.tech.NfcA"],
["android.nfc.tech.NfcB"],
["android.nfc.tech.NfcF"],
["android.nfc.tech.Nfcf"],
["android.nfc.tech.NfcV"],
["android.nfc.tech.NdefFormatable"],
["android.nfc.tech.MifareClassi"],
["android.nfc.tech.MifareUltralight"]
];
document.addEventListener("newintent",
function() {
console.error('newintent');
setTimeout(handle_nfc_data1, 1000);
}, false);
document.addEventListener("pause", function(e) {
if (nfcAdapter) {
nfcAdapter.disableForegroundDispatch(main);
console.log('pause');
}
}, false);
document.addEventListener("resume", function(e) {
if (nfcAdapter) {
nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
}
}, false);
nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
} catch (e) {
console.error(e);
}
}
function handle_nfc_data1() {
NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
var main = plus.android.runtimeMainActivity();
var intent = main.getIntent();
if ("android.nfc.action.TECH_DISCOVERED" == intent.getAction()) {
if (readyWriteData) {
__write(intent);
readyWriteData = false;
} else if (readyRead) {
__read(intent);
readyRead = false;
} else if (readyGetid) {
__getId(intent);
readyGetid = false;
}
}
}
function showToast(msg) {
plus.nativeUI.toast(msg);
}
function __write(intent) {
try {
waiting.setTitle('请勿移开标签\n正在写入...');
var textBytes = plus.android.invoke(writeInfo, "getBytes");
// image/jpeg text/plain
var textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
plus.android.invoke("text/plain", "getBytes"), plus.android.invoke("", "getBytes"), textBytes);
var message = new NdefMessage([textRecord]);
var Ndef = plus.android.importClass('android.nfc.tech.Ndef');
var NdefFormatable = plus.android.importClass('android.nfc.tech.NdefFormatable');
var tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
var ndef = Ndef.get(tag);
console.log(JSON.stringify(ndef));
if (ndef != null) {
var size = message.toByteArray().length;
console.log("size=" + size);
ndef.connect();
if (!ndef.isWritable()) {
showToast("tag不允许写入");
waiting.close();
return;
}
if (ndef.getMaxSize() < size) {
showToast("文件大小超出容量");
waiting.close();
return;
}
ndef.writeNdefMessage(message);
waiting.close();
showToast("写入数据成功.");
return;
} else {
var format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
showToast("格式化tag并且写入message");
waiting.close();
return;
} catch (e) {
showToast("格式化tag失败.");
waiting.close();
return;
}
} else {
showToast("Tag不支持NDEF");
waiting.close();
return;
}
}
} catch (e) {
console.log("error=" + e);
waiting.close();
alert('写入失败');
}
}
function __read(intent) {
try {
waiting.setTitle('请勿移开标签\n正在读取数据...');
var Parcelable = plus.android.importClass("android.os.Parcelable");
var rawmsgs = intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
var records = rawmsgs[0].getRecords();
var result = records[0].getPayload();
var s = plus.android.newObject("java.lang.String", result);
waiting.close();
if (s.length > 0) {
if (typeof readAction === 'function') {
readAction(s);
}
} else {
showToast("数据为空");
}
} catch (e) {
console.log("error=" + e);
waiting.close();
alert('读取失败');
}
}
function __getId(intent) {
try {
var tag = plus.android.importClass('android.nfc.Tag');
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
var result = tag.getId();
//var id = plus.android.newObject("java.lang.String", result);
waiting.close();
if (result.length > 0) {
if (typeof GetidAction === 'function') {
GetidAction(GetIdParam, result);
}
} else {
showToast("数据为空");
}
} catch (e) {
//TODO handle the exception
console.log("error=" + e);
waiting.close();
alert('读取失败');
}
}
document.addEventListener('plusready', listenNFCStatus, false);
var waiting;
var readyWriteData = false;
var readyRead = false;
var readyGetid = false;
var readAction;
var GetidAction;
var GetIdParam;
var writeAction;
var writeInfo;
function writeData(writeValue, writefunction, ) {
readyWriteData = true;
writeInfo = writeValue;
writeAction = writefunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function readData(readfunction) {
readyRead = true;
readAction = readfunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function getId(param, getidfunction) {
readyGetid = true;
GetIdParam = param;
GetidAction = getidfunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function convertCardID(cardIdResult) {
var tmp = '';
for (var i = 0; i < cardIdResult.length; i++) {
var valueStr = parseInt(cardIdResult[i]);
if (valueStr < 0) {
tmp = tmp + (255 + valueStr + 1).toString(16).toUpperCase() + ":";
} else if (valueStr >= 0 && valueStr < 16) {
tmp = tmp + '0' + valueStr.toString(16).toUpperCase() + ":";
} else {
tmp = tmp + valueStr.toString(16).toUpperCase() + ":";
}
}
return tmp.substr(0, tmp.length - 1);
}
5 个评论
要回复文章请先登录或注册
1***@qq.com
震A震 (作者)
2***@qq.com
震A震 (作者)
震A震 (作者)