1***@qq.com
1***@qq.com
  • 发布:2021-01-14 09:31
  • 更新:2021-01-18 10:49
  • 阅读:739

【报Bug】3.x版本调用nfc读卡失败

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: Win10

HBuilderX类型: 正式

HBuilderX版本号: 3.0.5

手机系统: Android

手机系统版本号: Android 11

手机厂商: OPPO

手机机型: ACE2

页面类型: vue

打包方式: 云端

项目创建方式: HBuilderX

操作步骤:

使用HBuilderX3.x版本打包NFC读卡功能

预期结果:

读取出卡片信息

实际结果:

NFC读卡失败

bug描述:

更新3.x版本后调用nfc读卡失败(基座包和云端正式包),切换2.9.8版本正常打包使用

2021-01-14 09:31 负责人:DCloud_Android_ST 分享
已邀请:
DCloud_App_Array

DCloud_App_Array

请提供复现问题的示例代码

  • 木叶96

    const resultBytes = isoDep.transceive(apduBytes);

    读卡指令执行返回的结果是null

    2021-01-14 13:38

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

    <template>  
    <view class="wrap">
    <view class="sub-btn-area">
    <view class="img-container">
    <image src="../../../static/pages/mission/rfid@2x.png" mode="widthFix"></image>
    </view>
    </view>
    </view>
    <view class="connect">
    <view class="img-container">
    <image src="../../../static/pages/mission/rfid-2@2x.png" mode="widthFix"></image>
    </view>
    <view class="tip">请将手机背面靠近RFID卡,</view>
    <view class="tip">并根据任务列表内容执行相关任务</view>
    <view>
    {{ cardId }}
    </view>
    </view>
    </view>
    </template>

    <script>
    function 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;
    }
    var NfcAdapter;
    var NdefRecord;
    var NdefMessage;
    var waiting
    var readyRead = false;
    var nfcAdapter, main, pendingIntent, intentFiltersArray, techListsArray, IntentFilter
    import { getCurTimeStamp } from '../../../common/util/index.js'
    export default {
    data() {
    return {
    currentNFCInfo: [], //NFC 读取消息
    cardId: null,
    bannerShow: false,
    remark: "",
    message: "",
    count: 0,
    timestampHide: "",
    timestampShow: "",
    deviceRFID: '',
    taskId: ''
    };
    },
    methods: {
    listenNFCStatus() {
    try {
    console.log('Init NFC...');
    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');
    IntentFilter = plus.android.importClass('android.content.IntentFilter');
    NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
    nfcAdapter = NfcAdapter.getDefaultAdapter(main);
    if (nfcAdapter == null) {
    console.log("设备不支持NFC!");
    return;
    }
    if (!nfcAdapter.isEnabled()) {
    console.log("请在系统设置中先启用NFC功能!");
    return;
    }
    var intent = new Intent(main, main.getClass());
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
    var ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
    ndef.addDataType("*/*");
    intentFiltersArray = [ndef];
    techListsArray = [
    ["android.nfc.tech.IsoDep"],
    ["android.nfc.tech.NfcA"],
    ["android.nfc.tech.NfcB"],
    ["android.nfc.tech.NfcF"],
    ["android.nfc.tech.Ndef"],
    ["android.nfc.tech.NfcV"],
    ["android.nfc.tech.NdefFormatable"],
    ["android.nfc.tech.MifareClassic"],
    ["android.nfc.tech.MifareUltralight"]
    ];

    } catch (e) {
    console.error(e);
    }
    },
    handle_nfc_data() {

    NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
    NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
    // main = plus.android.runtimeMainActivity();
    var intent = main.getIntent();

    console.log("action type:" + intent.getAction());
    if ("android.nfc.action.TECH_DISCOVERED" == intent.getAction()) {
    if (readyRead) {
    this.__read(intent);
    readyRead = false;
    }
    } else {
    // waiting.close();
    console.log("nfc读取失败")
    }
    },
    __read(intent) {
    try {
    var content = "";
    waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
    waiting.setTitle('请勿移开标签\n正在读取数据...');
    var tag = plus.android.importClass("android.nfc.Tag");
    waiting.close();
    var Parcelable = plus.android.importClass("android.os.Parcelable");
    var rawmsgs = intent.getByteArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    var bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
    if (bytesId != null && bytesId.length > 0) {
    this.cardId = bytesToHexString(bytesId)
    // todo: 直接写入到缓存中 使用vuex
    if (this.cardId === this.deviceRFID) {
    // 弹窗,确认到设备列表页
    } else {
    // 弹窗,提示不是同一个地方
    }
    } else {
    console.log("NFC获取失败");
    uni.showToast({
    title: "NFC获取失败.",
    icon: "none"
    });
    }

    } catch (e) {
    console.log(e);
    console.log("NFC获取失败,丢出异常");
    waiting.close();
    uni.showToast({
    title: "NFC获取失败,丢出异常.",
    icon: "none"
    });
    //TODO handle the exception
    }
    },
    readData() {
    readyRead = true;
    // waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
    console.log('read')
    setTimeout(this.handle_nfc_data, 1000);
    }
    },
    onLoad: function(option) {
    this.listenNFCStatus()
    this.deviceRFID = option.rfidId
    this.id = option.id
    this.pageType = option.type
    },
    onShow: function() {
    console.log('page Show');
    this.timestampShow = (new Date()).getTime();
    var TimeScale = this.timestampShow - this.timestampHide;
    nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
    if (this.count++ == 0) {
    // this.listenNFCStatus()
    return false;
    } else if (TimeScale > 400) {
    return false;
    } else {
    this.readData();
    }
    },
    onHide: function() {
    console.log("onHide");
    this.timestampHide = (new Date()).getTime();
    console.log(this.timestampHide);
    }
    }
    </script>

    <style lang="scss">
    .wrap {
    height: 100vh;
    padding-bottom: 50px;
    color: #666666;

    .connect {
    text-align: center;
    // margin-top: 20px;
    .img-container {
    width: 216rpx;
    margin: 92rpx auto;
    image {
    width: 100%;
    }
    }

    .tip {
    font-size: 32rpx;
    font-family: PingFang SC;
    font-weight: 400;
    color: #666666;
    line-height: 52rpx;
    }
    }

    .sub-btn-area {
    margin-top: 30rpx;
    .img-container {
    margin: 60rpx auto 162rpx auto;
    width: 570rpx;
    image {
    width: 100%;
    }
    }
    }
    }
    </style>

    2021-01-18 10:28

3***@qq.com

3***@qq.com

哥们,我提完问题后才发现你也提了。

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

    3.0.7.20210109-alpha. 这个版本修复问题了

    2021-01-18 10:49

DCloud_Android_ST

DCloud_Android_ST

官方并没有提供NFC NJS实现的吧? 如果是请使用3.0.7试下。3.0.7修复了一个NJS对象无法转换问题

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

    谢谢,3.0.7 alpha 版修复这个问题了

    2021-01-18 10:50

1***@qq.com

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


<template>  
    <view class="wrap">  
        <view class="sub-btn-area">  
            <view class="img-container">  
                <image src="../../../static/pages/mission/rfid@2x.png" mode="widthFix"></image>  
            </view>  
        </view>  
        </view>  
        <view class="connect">  
            <view class="img-container">  
                <image src="../../../static/pages/mission/rfid-2@2x.png" mode="widthFix"></image>  
            </view>  
            <view class="tip">请将手机背面靠近RFID卡,</view>  
            <view class="tip">并根据任务列表内容执行相关任务</view>  
            <view>  
                {{ cardId }}  
            </view>  
        </view>  
    </view>  
</template>  

<script>  
    function 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]) 

该问题目前已经被锁定, 无法添加新回复