dcloud Native.js NFC开发


这段代码感谢DCloud_Android_磊子提供,作为我们学习研究dcloud移动app技术的一个参考。


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);
    }
}

你可能感兴趣的:(MUI,&,Html5+)