Android NFC sensing and read tag data at the same time

拟墨画扇 提交于 2019-12-25 07:19:07

问题


I have a question about the 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.

Second time sensing (my application is launched), I can read the data from NFC tag.

Is it possible just sensing once that can launch my application and get the data from tag?


回答1:


In 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()..

     /**
      * 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() ...

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



回答2:


Use the below pattern (from here). Summary:

  1. The foreground mode lets you capture scanned tags in the form of intents sent to onNewIntent. An onResume will follow the onNewIntent call, so we'll process the intent there. But onResume can also come from other sources, so we add a boolean variable to make sure we only process each new intent once.

  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.

    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();
        }
    
    }
    


来源:https://stackoverflow.com/questions/14457055/android-nfc-sensing-and-read-tag-data-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!