uni-app SDK上面无法监听到NFC信息 小程序里面无法读取 官方有没有准备添加这个NFC模块

uni-app SDK上面无法监听到NFC信息 小程序里面无法读取 官方有没有准备添加这个NFC模块

开发环境 版本号 项目创建方式
Mac mac 15.4 HBuilderX

产品分类:uniapp/App

PC开发环境操作系统:Mac

HBuilderX类型:正式

HBuilderX版本号:4.87

手机系统:Android

手机系统版本号:Android 12

手机厂商:华为

手机机型:荣耀畅享

页面类型:vue

vue版本:vue2

打包方式:云端

项目创建方式:HBuilderX

示例代码:

package com.example.unimpdemo;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.net.Uri;
import android.os.Parcelable;
import android.util.Log;
import android.nfc.Tag;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 1.子类需要在onCreate方法中做Activity初始化。
 * 2.子类需要在onNewIntent方法中进行NFC标签相关操作。
 * 当launchMode设置为singleTop时,第一次运行调用onCreate方法,
 * 第二次运行将不会创建新的Activity实例,将调用onNewIntent方法
 * 所以我们获取intent传递过来的Tag数据操作放在onNewIntent方法中执行
 * 如果在栈中已经有该Activity的实例,就重用该实例(会调用实例的onNewIntent())
 * 只要NFC标签靠近就执行
 *
 * Created by gc on 2016/12/8.
 */
public class BaseActivity extends AppCompatActivity {

private static final String TAG = "BaseActivity";
private NfcAdapter mNfcAdapter;
private PendingIntent mPendingIntent;

[@Override](/user/Override)
public void onNewIntent(Intent intent) {
    //1.获取Tag对象
    super.onNewIntent(intent);
    Uri data = intent.getData();
    // 读取NDEF数据
    Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    Log.d(TAG, "onNewIntent    data=1=" + data);

}

/**
 * 启动Activity,界面可见时
 */
[@Override](/user/Override)
protected void onStart() {
    super.onStart();
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    //一旦截获NFC消息,就会通过PendingIntent调用窗口
    //        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), PendingIntent.FLAG_IMMUTABLE);
    //        FLAG_UPDATE_CURRENT:如果存在匹配的 PendingIntent,更新其内部 Intent 的 Extra 数据(最常用)。
    //        FLAG_MUTABLE:允许修改 PendingIntent 内部的 Intent 数据(仅特殊场景使用)。
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
}

/**
 * 获得焦点,按钮可以点击
 */
[@Override](/user/Override)
public void onResume() {
    super.onResume();
    //设置处理优于所有其他NFC的处理
    if (mNfcAdapter != null)
        mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}

/**
 * 暂停Activity,界面获取焦点,按钮可以点击
 */
[@Override](/user/Override)
public void onPause() {
    super.onPause();
    //恢复默认状态
    if (mNfcAdapter != null)
        mNfcAdapter.disableForegroundDispatch(this);
}
}

更多关于uni-app SDK上面无法监听到NFC信息 小程序里面无法读取 官方有没有准备添加这个NFC模块的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复
<uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <activity android:name="com.example.unimpdemo.MainActivity" android:theme="@style/DCloudMPHostActivityTheme" android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale" android:hardwareAccelerated="true" android:exported="true" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.TAG_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
    <!-- 配置支持的NFC技术类型 -->  
    <meta-data  
        android:name="android.nfc.action.TECH_DISCOVERED"  
        android:resource="@xml/nfc_tech_filter" />  

</activity><br>

更多关于uni-app SDK上面无法监听到NFC信息 小程序里面无法读取 官方有没有准备添加这个NFC模块的实战教程也可以访问 https://www.itying.com/category-93-b0.html


该bug反馈内容不完整:描述过于简略,未说明具体错误现象及已尝试的解决方案;代码示例仅展示原生Android Activity实现,未体现uni-app集成方式(缺少manifest配置、插件注册等关键步骤),无法直接复现;复现步骤缺失关键操作流程(如uni-app项目配置、插件调用方式)。分类信息较完整,包含HBuilderX 4.87、Android 12等必要环境信息。
经核查知识库,uni-app的App平台(编译为原生应用)支持NFC功能,但需通过规范方式实现:

官方明确说明App平台NFC支持方案:Native.js实现安卓NFC 或 原生插件开发
用户混淆了"小程序"与"uni-app编译的App"概念:其提供的Android原生代码属于App平台范畴,而非微信小程序等平台(知识库显示仅部分小程序平台支持NFC)

该问题不成立,属典型集成误区:

uni-app App平台本身支持NFC,但需按规范开发(不能直接继承BaseActivity覆盖生命周期)
用户应使用Native.js调用系统API或开发原生插件,而非修改Activity基类
HBuilderX 4.87版本已支持NFC功能,无需等待官方添加

建议解决方案:

参考App平台NFC文档使用uni.startHCE等标准API
若需深度定制,按原生插件开发规范实现
补充提供uni-app端调用代码及错误日志以便进一步诊断 内容为 AI 生成,仅供参考

回到顶部