Lucky Dog

[안드로이드] NFC 수신 본문

안드로이드

[안드로이드] NFC 수신

Poohya 2015. 4. 3. 10:55


1. Activity 의 경우


1-1. manifest


    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <data android:mimeType="*/*" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
 
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
 
    <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />
    </intent-filter>
 
    <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/nfc_tech_filter" />

          

1-2. xml file (nfc_tech_filter.xml)


<?xml version="1.0" encoding="utf-8"?>
 
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.NfcF</tech>
        <tech>android.nfc.tech.NfcV</tech>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
        <tech>org.nfcforum.ndef.type2</tech>
    </tech-list>
</resources>




2. foreground에서 NFC 수신


2-1. onResume()


 
        PendingIntent pIntent = PendingIntent.getActivity(this0new Intent(thisgetClass()), 0);
 
        // NFC filter
        IntentFilter intentFilter_1 = new IntentFilter();
 
        intentFilter_1.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            intentFilter_1.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            e.printStackTrace();
        }
 
        IntentFilter intentFilter_2 = new IntentFilter();
        intentFilter_2.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
 
        try {
            intentFilter_2.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            e.printStackTrace();
        }
 
        IntentFilter intentFilter_3 = new IntentFilter();
        intentFilter_3.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
 
        try {
            intentFilter_3.addDataType("*/*");
        } catch ( MalformedMimeTypeException e) {
            e.printStackTrace();
        }
 
        IntentFilter[] intentFilters = new IntentFilter[]{intentFilter_1, intentFilter_2, intentFilter_3};
 
        String[][] techLists = new String[][]{new String[]{NfcA.class.getName(),
                NfcB.class.getName(), NfcF.class.getName(), NfcV.class.getName(),
                IsoDep.class.getName(), MifareClassic.class.getName(),
                MifareUltralight.class.getName(), Ndef.class.getName()}};
 
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        mNfcAdapter.enableForegroundDispatch(this, pIntent, intentFilters, techLists);


2-2. onpause()


mNfcAdapter.disableForegroundDispatch(this);







Comments