gpt4 book ai didi

¿How can I read the data from a NFC on the background (when de app is closed) in React Native?(如何在Reaction Native中从后台(关闭应用程序时)读取NFC中的数据?)

转载 作者:bug小助手 更新时间:2023-10-24 23:00:07 29 4
gpt4 key购买 nike



I got this code which works fine when the app is open. I want to be able to transfer the data even if the app is closed, or at least show an alert to open the app when you get the NFC tag near to the mobile. I am using react-native-nfc-manager.

我得到了这个代码,当应用程序打开时,它运行得很好。我希望即使应用程序关闭,我也能够传输数据,或者至少在你将NFC标签放在手机附近时显示打开应用程序的警报。我使用的是REACTION-NFC-MANAGER。


My goal is to make it work in Android and iOS, but it will be fine if it only works in Android for the moment.

我的目标是让它在Android和iOS上运行,但目前只在Android上运行就好了。




import React, { useEffect, useState } from 'react';
import {
Text,
SafeAreaView,
View,
TouchableOpacity,
StyleSheet
} from 'react-native';

import NfcManager, { NfcEvents } from 'react-native-nfc-manager';

const App = () => {
const [hasNfc, setHasNFC] = useState(null);

useEffect(() => {
const checkIsSupported = async () => {
const deviceIsSupported = await NfcManager.isSupported()

setHasNFC(deviceIsSupported)
if (deviceIsSupported) {
await NfcManager.start()
}
}

checkIsSupported()
}, [])

useEffect(() => {
readTag()
NfcManager.setEventListener(NfcEvents.DiscoverTag, (tag) => {
console.log('tag found', tag)
console.log(tag.ndefMessage[0].payload.toString('utf8'))

if (tag.ndefMessage && tag.ndefMessage.length > 0) {
// Iterate through the NDEF records
for (let i = 0; i < tag.ndefMessage.length; i++) {
const record = tag.ndefMessage[i];
console.log(record)

// Check the record's type (TNF) and payload
if (record.tnf === 1) { // TNF 1 corresponds to well-known types (e.g., text or URI)
// Assuming the payload is text data, you can decode it
const payloadText = String.fromCharCode.apply(null, record.payload);
console.log(`NDEF Record ${i + 1}: ${payloadText}`);
}
}
}
})

return () => {
NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
}
}, [])

const readTag = async () => {
await NfcManager.registerTagEvent();
}

if (hasNfc === null) return null;

if (!hasNfc) {
return (
<View style={styles.sectionContainer}>
<Text>NFC not supported</Text>
</View>
)
}

return (
<SafeAreaView style={styles.sectionContainer}>

</SafeAreaView>
);


}

const styles = StyleSheet.create({
sectionContainer: {
backgroundColor: 'white',
flex: 1,
alignItems: 'center'
},
})



export default App;



更多回答

can you please clarify whether you want it to happen when app is in background or after app is killed/closed? both are different cases!

你能澄清一下,你是希望它发生在APP在后台还是在APP被杀死/关闭之后?两者是不同的情况!

@KarthikSuthan when the app is closed

@KarthikSuthan当应用程序关闭时

优秀答案推荐

You cannot read from NFC when your app is closed (because obviously your app is not running), there are 2 different ways to configure a specific NFC Tag to start your app in the foreground (one for iOS and one for Android).

当你的应用程序关闭时,你不能读取NFC(因为很明显你的应用程序没有运行),有两种不同的方法来配置特定的NFC标签来在前台启动你的应用程序(一种用于iOS,另一种用于Android)。


Then once your App has been started by a correctly configured NFC Tag you can then read it.

然后,一旦你的应用被正确配置的NFC标签启动,你就可以阅读它了。



In Android add to AndroidManifest.xml new intent-filter

在Android中添加到AndroidManifest.xml新的意图过滤器


<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/your-stream" />
</intent-filter>

It will launch your app.

它将启动您的应用程序。


更多回答

Have you any docs on how to do this?

你有关于如何做到这一点的文档吗?

Android Application Record can cause install/opening of an App developer.android.com/guide/topics/connectivity/nfc/nfc#aar and for Apple adding a URL that is a "Universal Link" should open the App developer.apple.com/documentation/corenfc/…

安卓应用程序记录可能会导致安装/打开应用程序文档,对于苹果公司来说,添加一个作为“通用链接”的URL应该会打开应用程序开发人员.apple.com/developer.android.com/guide/topics/connectivity/nfc/nfc#aar/corenfc/…

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com