2014-12-10 6 views
0

외부 레코드 유형을 포함하도록 NFC 태그를 작성했습니다.NFC NDEF 외부 유형에서 활동을 시작할 수 없습니다.

Record Type:External 
Type:/com.domainname:link-uuid 
xyzxyz-xyzxyz 

기술

NDEF 내용 그래서 난 내 태그가 올바르게 작성되었는지 확신

android.nfc.NfcA 
android.nfc.tech.MifareUltralight 
android.nfc.tech.Ndef 

지원 : 태그를 읽을 SmartQ를 사용

다음을 말한다.

휴대 전화에서 태그를 발견하고 유형이 외부 /com.domainname:link-uuid 인 경우에만 실행되도록 필터링 할 때 활동을 시작하려고합니까?

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

    <uses-feature 
     android:name="android.hardware.nfc" 
     android:required="true" /> 

    <uses-permission android:name="android.permission.NFC" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".NFCWrite" 
      android:configChanges="orientation|keyboardHidden" 
      android:label="@string/title_activity_nfcread" 
      android:launchMode="singleTask" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".NFCRead" 
      android:configChanges="orientation|keyboardHidden" 
      android:label="@string/title_activity_nfcread" 
      android:launchMode="singleTask" > 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data 
        android:host="ext" 
        android:pathPrefix="/com.domainname-uuid" 
        android:scheme="vnd.android.nfc" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

그리고 읽기 작업은 다음과 같습니다 :이 활동으로 잘못을하고있는 중이 야 무엇

package com.domainname.android.interlink.nfc; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.nfc.NfcManager; 
import android.nfc.Tag; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.util.Log; 
import android.widget.Toast; 

import java.io.UnsupportedEncodingException; 
import java.util.Arrays; 


public class NFCRead extends Activity { 

