【发布时间】:2011-07-01 07:35:21
【问题描述】:
我想从三星 Nexus 手机读取卡片,但 android nfc api 没有提供足够的选项。我也尝试过使用第三方 api 名称“open nfc”,但它给出了不支持 api 的错误。谁能提供我从卡中读取数据的代码。我有读取标签但没有读取卡片的代码。这是下载open 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");
}
}
【问题讨论】:
-
你读过这个:developer.android.com/guide/topics/nfc/index.html#ndef吗?您的问题有点模糊,您需要更具体地说明您要实现的目标。更好的是,发布您编写的代码,解释它的失败之处,我们可以尝试帮助您。
-
这个使用我上面提到的api。它给出了 opnenfc 失败的错误。
-
请从您的 logcat 中发布完整的错误,包括任何异常的堆栈跟踪。
-
我已经浏览了 developer.android.com,但我没有得到解决方案。我想要一个通用代码来从三星 nexus 手机中读取卡片。
-
这是错误:需要不可用的共享库 org.opennfc_library 失败