ps:测试时需在移动端测试 PC测试无效
document.addEventListener("plusready", function() {
// NFC监听
monitorNFC()
}
function monitorNFC(t){
let NfcAdapter;
let NdefRecord;
let NdefMessage;
function listenNFCStatus() {
try {
let main = plus.android.runtimeMainActivity();
let Intent = plus.android.importClass('android.content.Intent');
let Activity = plus.android.importClass('android.app.Activity');
let PendingIntent = plus.android.importClass('android.app.PendingIntent');
let IntentFilter = plus.android.importClass('android.content.IntentFilter');
NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
let nfcAdapter = NfcAdapter.getDefaultAdapter(main);
let intent = new Intent(main, main.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
let pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
let ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
ndef.addDataType("*/*");
let intentFiltersArray = [ndef];
let 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.MifareClassic"],
["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) {
console.log('resume');
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");
let main = plus.android.runtimeMainActivity();
let intent = main.getIntent();
if ("android.nfc.action.TECH_DISCOVERED" == intent.getAction()) {
// if (readyWriteData) {
// __write(intent);
// readyWriteData = false;
// } else if (readyRead) {
console.log("进行读取NFC")
__read(intent);
readyRead = false;
// }
}
}
function __read(intent) {
try {
let content = "";
waiting = plus.nativeUI.showWaiting("请勿移开标签\n正在读取数据...");
let tag = plus.android.importClass("android.nfc.Tag");
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
let bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
let rawmsgs = intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
let records = rawmsgs[0].getRecords();
let result = records[0].getPayload();
let s = plus.android.newObject("java.lang.String",result);
console.log( s)
if(s){
s = s.slice(s.indexOf('en') + 2)
}
console.log('查询解过:'+s)
// console.log("bytesId:" + (bytesId));
// console.log("十六进制ID:" + (bytesToHexString(tag.getId())));
} catch (e) {
alert(e);
}
readyRead = false
waiting.close();
}
function bytesToHexString(inarray) {
let i, j, x;
let hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F"
];
let out = "";
for (j = 0; j < inarray.length; ++j) {
x = parseInt(inarray[j]) & 0xff;
i = (x >> 4) & 0x0f;
out += hex[i];
i = x & 0x0f;
out += hex[i];
}
return out;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)