    private static final int PENDING_INTENT_TECH_DISCOVERED = 1; 
    private static final int PENDING_INTENT_NDEF_DISCOVERED = 1; 
    private NfcAdapter mNfcAdapter; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     Log.i("NFCRead.onCreate","Running..."); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_nfcread); 

     // Resolve the intent that started us: 
     try { 
      resolveIntent(this.getIntent()); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Called when the activity gets focus. 
    */ 
    @Override 
    public void onResume() { 
     Log.i("NFCRead.onResume","Running..."); 
     super.onResume(); 

     // Retrieve an instance of the NfcAdapter ("connection" to the NFC system service): 
     NfcManager nfcManager = (NfcManager) this.getSystemService(Context.NFC_SERVICE); 
     if (nfcManager != null) { 
      mNfcAdapter = nfcManager.getDefaultAdapter(); 
     } 

     if (mNfcAdapter != null) { 
      // The isEnabled() method, if invoked directly after the NFC service 
      // crashed, returns false regardless of the real setting (Android 2.3.3+). 
      // As a nice side-effect it re-establishes the link to the correct instance 
      // of NfcAdapter. Therefore, just execute this method twice whenever we 
      // re-request the NfcAdapter, so we can be sure to have a valid handle. 
      try { 
       mNfcAdapter.isEnabled(); 
      } catch (NullPointerException e) { 
       // Drop NullPointerException that is sometimes thrown 
       // when NFC service crashed 
      } 
      try { 
       mNfcAdapter.isEnabled(); 
      } catch (NullPointerException e) { 
       // Drop NullPointerException that is sometimes thrown 
       // when NFC service crashed 
      } 

      // Create a PendingIntent to handle discovery of Ndef and NdefFormatable tags: 
      PendingIntent pi = createPendingResult(
        PENDING_INTENT_NDEF_DISCOVERED, 
        new Intent(), 
        0); 
      if (pi != null) { 
       try { 
        // Enable foreground dispatch for Ndef and NdefFormatable tags: 
        mNfcAdapter.enableForegroundDispatch(
          this, 
          pi, 
          new IntentFilter[]{ 
            new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) 
          }, 
          new String[][]{ 
            new String[]{"android.nfc.tech.Ndef"} 
          }); 
       } catch (NullPointerException e) { 
        // Drop NullPointerException that is sometimes thrown 
        // when NFC service crashed 
       } 
      } 
     } 
    } 

    /** 
    * Called when the activity loses focus. 
    */ 
    @Override 
    public void onPause() { 
     Log.i("NFCRead.onPause","Running..."); 
     super.onPause(); 

     if (mNfcAdapter != null) { 
      try { 
       // Disable foreground dispatch: 
       mNfcAdapter.disableForegroundDispatch(this); 
      } catch (NullPointerException e) { 
       // Drop NullPointerException that is sometimes thrown 
       // when NFC service crashed 
      } 
     } 
    } 

    /** 
    * Called when activity receives a new intent. 
    */ 
    @Override 
    public void onNewIntent(Intent data) { 
     Log.i("NFCRead.onNewIntent","Running..."); 
     // Resolve the intent that re-invoked us: 
     try { 
      resolveIntent(data); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Called when a pending intent returns (e.g. our foreground dispatch). 
    */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Log.i("NFCRead.onActivityResult","Running..."); 
     switch (requestCode) { 
      case PENDING_INTENT_TECH_DISCOVERED: 
       // Resolve the foreground dispatch intent: 
       try { 
        resolveIntent(data); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 
       break; 
     } 
    } 

    /** 
    * Method to handle any intents that triggered our activity. 
    * @param data    The intent we received and want to process. 
    * 
    */ 
    private void resolveIntent(Intent data) throws UnsupportedEncodingException { 
     Log.i("resolveIntent","Running..."); 
     this.setIntent(data); 

     String action = data.getAction(); 

     // We were started from the recent applications history: just show our main activity 
     // (otherwise, the last intent that invoked us will be re-processed) 
     if ((data.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { return; } 

     // Intent is a tag technology (we are sensitive to Ndef, NdefFormatable) or 
     // an NDEF message (we are sensitive to URI records with the URI http://www.mroland.at/) 
     if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { 
      Log.i("TECH_DISCOVERED", "TECH_DISCOVERED"); 
      if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { 
       // The reference to the tag that invoked us is passed as a parameter (intent extra EXTRA_TAG) 
       Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG); 

       Log.i("NDEF Discovered:", tag.toString()); 

       // Let's read the tag whenever we are not in write mode 

       // Retrieve information from tag and display it 
       StringBuilder tagInfo = new StringBuilder(); 

       // Get tag's UID: 
       byte[] uid = tag.getId(); 
       tagInfo.append("UID: ").append(new String(uid, "UTF-8")).append("\n\n"); 

       // Get tag's NDEF messages: The NDEF messages are passed as parameters (intent 
       // extra EXTRA_NDEF_MESSAGES) and have to be casted into an NdefMessage array. 
       Parcelable[] ndefRaw = data.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
       NdefMessage[] ndefMsgs = null; 
       if (ndefRaw != null) { 
        ndefMsgs = new NdefMessage[ndefRaw.length]; 
        for (int i = 0; i < ndefMsgs.length; ++i) { 
         // Cast each NDEF message from Parcelable to NdefMessage: 
         ndefMsgs[i] = (NdefMessage) ndefRaw[i]; 
        } 
       } 

       // Find External records: 

       if (ndefMsgs != null) { 
        // Iterate through all NDEF messages on the tag: 
        for (NdefMessage ndefMsg : ndefMsgs) { 
         // Get NDEF message's records: 
         NdefRecord[] records = ndefMsg.getRecords(); 

         if (records != null) { 
          // Iterate through all NDEF records: 
          for (NdefRecord record : records) { 
           // Test if this record is a URI record: 
           if ((record.getTnf() == NdefRecord.TNF_EXTERNAL_TYPE) 
             && Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) { 
            // Get record payload: 

            //         // Drop prefix identifier byte and convert remaining URL to string (UTF-8): 
            //         String uri = new String(Arrays.copyOfRange(payload, 1, payload.length), Charset.forName("UTF-8")); 

            // Use UriRecordHelper to decode URI record payload: 
            String uuid = new String(record.getPayload(), "UTF-8"); 

            tagInfo.append("UUID: "); 
            tagInfo.append(uuid); 
            tagInfo.append("\n"); 
           } 
          } 
         } 
        } 
       } 

       // Show our tag detected dialog (with the tag information passed as parameter): 
       Toast toast = Toast.makeText(getApplicationContext(), tagInfo.toString(), Toast.LENGTH_SHORT); 
       toast.show(); 
      } 
     } 
    } 

} 

가 호출되지 않습니다

응용 프로그램이 실행되는 경우에도 다음과 같이

내 manifest.xml입니다 . 따라서 태그는 내 응용 프로그램에서 읽히지 않습니다.

나는 그것을 배울 수 있도록 자습서의 대부분의 코드를 사용하고 있습니다. 태그를 쓰는 작업을 할 수 있었지만 읽을 수는 없습니다.

제게 잘못 가고있는 부분을 알려주십시오. 감사.

답변

0

문제가 해결되었습니다.

문제는 내가 태그를 쓸 때 /를 도메인 이름 앞에 붙이는 것이 었습니다.

NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE,"/com.domainname:link1-uuid".getBytes(),new byte[]{},msgBytes); 

에 :

그래서 나는 변경

NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE,"com.domainname:link1-uuid".getBytes(),new byte[]{},msgBytes); 

활동은 이제 완벽하게 시작합니다.