哦啦啦
哦啦啦
  • 发布:2019-09-02 17:15
  • 更新:2024-07-23 15:36
  • 阅读:7628

uni-app 利用native调用Android NFC 读取RFID标签的UID

分类:uni-app

manifest.json 权限:
"<uses-permission android:name=\"android.permission.INTERNET\"/>",
"<uses-permission android:name=\"android.permission.NFC\"/>",
"<uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\"/>",
nfc.vue代码如下,完全可运行:

<template>  
    <view>  
        <view class="uni-padding-wrap">  
            NFC  
            <view class="uni-common-mt" style="background:#FFF; padding:20upx;">  
                <text>  
                    UID:{{UID}}  
                    {{tip}}  
                </text>  
            </view>  
        </view>  
    </view>  
</template>  

<script>  
    var NfcAdapter;  
    export default {  
        data() {  
            return {  
                title: 'redNFC',  
                UID: '',  
                msg: '',  
                tip: '',  
            }  
        },  
        onLoad() {  
            console.log("onLoad");  

        },  
        onShow() {  
            console.log("onShow");  
            this.NFCInit();  
        },  
        onHide() {  
            console.log("onHide");  
            this.NFCReadUID();  
        },  
        methods: {  
            NFCInit() {  
                try {  
                    var main = plus.android.runtimeMainActivity();  
                    //console.log(main);  
                    var Intent = plus.android.importClass('android.content.Intent');  
                    // console.log(Intent);  
                    var Activity = plus.android.importClass('android.app.Activity');  
                    //console.log(Activity);  
                    var PendingIntent = plus.android.importClass('android.app.PendingIntent');  
                    // console.log(PendingIntent);                    
                    var IntentFilter = plus.android.importClass('android.content.IntentFilter');  
                    // console.log(IntentFilter);  
                    // var Uri = plus.android.importClass('android.net.Uri');                     
                    // var Bundle = plus.android.importClass('android.os.Bundle');                    
                    // var Handler = plus.android.importClass('android.os.Handler');  
                    //console.log(Handler);  
                    NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');  
                    //console.log(NfcAdapter);  
                    var _nfcAdapter = NfcAdapter.getDefaultAdapter(main)  
                    // console.log(_nfcAdapter);  

                    var ndef = new IntentFilter("android.nfc.action.NDEF_DISCOVERED"); //NfcAdapter.ACTION_NDEF_DISCOVERED  
                    // console.log(ndef);  
                    var tag = new IntentFilter("android.nfc.action.TAG_DISCOVERED"); //NfcAdapter.ACTION_TECH_DISCOVERED  
                    // console.log(tag);  
                    var tech = new IntentFilter("android.nfc.action.TECH_DISCOVERED");  
                    // console.log(tech);  
                    var intentFiltersArray = [ndef, tag, tech];  

                    var techListsArray = [  
                        ["android.nfc.tech.Ndef"],  
                        ["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"]  
                    ];  

                    var _intent = new Intent(main, main.getClass());  
                    // console.log(_intent);  
                    _intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  

                    var pendingIntent = PendingIntent.getActivity(main, 0, _intent, 0);  
                    // console.log(pendingIntent);  

                    if (_nfcAdapter == null) {  
                        this.tip = '本设备不支持NFC!';  
                    } else if (_nfcAdapter.isEnabled() == false) {  
                        this.tip = 'NFC功能未打开!';  
                    } else {  
                        this.tip = 'NFC正常';  
                        _nfcAdapter.enableForegroundDispatch(main, pendingIntent, IntentFilter, techListsArray);  
                    }  
                } catch (e) {  
                    //TODO handle the exception  
                }  
            },  
            NFCReadUID() {  
                var main = plus.android.runtimeMainActivity();  
                var _intent = main.getIntent();  
                var _action = _intent.getAction();  
                // console.log("action type:" + _action);  

                if (NfcAdapter.ACTION_NDEF_DISCOVERED == _action || NfcAdapter.ACTION_TAG_DISCOVERED == _action || NfcAdapter.ACTION_TECH_DISCOVERED ==  
                    _action) {  
                    var Tag = plus.android.importClass('android.nfc.Tag');  
                    var tagFromIntent = _intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
                    var uid = _intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);  
                    console.log(uid);  
                    this.UID = this.Bytes2HexString(uid);  
                    //console.log(this.UID);  
                }  
            },  
            //将byte[] 转为Hex,  
            Bytes2HexString(arrBytes) {  
                var str = "";  
                for (var i = 0; i < arrBytes.length; i++) {  
                    var tmp;  
                    var num = arrBytes[i];  
                    if (num < 0) {  
                        //Java中数值是以补码的形式存在的,应用程序展示的十进制是补码对应真值。补码的存在主要为了简化计算机底层的运算,将减法运算直接当加法来做  
                        tmp = (255 + num + 1).toString(16);  
                    } else {  
                        tmp = num.toString(16);  
                    }  
                    if (tmp.length == 1) {  
                        tmp = "0" + tmp;  
                    }  
                    str += tmp;  
                }  
                return str;  
            }  
        }  
    }  
</script>  

<style>  

</style>  
3 关注 分享
全栈伟哥 k***@gmail.com 5***@qq.com

要回复文章请先登录注册

wfvue

wfvue

回复 1***@qq.com :
请问解决了吗
2021-01-16 12:27
wfvue

wfvue

回复 1***@qq.com :
请问这个问题解决了吗
2021-01-16 06:58
haapy

haapy

您好,我想问下,为啥我用您这套代码读取rfid标签不起作用
2020-12-04 10:33
哦啦啦

哦啦啦 (作者)

回复 1***@qq.com :
NFC的读取是被动触发的,只有在手机与卡近距离感应时才会触发返回读取的UID,只要正常读取就一定会有返回值;
2020-09-25 14:19
哦啦啦

哦啦啦 (作者)

回复 2***@qq.com :
扫描卡片是什么场景,目前实现的功能目的是调用Android手机的NFC模块对RFID HF频段的UID读取
2020-09-25 14:16
1***@qq.com

1***@qq.com

您好作者,这套代码在不重启app的情况下,在不拍卡的情况下调用NFCReadUID会返回上一次的UID,请问这个能做到清除不,意思是不拍卡调用NFCReadUID就返回空
2020-07-10 15:39
2***@qq.com

2***@qq.com

回复 哦啦啦 :
您好。您的代码只支持扫描卡片吗?
2020-06-26 11:03
哦啦啦

哦啦啦 (作者)

花了一点时间写了关于NFC的api使用,参考了dcloud社区的文章,不过最终都没能实现,既然能利用Native调用原生代码,那就索性把我这能真实运行的Android代码,用native照样写一遍,byte[]转Hex也卡了一点时间,好在最后终于搞定了
2019-09-02 17:20