uni app 使用nfc识别功能
在自己的app识别出nfc卡内信息时 手机自己本身的nfc识别app也被调用 导致自己的app返回首页直接刷新 重载
/* js*/
nfcRuning: function(resolve) {
// console.log("--------------nfcRuning---------------")
NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
let main = plus.android.runtimeMainActivity();
let intent = main.getIntent();
let action = intent.getAction();
let that = this;
if (package_TECH_DISCOVERED == action) {
if (readyRead) {
//这里通过read方法拿到NFC数据
const type = that.read(intent);
// readyRead = false;
//将数据返回出去
resolve(type)
}
}
}
read(intent) {
// toast('请勿移开标签正在读取数据');
let that = this;
// NFC id
let code_type = '';
let bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
let nfc_id = that.byteArrayToHexString(bytesId);
let Parcelable = plus.android.importClass("android.os.Parcelable");
let rawmsgs = intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
if (rawmsgs != null && rawmsgs.length > 0) {
let records = rawmsgs[0].getRecords();
let result = records[0].getPayload();
let data = plus.android.newObject("java.lang.String", result);
code_type = data;
} else {
code_type = '暂无数据'
}
return code_type;
},
/*vue*/
async testNFC() {
console.log(111111)
//这里用异步获取读取到的NFC数据
this.nfc_type = await nfcFun.listenNFCStatus();
console.log(this.nfc_type)
},
能否阻止跳转
看起来你是在使用 Android 的 NFC 功能,并希望在识别到 NFC 卡片时阻止手机自带的 NFC 识别应用被调用。
如果要阻止手机自带的 NFC 识别应用被调用,你可以尝试在你的应用的 AndroidManifest.xml 文件中声明 android.nfc.action.NDEF_DISCOVERED 的 intent filter,并设置 android:priority 属性的值为最高。这样,你的应用就可以在识别到 NFC 卡片时优先响应该 intent,从而阻止手机自带的 NFC 识别应用被调用。
具体实现方法如下:
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action
在 AndroidManifest.xml 文件中声明 android.nfc.action.NDEF_DISCOVERED 的 intent filter,并设置 android:priority 属性的值为最高。
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter android:priority="1000">
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
在你的应用中处理 android.nfc.action.NDEF_DISCOVERED intent。
当你的应用接收到 android.nfc.action.NDEF_DISCOVERED intent 时,你可以使用 getParcelableArrayExtra() 方法从 intent 中获取 NFC 数据,然后解析该数据并做出相应的处理。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
// 获取 NFC 数据
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
// 解析 NFC 数