2014-01-16 2 views
1

NFC 작동 방식의 예를 발견했습니다. 그러나 NFC 카드를 붙일 때 Resume/OnPause가 호출됩니다. 왜? 함수 : onNewIntent가 호출되지 않았습니다.Android에서 NFC가 작동하지 않습니다.

활동의 코드 :

public class AMain extends Activity { 

    public static final String MIME_TEXT_PLAIN = "text/plain"; 
    public static final String TAG = "NfcDemo"; 

    private TextView mTextView; 
    private NfcAdapter mNfcAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.amain); 

     mTextView = (TextView) findViewById(R.id.tv); 

     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 

     if (mNfcAdapter == null) { 
      // Stop here, we definitely need NFC 
      Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 

     } 

     if (!mNfcAdapter.isEnabled()) { 
      mTextView.setText("NFC is disabled."); 
     } else { 
      mTextView.setText("NFC is enabled."); 
     } 

     handleIntent(getIntent()); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     /* 
     * It's important, that the activity is in the foreground (resumed). Otherwise 
     * an IllegalStateException is thrown. 
     */ 
     setupForegroundDispatch(this, mNfcAdapter); 
    } 

    @Override 
    protected void onPause() { 
     /* 
     * Call this before onPause, otherwise an IllegalArgumentException is thrown as well. 
     */ 
     stopForegroundDispatch(this, mNfcAdapter); 

     super.onPause(); 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     /* 
     * This method gets called, when a new Intent gets associated with the current activity instance. 
     * Instead of creating a new activity, onNewIntent will be called. For more information have a look 
     * at the documentation. 
     * 
     * In our case this method gets called, when the user attaches a Tag to the device. 
     */ 
     handleIntent(intent); 
    } 

    private void handleIntent(Intent intent) { 
     String action = intent.getAction(); 
     Log.d(TAG,action); 
     if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { 

      String type = intent.getType(); 
      if (MIME_TEXT_PLAIN.equals(type)) { 

       Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
       new NdefReaderTask().execute(tag); 

      } else { 
       Log.d(TAG, "Wrong mime type: " + type); 
      } 
     } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { 

      // In case we would still use the Tech Discovered Intent 
      Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
      String[] techList = tag.getTechList(); 
      String searchedTech = Ndef.class.getName(); 

      for (String tech : techList) { 
       if (searchedTech.equals(tech)) { 
        new NdefReaderTask().execute(tag); 
        break; 
       } 
      } 
     } 
    } 

    /** 
    * @param activity The corresponding {@link Activity} requesting the foreground dispatch. 
    * @param adapter The {@link NfcAdapter} used for the foreground dispatch. 
    */ 
    public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) { 
     final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); 
     intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); 

     IntentFilter[] filters = new IntentFilter[1]; 
     String[][] techList = new String[][]{}; 

     // Notice that this is the same filter as in our manifest. 
     filters[0] = new IntentFilter(); 
     filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); 
     filters[0].addCategory(Intent.CATEGORY_DEFAULT); 
     try { 
      filters[0].addDataType(MIME_TEXT_PLAIN); 
     } catch (MalformedMimeTypeException e) { 
      throw new RuntimeException("Check your mime type."); 
     } 

     adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); 
    } 

    /** 
    * @param activity The corresponding {@link BaseActivity} requesting to stop the foreground dispatch. 
    * @param adapter The {@link NfcAdapter} used for the foreground dispatch. 
    */ 
    public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) { 
     adapter.disableForegroundDispatch(activity); 
    } 

    /** 
    * Background task for reading the data. Do not block the UI thread while reading. 
    * 
    * @author Ralf Wondratschek 
    * 
    */ 
    private class NdefReaderTask extends AsyncTask<Tag, Void, String> { 

     @Override 
     protected String doInBackground(Tag... params) { 
      Tag tag = params[0]; 

      Ndef ndef = Ndef.get(tag); 
      if (ndef == null) { 
       // NDEF is not supported by this Tag. 
       return null; 
      } 

      NdefMessage ndefMessage = ndef.getCachedNdefMessage(); 

      NdefRecord[] records = ndefMessage.getRecords(); 
      for (NdefRecord ndefRecord : records) { 
       if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) { 
        try { 
         return readText(ndefRecord); 
        } catch (UnsupportedEncodingException e) { 
         Log.e(TAG, "Unsupported Encoding", e); 
        } 
       } 
      } 

      return null; 
     } 

     private String readText(NdefRecord record) throws UnsupportedEncodingException { 
      /* 
      * See NFC forum specification for "Text Record Type Definition" at 3.2.1 
      * 
      * http://www.nfc-forum.org/specs/ 
      * 
      * bit_7 defines encoding 
      * bit_6 reserved for future use, must be 0 
      * bit_5..0 length of IANA language code 
      */ 

      byte[] payload = record.getPayload(); 

      // Get the Text Encoding 
      String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; 

      // Get the Language Code 
      int languageCodeLength = payload[0] & 0063; 

      // String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII"); 
      // e.g. "en" 

      // Get the Text 
      return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      if (result != null) { 
       mTextView.setText("Read content: " + result); 
      } 
     } 
    } 
} 

매니페스트 파일 :

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.nfc" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-permission android:name="android.permission.NFC" /> 
    <uses-feature 
     android:name="android.hardware.nfc" 
     android:required="true" /> 
    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="19" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.nfc.AMain" 
      android:label="@string/app_name" > 
      <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"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
       <data android:mimeType="text/plain" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.nfc.action.TAG_DISCOVERED"/> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
      </intent-filter> 
      <meta-data android:name="android.nfc.action.TECH_DISCOVERED" 
       android:resource="@xml/nfc_tech_filter" /> 
     </activity> 

    </application> 

</manifest> 
+0

작동하지 않는 기능은 무엇입니까? – Shoban

+0

onNewIntent는 – Vyacheslav

답변

3

NfcAdapter.ACTION_TAG_DISCOVERED를 의도 필터에 추가해야합니다. 현재 의도 필터 (filters [0] .addAction (NfcAdapter.ACTION_NDEF_DISCOVERED);)는 NDEF 메시지를 포함하는 NFC 태그를 포착합니다. 당신은 당신이 원하는 것을 얻을해야

 filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);

에 필터를 변경하여 .

+0

감사합니다. 나는 그것을 내일 시험하고 쓸 것이다 – Vyacheslav

+0

너의 쓰기이었다. – Vyacheslav

+0

행복한 코딩 :) –

0

안녕하세요 당신이 당신의 매니페스트에서 <uses-permission android:name="android.permission.NFC" />을 설정할 수 있나요?

+0

을 호출하지 않습니다. 물론 있습니다. – Vyacheslav

관련 문제