[英]Android nfc to read card from samsung nexus
我想從三星 Nexus 手機讀取卡片,但 android nfc api 沒有提供足夠的選項。 我也嘗試過使用第三方 api 名稱“open nfc”,但它給出了不支持 api 的錯誤。 誰能提供我從卡中讀取數據的代碼。 我有讀取標簽但沒有讀取卡片的代碼。 這是下載打開的 nfc api 的鏈接。
http://sourceforge.net/projects/open-nfc/files/Open%20NFC%204.3%20beta%20%2810381%29/
任何幫助表示贊賞。
這是我使用的代碼。 它給出了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");
}
}
在使用 Open NFC 時,您無法使用 Nexus S 讀取卡片。 至少現在還沒有。
Open NFC 堆棧獨立於硬件並使用稱為 HAL 的抽象層。 Open NFC 唯一可用的 HAL 用於 Inside Secure 硬件,而 Nexus S 使用 NXP 硬件。
你不寫你想讀什么樣的卡,但可能通過使用 Android api 來實現。 其中,我能夠讀取和寫入 MIFARE 卡。
您需要過濾發現的技術
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
然后,在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 是我使用默認 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;
}
該代碼可以使用一些重構並且有點被切碎,但我認為它顯示了正在發生的事情。 我希望它對您自己的搜索有所幫助 - 我建議訪問http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.ZFC35FDC70D5FC69D269883A822C7
您需要將 opennfc 庫添加到您的 Eclipse 項目中。
右鍵單擊項目和 select Properties
, go 到Java Build Path|Libraries
。 單擊Add external JARs
和 select 您的 opennfc 庫。 最后,單擊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). 所以我懷疑你是否在不改變驅動程序(open_nfc_driver.ko)的情況下與實際硬件交談。 移植指南(也包含在包中)討論了如何將驅動程序移植到真實硬件(第 4.5 節使移植適應真實硬件平台)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.