我想读的三星Nexus手机,但Android的NFC API不提供足够的选项卡。我已经使用第三方API名称开放NFC也尝试过,但它给人的不支持API错误。任何人都可以向我提供的code从卡中读取数据。我有code读取标签,但不能读取卡。这里是下载开放API NFC链接。
的http://sourceforge.net/projects/open-nfc/files/Open%20NFC%204.3%20beta%20%2810381%29/
任何帮助是AP preciated。
这是code I使用。它给人的opennfc失败的错误...
公共类NFCone扩展活动实现CardDetectionEventHandler,ReadCompletionEventHandler,NfcTagDetectionEventHandler {CardListenerRegistry我= NULL;CardDetectionEventHandler手= NULL;NfcManager nfcMngr = NULL;NfcTagManager mNfcTagManager = NULL;NfcTagDetectionEventHandler tagHand = NULL;ReadCompletionEventHandler readHand = NULL;公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main1); 的System.out.println(的onCreate); 尝试 { nfcMngr.start(); i.registerCardListener(NfcPriority.MAXIMUM,手); } 赶上(例外五) { }}保护无效onResume(){ // TODO自动生成方法存根 super.onResume(); 的System.out.println(onResume); Toast.makeText(这一点,NDEF读者开始......,Toast.LENGTH_SHORT) 。显示(); 尝试{ 如果(mNfcTagManager!= NULL){ mNfcTagManager.registerTagListener(NfcPriority.MAXIMUM,tagHand); mNfcTagManager.registerMessageReader(NdefTypeNameFormat.WELL_KNOWN, U,NfcPriority.MINIMUM,这一点); } } 赶上(例外五) {} }保护无效的onPause(){ super.onPause(); 的System.out.println(的onPause); mNfcTagManager.unregisterMessageReader(readHand); mNfcTagManager.unregisterTagListener(tagHand);}保护无效的onDestroy(){ super.onDestroy(); 的System.out.println(的onDestroy); i.unregisterCardListener(手); 尝试{ nfcMngr.stop(); } 赶上(例外五) {}}公共无效onCardDetected(连接接口){ 的System.out.println(onCardDetected); // ConnectionProperty [] CON = connection.getProperties();}公共无效onCardDetectedError(NfcError code什么的){ 的System.out.println(onCardDetectedError); // TODO自动生成方法存根}私人无效startBrowserOn(字符串URL){ 的System.out.println(startBrowserOn); startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(URL)));}公共无效onReadError(NfcError code什么的){ 的System.out.println(onReadError);}公共无效onTagRead(NdefMessage消息){ { 的System.out.println(onTagRead); 如果(消息!= NULL){ 矢量<&NdefRecord GT;记录= message.getRecords(); 的for(int i = 0; I< records.size();我++){ 如果(UriRecord.isUriRecord(records.elementAt(ⅰ))){ UriRecord URI; 尝试{ 尝试{ URI =新UriRecord(records.elementAt(I)); startBrowserOn(uri.getUri()的toString()); }赶上(NfcException E){ // TODO自动生成catch块 e.printStackTrace(); } }赶上(的URISyntaxException E){ // TODO自动生成catch块 e.printStackTrace(); Toast.makeText(这一点,的URISyntaxException! Toast.LENGTH_SHORT).show(); } 打破; }}}}} 公共无效onTagDetected(NfcTagConnection连接){ 的System.out.println(onTagDetected);} 公共无效onTagDetectedError(NfcError code什么的){ 的System.out.println(onTagDetectedError);} }
解决方案
您需要将opennfc库添加到您的Eclipse项目。
在项目点击右键,选择属性
中,转到 Java构建路径|库
。点击添加外部JAR
并选择您opennfc库。最后,点击订单和出口
标签,并确保您的opennfc库被选中用于出口。
I want to read a card from Samsung Nexus phone but the android nfc api does not provide enough options. i've also tried using a third party api names "open nfc" but the it gives error of not supporting the 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.
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...
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 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.
上一篇:为什么有些用户在SQLiteDatabase.dbopen越来越SQLiteException()?用户、SQLiteDatabase、SQLiteException、dbopen
下一篇:如何让所有的cookies或android.webkit.CookieManager cookie的网址有的、网址、cookies、webkit