用Native.js怎么开发NFC

JavaScript011

用Native.js怎么开发NFC,第1张

大家先尝鲜一下,我仅测试读公交卡了。数据可以出来,具体哪些数据有用,需要专业开发者补充完善分享。在此先谢过了。

function test_nfc(){

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')

var NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter')

var 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.log('newintent')

setTimeout(handle_nfc_data,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)

}

function handle_nfc_data(){

var main = plus.android.runtimeMainActivity()

var runtimeIntent = main.getIntent()

var b = runtimeIntent.getExtras()

plus.android.importClass(b)

var set = b.keySet()

plus.android.importClass(set)

var sb

var i = set.iterator()

plus.android.importClass(i)

var hasNest = i.hasNext()

var NfcA = plus.android.importClass("android.nfc.tech.NfcA")

var IsoDep = plus.android.importClass("android.nfc.tech.IsoDep")

var Tag = plus.android.importClass("android.nfc.Tag")

while (hasNest) {

var key = i.next()

var v = b.get(key)

if(v instanceof NfcA){

var atqa = v.getAtqa()

var sak = v.getSak()

var tag = g.getTag()

plus.android.importClass(tag)

var techList = tag.getTechList()

console.log('NfcA Atqa=' + atqa + "Sak=" + sak + "techList=" + techList)

}else if(v instanceof IsoDep){

var tag = g.getTag()

plus.android.importClass(tag)

var techList = tag.getTechList()

console.log('IsoDep techList=' + techList)

}else if(v instanceof Tag){

var tag = v

var techList = tag.getTechList()

console.log('Tag techList=' + techList)

}

console.log(key + 'v=' + v)

sb = key + "=" + v + "\n"

hasNest = i.hasNext()

console.log('hasNest=' + hasNest)

}

}

在 根目录 的static中新建nfc.js文件

nfc.js

在需要的页面里面引入这个js

没了,结束了,是不是很简单呐,如有问题,欢迎留言。

最后:如果此篇博文对您有帮助,还请动动小手点点关注点点赞呐~,谢谢 ~ ~