Click here to Skip to main content
16,016,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hy..i have a task to make my kitkat-nexus to act as a tag. I have ACS 122U as reader. i have read the program example in this site http://blog.opendatalab.de/hack/2013/11/07/android-host-card-emulation-with-acr122/. then i tryed the code on my own eclipse.

main activity :

Java
public class MainActivity extends Activity implements OnMessageReceived, ReaderCallback {

    private NfcAdapter nfcAdapter;
    private ListView listView;
    private IsoDepAdapter isoDepAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.listView);
        isoDepAdapter = new IsoDepAdapter(getLayoutInflater());
        listView.setAdapter(isoDepAdapter);
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        Log.i("end of onCreate-----","onCreate HCE");
    }

    @Override
    public void onResume() {
        super.onResume();
        //nfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
            //  null);

        //nfcAdapter.disableReaderMode(this); //tambahan poipo
        Log.i("onResume---", "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        nfcAdapter.disableReaderMode(this);
        Log.i("onPause---", "onPause");
    }

    @Override
    public void onTagDiscovered(Tag tag) {
        IsoDep isoDep = IsoDep.get(tag);
        IsoDepTransceiver transceiver = new IsoDepTransceiver(isoDep, this);
        Thread thread = new Thread(transceiver);
        Log.i("dibawah thread", "ontagdiscovered");
        thread.start();
    }

    @Override
    public void onMessage(final byte[] message) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                isoDepAdapter.addMessage(new String(message));
                Log.i("didlmrun---", "onMessage");
            }
        });

        Log.i("diluarrun---", "onMessage");
    }

    @Override
    public void onError(Exception exception) {
        onMessage(exception.getMessage().getBytes());
    }
}



hostapduservice :

...
...
...

Java
@Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
        if (selectAidApdu(apdu)) {
            Log.i("HCEDEMO====", "Application selected====");
            return getWelcomeMessage();
        }
        else {
            Log.i("HCEDEMO======", "Received: =====" + new String(apdu));
            return getNextMessage();
        }
    }

...
...
...


then in the manifest file :

Java
<uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

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




    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service
            android:name=".MyHostApduService"
            android:exported="true"
            android:permission="android.permission.BIND_NFC_SERVICE" >
            <intent-filter>
                <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
            </intent-filter>

            <meta-data
                android:name="android.nfc.cardemulation.host_apdu_service"
                android:resource="@xml/apduservice" />
        </service>

         <activity
            android:name="de.grundid.hcedemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        </activity>

    </application>



ok,,when i ran the above source code,, i saw my acs122u blinking continously when i tapped my nexus near to it. but i didn't see the log.i(....) from hostapdu service. In the eclipse log cat, there were just some log.i from main activity. what should i do to bind that hostapdu service to main activity, so my nexus can act as a tag...??? thanks in advance... :-)
Posted
Comments
Member 11867599 28-Jul-15 0:54am    
Hi There
buddy.

what i suggest is to change in code every appear of "Log.i" by "System.out.println".

if you don have configured correctly the log system, it doesnt show anything, but internally it is working.


Plase excuse for my english gramma and hope this was helpful for you.


Jose
Hyosoka Poipo 4-Aug-15 22:16pm    
Thanks for your answer Mr.Jose... Btw... I have fixed this problem hehehehehe...:-)
Ummm..... I just move above code Android Studio and everything is good...:-)
So I think the problem was coming from my eclipse ADT, but It doesn't matter now...Hehehe...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900