2***@qq.com
2***@qq.com
  • 发布:2023-10-14 16:16
  • 更新:2023-10-14 20:53
  • 阅读:223

uniapp 做NFC读取功能 为什么卡片贴在手机时调用读取方法时读取不到 只能拿远贴近才能读取到

分类:HBuilderX

uniapp 做NFC读取功能 为什么卡片贴在手机时调用读取方法时读取不到 只能拿远贴近才能读取到

2023-10-14 16:16 负责人:无 分享
已邀请:
II优选

II优选 - 定制插件开发-联系QQ:770104707

怎么写的?

II优选

II优选 - 定制插件开发-联系QQ:770104707

应该是触发问题

  • 2***@qq.com (作者)

    用的插件市场的uni-nfc

    提供的方法 我定时调用了一下

    2023-10-14 16:32

  • 2***@qq.com (作者)

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


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

    var _nfcAdapter = NfcAdapter.getDefaultAdapter(main);

    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);
    var that = this;
    //监听贴卡到手机事件
    plus.globalEvent.addEventListener('newintent', e => {
    //获取ic卡数据
    that.NFCReadUID()
    //销毁监听事件
    plus.globalEvent.removeEventListener("newintent", false);
    });
    if (_nfcAdapter == null) {} else if (_nfcAdapter.isEnabled() == false) {} else {
    _nfcAdapter.enableForegroundDispatch(main, pendingIntent, IntentFilter, techListsArray);
    }
    } catch (e) {
    //TODO handle the exception
    }

    2023-10-14 16:33

  • 2***@qq.com (作者)

    他只有把卡片拿远再贴一下才可以获取到

    2023-10-14 16:34

  • II优选

    回复 2***@qq.com: 没用过,可以试试插件市场的插件是不是也是这个样子的

    2023-10-14 16:39

套马杆的套子

套马杆的套子 - 没有解决不了的问题,只有解决不完的问题

调用界面中引用附件中的js,界面中使用如下

            <uni-popup ref="alertDialog" type="dialog">  
                <uni-popup-dialog type="info" title="提示" content="请将手机靠近NFC贴,直到震动" :isShowCfirm="isShowCfirm">  
                </uni-popup-dialog>  
            </uni-popup>

data中定义

auiDialog: {  
                    title: '提示',  
                    msg: '请将手机靠近NFC贴,直到震动',  
                    btns: [{  
                        name: '关闭'  
                    }],  
                    mask: true,  
                    maskTapClose: true,  
                    items: [],  
                    theme: 1,  

                },

在调用

this.nfc.NFCInit(that, 'alertDialog')  
plus.globalEvent.addEventListener("resume", function(e) {  

                                var code = that.nfc.NFCReadUID()  
                                that.$refs.alertDialog.close()  
                                if (code != null && code != '') {  

                                    that.bindFun(code, that.selectItem, type);  
                                } else {  
                                    that.$toast('NFC内容为空!')  
                                }  
                                plus.globalEvent.removeEventListener('resume');  
                            }, false);

要回复问题请先登录注册