Android nfc to read card from samsung nexus

…衆ロ難τιáo~ 提交于 2019-12-06 13:37:48

You need to add the opennfc library to your Eclipse project.

Right click on the project and select Properties, the go to Java Build Path|Libraries. Click Add external JARs and select your opennfc library. Finally, Click on the Order and Export tab and make sure that your opennfc library is checked for export.

You cannot read a card using Nexus S while using Open NFC. At least not yet.

The Open NFC stack is hardware independant and uses an abstraction layer, called the HAL. The only available HALs for Open NFC are for Inside Secure hardware, while Nexus S uses NXP hardware.

You dont write what kind of card you are trying to read, but probably its achievable by using the Android api. Among other, I was able to read from and write to a MIFARE card.

You need to filter for technology discovered

<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>

then, in the onNewIntent method

    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
     MifareClassic clas = MifareClassic.get(tagFromIntent);
            try
            {
                clas.connect();
                if (!DefaultAuthenticate(clas))
                    throw new Exception("Unable to authenticate using default key");
                String myData = new String(clas.readBlock(clas
                            .sectorToBlock(MY_DATA_SECTOR)), "US-ASCII");
                clas.close();
            } catch (Exception ex)
            {
                ShowMessage(ex.getMessage());
            } 

DefaultAnthenticate is my method that authenticates using default MIFARE keys:

private Boolean DefaultAuthenticate(MifareClassic clas) throws IOException
    {
        Boolean result = false;
        result = clas.authenticateSectorWithKeyA(MY_DATA_SECTOR,
                MifareClassic.KEY_DEFAULT);
        result = result
                && clas.authenticateSectorWithKeyB(MY_DATA_SECTOR,
                        MifareClassic.KEY_DEFAULT);
        return result;
    }

The code could use some refactoring and is a bit chopped up, but i think it shows what's going on. I hope it will help you on your own search - i suggest visiting http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html

The OpenNFC stack SDK add-on package provided on sourceforg comes with a custom kernel which emulates the NFC controller (see the SDK doc that comes with the package). So I doubt if you are talking to the actual HW without changing the driver (open_nfc_driver.ko) to do so. The Porting Guide (also included in the package) discusses how to port the driver to real hardware (section 4.5 Adapting the porting to a real hardware platform).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!