简体   繁体   English

Android NFC感应并同时读取标签数据

[英]Android NFC sensing and read tag data at the same time

I have a question about the android NFC. 我对android NFC有疑问。

I have already done the function about read and write, but still have one problem. 我已经完成了有关读写的功能,但是仍然有一个问题。

I wrote the AAR in my tag, after first sensing, it can launch my application. 我在自己的标签中写了AAR,首先感觉到它可以启动我的应用程序。

Second time sensing (my application is launched), I can read the data from NFC tag. 第二次检测(我的应用程序已启动),我可以从NFC标签读取数据。

Is it possible just sensing once that can launch my application and get the data from tag? 是否有可能检测到一次可以启动我的应用程序并从标签获取数据的情况?

Use the below pattern (from here ). 使用以下模式(从此处开始 )。 Summary: 摘要:

  1. The foreground mode lets you capture scanned tags in the form of intents sent to onNewIntent. 前台模式可让您以发送到onNewIntent的Intent的形式捕获扫描的标签。 An onResume will follow the onNewIntent call, so we'll process the intent there. onResume将跟随onNewIntent调用,因此我们将在那里处理意图。 But onResume can also come from other sources, so we add a boolean variable to make sure we only process each new intent once. 但是onResume也可以来自其他来源,因此我们添加了一个布尔变量,以确保仅对每个新意图进行一次处理。

  2. An intent is also present when the activity is launched. 启动活动时也存在一个意图。 By initializing the boolean variable to false, we fit it into the above flow - an your problem should be fixed. 通过将boolean变量初始化为false,我们将其适合上​​述流程-应该解决您的问题。

     protected boolean intentProcessed = false; public void onNewIntent(Intent intent) { Log.d(TAG, "onNewIntent"); // onResume gets called after this to handle the intent intentProcessed = false; setIntent(intent); } protected void onResume() { super.onResume(); // your current stuff if(!intentProcessed) { intentProcessed = true; processIntent(); } } 

In AndroidManifest - 在AndroidManifest中-

  <activity
        android:name=".TagDiscoverer"
        android:alwaysRetainTaskState="true"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:screenOrientation="nosensor" >
        <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.NDEF_DISCOVERED" />
            <action android:name="android.nfc.action.TECH_DISCOVERED" />
            <action android:name="android.nfc.action.TAG_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
        </intent-filter>

        <meta-data
            android:name="android.nfc.action.TECH_DISCOVERED"/>
    </activity>

you should initiate the NFC adopter in OnCreate().. 您应该在OnCreate()中启动NFC采用者。

     /**
      * Initiates the NFC adapter
     */
  private void initNfcAdapter() {
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
   }

Now in OnResume() ... 现在在OnResume()中...

  @Override
  protected void onResume() {
  super.onResume();
  if (nfcAdapter != null) {
    nfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
  }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM