2017-04-11 319 views
1

我試圖從NFC標籤讀取數據。如果我使用Play Store的「NfcV-reader」應用程序(由ST Microelectronics,標籤製造商編寫),那麼我可以確認該標籤包含帶有MIME數據的NDEF記錄。 MIME類型是「應用程序/ MyApp的」和MIME有效載荷是「0123」,如下圖所示:Android NFC:正在接收TECH_DISCOVERED,期待NDEF_DISCOVERED

我想我自己的應用程序啓動時這個標籤是由Android的認可。我用我認爲是正確的意圖過濾器創建了該應用程序。這裏的AndroidManifest XML:

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <activity android:name=".ActivityNfcReader"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.ACTION_NDEF_DISCOVERED"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
       <data android:mimeType="application/myapp"/> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

我已經下載了應用一臺Nexus 6P設備(Android版本6.0.1),將設備設置到主屏幕,並與字符串「NFC」的過濾器開始的logcat 。下面是我得到:

04-11 16:23:33.108 4050 4050 D NativeNfcTag: Connect to a tech with a different handle 
04-11 16:23:33.273 4050 3909 D NativeNfcTag: Starting background presence check 
04-11 16:23:33.278 918 4148 I ActivityManager: START u0 {flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras)} from uid 1027 on display 0 
04-11 16:23:33.343 918 3469 I ActivityManager: START u0 {act=android.nfc.action.TECH_DISCOVERED cmp=com.google.android.tag/com.android.apps.tag.TagViewer (has extras)} from uid 1027 on display 0 
04-11 16:23:35.430 4050 3909 D NativeNfcTag: Tag lost, restarting polling loop 
04-11 16:23:35.432 4050 3909 D NfcService: Discovery configuration equal, not updating. 
04-11 16:23:35.433 4050 3909 D NativeNfcTag: Stopping background presence check 

我的問題是:爲什麼Android的調度「android.nfc.action.TECH_DISCOVERED」當我期待「android.nfc.action.NDEF_DISCOVERED」?

EDIT 1

我使用另一應用中, 「NFC TagInfo」,驗證標記的內容(接收低於第一註釋之後加入更多信息)。掃描的標籤顯示爲NFC TagInfo如下:最後在這裏

是呈現標籤到Android設備的logcat的:

04-12 09:30:48.609 3829 3829 D NativeNfcTag: Connect to a tech with a different handle 
04-12 09:30:48.775 3829 10744 D NativeNfcTag: Starting background presence check 
04-12 09:30:50.661 917 6053 I ActivityManager: START u0 {cmp=at.mroland.android.apps.nfctaginfo/.TagViewer (has extras)} from uid 10103 on display 0 
04-12 09:30:50.834 917 935 I ActivityManager: Displayed at.mroland.android.apps.nfctaginfo/.TagViewer: +160ms 

EDIT 2 (在解決問題後用正確的logcat添加正確的logcat)

問題出在intent過濾器上。請參閱下面的接受答案。以下是現在已解決問題的logcat行爲:

04-12 12:14:48.237 3829 3829 D NativeNfcTag: Connect to a tech with a different handle 
04-12 12:14:49.371 3829 4052 E BrcmNfcNfa: rw_i93_process_timeout(): retry_count = 1 
04-12 12:14:49.422 3829 14236 D NativeNfcTag: Starting background presence check 
04-12 12:14:49.424 917 4427 I ActivityManager: START u0 {flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras)} from uid 1027 on display 0 
04-12 12:14:49.457 917 6053 I ActivityManager: START u0 {act=android.nfc.action.NDEF_DISCOVERED typ=application/myapp cmp=com.dummy.nfc.reader/.ActivityNfcReader (has extras)} from uid 1027 on display 0 
04-12 12:14:49.553 917 935 I ActivityManager: Displayed com.dummy.nfc.reader/.ActivityNfcReader: +91ms (total +106ms) 
+0

你驗證(例如與像NFC TagInfo的應用程序)是Android的實際暴露了'Ndef'標籤技術爲您的設備上的標籤?請注意,由於Type 5 Tag技術最近才由NFC論壇指定,因此並非所有Android設備都支持所有NFC-V標籤上的NDEF。 –

+0

謝謝邁克爾。我已經使用NFC TagInfo應用的屏幕截圖更新了問題。該應用程序建議Android將標籤識別爲NfcV(android.nfc.tech.NfcV)並支持NDEF(android.nfc.tech.Ndef)。 – pfp

回答

1

您對NDEF消息的意圖過濾器是錯誤的。您當前有

<intent-filter> 
    <action android:name="android.intent.action.ACTION_NDEF_DISCOVERED"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
    <data android:mimeType="application/myapp"/> 
</intent-filter> 

雖然MIME類型與您的標記上的MIME類型匹配,但意圖操作不正確。發現NDEF消息的正確動作是"android.nfc.action.NDEF_DISCOVERED"。因此,你需要你的意圖過濾器改成這樣:

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

謝謝邁克爾 - 那是現貨。我已經通過第二次編輯更新了原始問題,以說明解決問題時的logcat行爲。我非常感謝你的幫助。 – pfp