2014-09-01 56 views
0

我正在编写需要读取NFC标签的应用。而不是阅读标签通过意图过滤器打开一个新的应用程序的行为,我希望我目前打开的应用程序只需读取附近的标签。Android:如何使用当前应用读取NFC标签

我试图使用enableForegroundDispatch()但没有运气。无论何时出现NFC标签,我的设备都会打开标签“标签”应用程序。

我到目前为止的代码是:

final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); 
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); 
IntentFilter[] filters = new IntentFilter[1]; 
String[][] techList = new String[][]{}; 
filters[0] = new IntentFilter(); 
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); 
filters[0].addCategory(Intent.CATEGORY_DEFAULT); 
try { 
    filters[0].addDataType(MIME_TEXT_PLAIN); 
} catch (MalformedMimeTypeException e) { 
    throw new RuntimeException("Check your mime type."); 
} 
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); 

(这基本上是从一个教程,我发现复制)。

有谁知道这是否是正确的方法去做这件事? 如何防止标准“标签”应用打开,而不是我自己的应用收到NDEF_DISCOVERED意图。

感谢

+0

什么数据你有你的标签?你想触发一个特定的标签类型(见'android.nfc.tech。*'命名空间)或任何标签? – 2014-09-01 17:46:40

+0

我需要的唯一东西(我认为标签上唯一的东西)是ID – chris 2014-09-02 08:05:18

+0

这并不能真正回答我的问题。 – 2014-09-02 08:40:35

回答

0

你的代码目前登记前台调度系统只有在包含了NDEF文本记录(或“text/plain的” MIME类型记录)NFC标签来触发。因此,如果您的标签不包含这样的记录,那么NFC发现事件会传递给其他应用(或者由您的标准标签应用处理)。

因此,您可能要注册的前景调度系统适合您标签的意图:

IntentFilter[] filters = new IntentFilter[1]; 
filters[0] = new IntentFilter(); 
filters[0].addAction(NfcAdapter.ACTION_TECH_DISCOVERED); 
filters[0].addCategory(Intent.CATEGORY_DEFAULT); 
String[][] techList = new String[][]{ new String[] { NfcA.class.getName() } }; 
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); 

或者你可以只登记在任何标记触发:

adapter.enableForegroundDispatch(activity, pendingIntent, null, null); 
5

第一所有您必须在AndroidMenifest.xml文件中获得nfc权限。权限是:

<uses-permission android:name="android.permission.NFC" /> 

<uses-feature android:name="android.hardware.nfc" /> 

将执行NFC读/写操作的行为,在menifest.xml文件活动添加此意图过滤:

<intent-filter> 
     <action android:name="android.nfc.action.TAG_DISCOVERED" /> 

     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 

在您的活动onCreate()方法你必须初始化NFC适配器和定义待定意图:

NfcAdapter mAdapter; 
PendingIntent mPendingIntent; 
mAdapter = NfcAdapter.getDefaultAdapter(this); 
if (mAdapter == null) { 
    //nfc not support your device. 
    return; 
} 
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
     getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

在的onResume()回拨使前景调度来检测NFC意图。

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); 

在的onPause()回调您必须禁用于地面调度:

if (mAdapter != null) { 
    mAdapter.disableForegroundDispatch(this); 
} 

在onNewIntent()回调方法,你会得到新的NFC意向。获得该意图后,您必须解析检测该卡的意图:

@Override 
protected void onNewIntent(Intent intent){  
    getTagInfo(intent) 
    } 
private void getTagInfo(Intent intent) { 
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
} 

现在您已经有标签。然后,您可以检查标签技术列表以检测该标签。标签检测技术在这里My Another Answer 完整的项目是在My GitHub Repo

+0

非常感谢!为我工作 – GrafOrlov 2015-12-01 15:03:48

+0

欢迎兄弟:) – 2015-12-01 16:40:27

相关问题