1***@qq.com
1***@qq.com
  • 发布:2023-05-06 10:58
  • 更新:2023-05-08 09:25
  • 阅读:399

NFC读取功能,官方基座能调试成功,但自己打包(云打包)就无法读取

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10企业版 22H2

HBuilderX类型: 正式

HBuilderX版本号: 3.7.11

手机系统: Android

手机系统版本号: Android 12

手机厂商: 华为

手机机型: 荣耀50

页面类型: vue

vue版本: vue2

打包方式: 云端

项目创建方式: HBuilderX

App下载地址或H5⽹址: https://cdn.crrsi.net/app/20230506/51city.apk

示例代码:
var NfcAdapter;  
var NdefRecord;  
var NdefMessage;  
var _getCardNo;  
var waiting;  
var readyWriteData = false;  
var readyRead = false;  
var isBind=false;  

export default {  
    initNFC(callback) {  
        if (uni.getSystemInfoSync().platform == 'android') {  
            this.listenNFCStatus(callback);  
        }  
    },  
    readNFC() {  
        if (uni.getSystemInfoSync().platform == 'android') {  
            this.readData();  
        }  
    },  
    closeNFC() {  
        if (uni.getSystemInfoSync().platform == 'android') {  
            this.closeReadAndWrite();  
        }  
    },  
    listenNFCStatus(callback) {  
        try {  
            // plus.android.requestPermissions("android.permission.NFC");  
            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);  

            if (_nfcAdapter == null) {  
                this.showToast('设备不支持NFC!');  
                return;  
            }  

            if (!_nfcAdapter.isEnabled()) {  
                this.showToast('请在系统设置中先启用NFC功能!');  
                return;  
            }  
            if (typeof callback === 'function') {  
                _getCardNo = callback;  
                readyRead = true;  
            }  

            this.bindEvent();  
            _nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);  
        } catch (e) {  
            console.log(e);  
            this.showToast(e);  
        }  
    },  
    bindEvent(){  
        if(isBind){  
            return;  
        }  
        var that = this;  
        //监听贴卡到手机事件   
        plus.globalEvent.addEventListener('newintent', e => {  
            console.log('newintent');  
            //获取ic卡数据  
            setTimeout(() => {  
                that.handle_nfc_data()  
            }, 1000);  
            console.log('bind');  
            //销毁监听事件  
            //plus.globalEvent.removeEventListener("newintent");  
        });  
        isBind = true;  
        console.log('bind');  
    },  
    unbindEvent(){  
        if (waiting) {  
            waiting.close();  
        }  
        plus.globalEvent.removeEventListener("newintent");  
        isBind = false;  
        console.log('unbind');  
    },  
    handle_nfc_data() {  
        var main = plus.android.runtimeMainActivity();  
        var _intent = main.getIntent();  
        var _action = _intent.getAction();  
        console.log(_action);  
        if (NfcAdapter.ACTION_NDEF_DISCOVERED == _action || NfcAdapter.ACTION_TAG_DISCOVERED == _action || NfcAdapter  
            .ACTION_TECH_DISCOVERED == _action) {  
            if (readyRead) {  
                this.__read(_intent);  
            }  
        } else {  
            console.log(_action);  
        }  

    },  
    showToast(msg) {  
        if (typeof plus !== 'undefined') {  
            plus.nativeUI.toast(msg);  
        } else {  
            // 为了方便在浏览器中调试    
            uni.showToast({  
                title: msg,  
                duration: 2000,  
                icon: 'none',  
            });  
        }  
    },  
    __read(_intent) {  
        try {  
            if (waiting) {  
                waiting.setTitle('请勿移开标签\n正在读取数据...');  
            }  
            var _action = _intent.getAction();  
            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 bind_code = _intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);  
                // ic卡号 进制转换  
                var tagid = this.bytesToHexString(bind_code);  
                tagid = this.prefixInteger(this.hex2int(tagid), 10);  
                console.log(tagid);  
                // var tagid = this.bytesToHexString(tagFromIntent.getId());  
                // tagid = this.prefixInteger(this.hex2int(tagid), 10);  
                // console.log(tagid);  
                this.closeWaiting();  
                if (typeof _getCardNo === 'function') {  
                    _getCardNo(tagid);  
                }  
            } else {  
                this.showToast('读取失败');  
                this.closeWaiting();  
            }  
            // this.unbindEvent();  
        } catch (e) {  
            this.showToast(e);  
            this.closeWaiting();  
        }  
    },  
    closeWaiting(){  
        if (waiting) {  
            waiting.close();  
        }  
    },  
    bytesToHexString(inarray) {  
        var i, j, x;  
        var hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",  
            "B", "C", "D", "E", "F"  
        ];  
        var 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;  
    },  
    hex2int(hex) {  
        var len = hex.length,  
            a = new Array(len),  
            code;  
        for (var i = 0; i < len; i++) {  
            code = hex.charCodeAt(i);  
            if (48 <= code && code < 58) {  
                code -= 48;  
            } else {  
                code = (code & 0xdf) - 65 + 10;  
            }  
            a[i] = code;  
        }  
        return a.reduce(function(acc, c) {  
            acc = 16 * acc + c;  
            return acc;  
        }, 0);  
    },  
    prefixInteger(num, n) {  
        return (Array(n).join(0) + num).slice(-n);  
    },  
    writeData() {  
        var textEle = plus.globalEvent.getElementById('text');  
        if (!textEle.value) {  
            uni.showToast({  
                title: '请输入要写入的内容!',  
                icon: 'none'  
            });  
            return;  
        }  
        readyWriteData = true;  
        waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");  
    },  

    readData() {  
        this.bindEvent();  
        waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!", {  
            modal: false  
        });  
    },  

    closeReadAndWrite() {  
        readyWriteData = false;  
        readyRead = false;  
        if (waiting) {  
            waiting.close();  
        }  
        this.unbindEvent();  
    }  
}

操作步骤:
  1. 程序编译,使用官方基座进行调试,使用账号登录APP,进入“快捷收银”功能页。
  2. 打开手机NFC,刷任意IC卡,程序识别成功,不弹出“NFC服务”提醒。
  3. 程序编译,使用云打包,并成功安装到手机
  4. 登录APP,并进入“快捷收银”功能页
  5. 打开手机NFC,刷任意IC卡,系统弹出“NFC服务”提醒,读取IC卡信息失败

预期结果:

成功读取IC卡信息,并查询到对应绑定用户

实际结果:

系统弹出“NFC服务”提醒,读取IC卡信息失败

bug描述:

你好,
同样的NFC代码,使用官方基座调试,能正常使用,但使用自定义基座,NFC功能却无法使用,打正式包也是一样的表现,不知道是哪点的问题。
NFC调用如下:

import nfc from '@/utils/nfc-utils.js';  

nfc.initNFC(function(chip) {  
    if (!chip) {  
        return;  
    }  
         toast(chip);  
});
2023-05-06 10:58 负责人:无 分享
已邀请:
1***@qq.com

1***@qq.com (作者)

官方没人回复吗?

要回复问题请先登录注册