简体   繁体   English

Android nfc从三星nexus读取卡

[英]Android nfc to read card from samsung nexus

I want to read a card from Samsung Nexus phone but the android nfc api does not provide enough options.我想从三星 Nexus 手机读取卡片,但 android nfc api 没有提供足够的选项。 i've also tried using a third party api names "open nfc" but the it gives error of not supporting the api.我也尝试过使用第三方 api 名称“open nfc”,但它给出了不支持 api 的错误。 Can anyone provide me the code to read data from a card.谁能提供我从卡中读取数据的代码。 I have the code to read a tag but not to read a card.我有读取标签但没有读取卡片的代码。 Here is the link to download the open nfc api.这是下载打开的 nfc api 的链接。

http://sourceforge.net/projects/open-nfc/files/Open%20NFC%204.3%20beta%20%2810381%29/ http://sourceforge.net/projects/open-nfc/files/Open%20NFC%204.3%20beta%20%2810381%29/

Any help is appreciated.任何帮助表示赞赏。

This is the code i used.这是我使用的代码。 Its giving an error of opennfc failing...它给出了opennfc失败的错误......

   public class NFCone  extends Activity implements CardDetectionEventHandler,     ReadCompletionEventHandler,NfcTagDetectionEventHandler{

CardListenerRegistry i=null;
CardDetectionEventHandler hand=null;
NfcManager nfcMngr = null;
NfcTagManager mNfcTagManager=null;
NfcTagDetectionEventHandler tagHand=null;
ReadCompletionEventHandler readHand=null;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main1); 
    System.out.println("onCreate");
    try
    {
        nfcMngr.start();
        i.registerCardListener(NfcPriority.MAXIMUM, hand);
    }
    catch(Exception e)
    {

    }
}
protected void onResume() {
    // TODO Auto-generated method stub

    super.onResume();
    System.out.println("onResume");

    Toast.makeText(this, "NDEF reader Starting ... ", Toast.LENGTH_SHORT)
            .show();
    try{
    if (mNfcTagManager != null) {

        mNfcTagManager.registerTagListener(NfcPriority.MAXIMUM, tagHand);
        mNfcTagManager.registerMessageReader(NdefTypeNameFormat.WELL_KNOWN,
                "U", NfcPriority.MINIMUM, this);
    }
    }
    catch(Exception e)
    {       }
    }
protected void onPause() 
{   
    super.onPause();
    System.out.println("onPause");
    mNfcTagManager.unregisterMessageReader(readHand);
    mNfcTagManager.unregisterTagListener(tagHand);

}
protected void onDestroy() 

{ 
    super.onDestroy();
    System.out.println("onDestroy");
    i.unregisterCardListener(hand);
    try{
    nfcMngr.stop();
    }
    catch(Exception e)
    {       }
}
public void onCardDetected(Connection connection) {
    System.out.println("onCardDetected");

    //ConnectionProperty[] con = connection.getProperties();

}
public void onCardDetectedError(NfcErrorCode what) {
    System.out.println("onCardDetectedError");
    // TODO Auto-generated method stub

}
private void startBrowserOn(String url) {
    System.out.println("startBrowserOn");
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
public void onReadError(NfcErrorCode what) {
    System.out.println("onReadError");
}
public void onTagRead(NdefMessage message) {
    {
        System.out.println("onTagRead");
        if (message != null) {

            Vector<NdefRecord> records = message.getRecords();

            for (int i = 0; i < records.size(); i++) {

                if (UriRecord.isUriRecord(records.elementAt(i))) {

                    UriRecord uri;

                    try {
                        try {

                            uri = new UriRecord(records.elementAt(i));
                            startBrowserOn(uri.getUri().toString());
                        } catch (NfcException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    } catch (URISyntaxException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();

                        Toast.makeText(this, "URISyntaxException! ",
                                Toast.LENGTH_SHORT).show();
                    }

                    break;
                }}}}
}
    public void onTagDetected(NfcTagConnection connection) {
    System.out.println("onTagDetected");

}
    public void onTagDetectedError(NfcErrorCode what) {
    System.out.println("onTagDetectedError");

}
 }

You cannot read a card using Nexus S while using Open NFC.在使用 Open NFC 时,您无法使用 Nexus S 读取卡片。 At least not yet.至少现在还没有。

The Open NFC stack is hardware independant and uses an abstraction layer, called the HAL. Open NFC 堆栈独立于硬件并使用称为 HAL 的抽象层。 The only available HALs for Open NFC are for Inside Secure hardware, while Nexus S uses NXP hardware. Open NFC 唯一可用的 HAL 用于 Inside Secure 硬件,而 Nexus S 使用 NXP 硬件。

You dont write what kind of card you are trying to read, but probably its achievable by using the Android api.你不写你想读什么样的卡,但可能通过使用 Android api 来实现。 Among other, I was able to read from and write to a MIFARE card.其中,我能够读取和写入 MIFARE 卡。

You need to filter for technology discovered您需要过滤发现的技术

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

then, in the onNewIntent method然后,在onNewIntent方法中

    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: DefaultAnthenticate 是我使用默认 MIFARE 密钥进行身份验证的方法:

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我希望它对您自己的搜索有所帮助 - 我建议访问http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.ZFC35FDC70D5FC69D269883A822C7

You need to add the opennfc library to your Eclipse project.您需要将 opennfc 库添加到您的 Eclipse 项目中。

Right click on the project and select Properties , the go to Java Build Path|Libraries .右键单击项目和 select Properties , go 到Java Build Path|Libraries Click Add external JARs and select your opennfc library.单击Add external JARs和 select 您的 opennfc 库。 Finally, Click on the Order and Export tab and make sure that your opennfc library is checked for export.最后,单击Order and Export选项卡并确保检查您的 opennfc 库以进行导出。

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). 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.所以我怀疑你是否在不改变驱动程序(open_nfc_driver.ko)的情况下与实际硬件交谈。 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).移植指南(也包含在包中)讨论了如何将驱动程序移植到真实硬件(第 4.5 节使移植适应真实硬件平台)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM