2016-06-07 2 views
1

TECH_DISCOVERED 인 텐트 필터를 사용하기 위해 NFC 전경 디스패치 등록을 변경 했으므로 NFC 태그를 처리하기 위해 여러 응용 프로그램 중에서 선택해야합니다. 태그가 발견되면 앱에 직접 NFC 인 텐트를 수신하는 방법이 있습니까?의도 선택기가 NFC TECH_DISCOVERED 필터 및 포 그라운드 디스패치 시스템으로 표시됩니다.

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); 
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); 
    IntentFilter[] mFilters = new IntentFilter[] { 
      ndef 
    }; 
    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); 
    adapter.enableForegroundDispatch(activity, pendingIntent, mFilters, null); 
} 

public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) { 
    adapter.disableForegroundDispatch(activity); 
} 
+0

아, 이제 내가 한 일을 얻었습니다 ;-) –

답변

1

TECH_DISCOVERED 의도 필터에는 기술 목록이 필요합니다. 따라서 현재의 전경 파견 등록은 태그 기술을 전혀 수신하지 않습니다. 매니페스트를 통해 그 의도 필터를 등록 할 때

, 당신은

<meta-data android:name="android.nfc.action.TECH_DISCOVERED" 
      android:resource="@xml/nfc_tag_filter" /> 

이 그렇게 사용합니다. 마찬가지로 메서드를 사용하여 전경 디스패치에 등록 할 때 마지막 인수 인 enableForegroundDispatch()에 기술 목록 (문자열 배열 배열)을 지정해야합니다. 예 : 가능한 모든 태그 기술을 수신하도록 (즉, NFC-A 또는 NFC-B 또는 NFC-F 또는 NFC-V 또는 NFC-바코드), 다음을 사용 :

IntentFilter[] filters = new IntentFilter[] { 
     new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED), 
}; 
String[][] techList = new String[][] { 
     new String[] { NfcA.class.getName() }, 
     new String[] { NfcB.class.getName() }, 
     new String[] { NfcF.class.getName() }, 
     new String[] { NfcV.class.getName() }, 
     new String[] { NfcBarcode.class.getName() }, 
}; 
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); 

주 당신은 필터링하려는 경우 그러나

adapter.enableForegroundDispatch(activity, pendingIntent, null, null); 

TAG_DISCOVERED 의도는이 경우 앱에 전달되므로주의 : 전경 파견 시스템을 통해 태그, 당신은 또한 단순히 포괄 전경 파견을 사용할 수 있습니다.

관련 문제