Lou*_*ier 3 android nfc android-intent mifare
我有很多使用MIFARE的经验(自1996年以来,使用GEMPLUS制造的卡片时).我甚至编写了低级代码来模拟MIFARE卡......但是现在事情变得更简单,更高级别我无法使用Java,Android和Android Studio!我想我的时间会变得笨拙......
我所要做的就是在检测到MIFARE卡时启动应用程序.我知道可以这样做,因为我在我的设备中使用了NFC卡信息应用程序,它在MIFARE卡的存在下正确启动.我已经卸载它以确保唯一的NFC应用程序是我自己的.我试图遵循http://developer.android.com/guide/topics/connectivity/nfc/nfc.html和http://developer.android.com/guide/topics/connectivity上不太令人满意的文档./nfc/advanced-nfc.html.
问题是我的应用程序从未启动过.代码非常简单,AFAIK应该正常工作......这是app清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nfctest.app" >
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.nfctest.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
它按NDEF_DISCOVERED过滤,因为TECH_DISCOVERED不能单独工作.
这是nfc_tech_filter资源文件:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是活动代码:
package com.example.nfctest.app;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView label;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label = (TextView)findViewById(R.id.label);
}
@Override
public void onResume() {
super.onResume();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
label.setText("NDEF_DISCOVERED");
} else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
label.setText("TECH_DISCOVERED");
}
}
@Override
public void onNewIntent(Intent intent) {
label.setText("onNewIntent!!!");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
活动布局非常简单:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.nfctest.app.MainActivity">
<TextView
android:id="@+id/label"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我需要开始编程实际的NFC代码,只要检测到TAG,就可以启动应用程序.文档让它听起来如此简单,以至于我确信它再次成为我的愚蠢...
通过将活动代码更改为:我能够使用foregroundDispatcher正确读取标记UID:
package com.example.nfctest.app;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class MainActivity extends ActionBarActivity {
TextView label;
IntentFilter[] filters;
String[][] techs;
PendingIntent pendingIntent;
NfcAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label = (TextView)findViewById(R.id.label);
pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter mifare = new IntentFilter((NfcAdapter.ACTION_TECH_DISCOVERED));
filters = new IntentFilter[] { mifare };
techs = new String[][] { new String[] { NfcA.class.getName() } };
adapter = NfcAdapter.getDefaultAdapter(this);
}
public void onPause() {
super.onPause();
adapter.disableForegroundDispatch(this);
}
public void onResume() {
super.onResume();
adapter.enableForegroundDispatch(this, pendingIntent, filters, techs);
}
public void onNewIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] id = tag.getId();
ByteBuffer wrapped = ByteBuffer.wrap(id);
wrapped.order(ByteOrder.LITTLE_ENDIAN);
int signedInt = wrapped.getInt();
long number = signedInt & 0xffffffffl;
label.setText("Tag detected: " + number);
}
}
Run Code Online (Sandbox Code Playgroud)
我甚至不需要在清单中设置任何意图过滤器来实现此功能,只需像这样的简单清单即可:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nfctest.app" >
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.nfctest.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
使用此代码,当我的应用程序位于前台时,我可以读取MIFARE卡序列号,但我仍然无法进行设置,以便ANDROID在后台启动我的活动...
当检测到标签(MIFARE或NOT)时,我仍然不知道如何让ANDROID启动我的app/activity.
您的问题是技术过滤器XML文件(因为您最终正确地发现了自己).您最初使用的技术过滤器,
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
Run Code Online (Sandbox Code Playgroud)
没有任何意义.<tech>一个<tech-filter>条目中的条目使用逻辑AND组合.
所以,你需要有一个标记,是Ndef 和 NdefFormatable 和 MifareClassic 和 MifareUltralight.这有两个原因是不可能的:
Ndef并且NdefFormatable是相互排斥的.标签可以要么已经包含NDEF数据/空NDEF消息( - > Ndef)或它可以是准备与NDEF消息被格式化( - > NdefFormatable).MifareClassic并且MifareUltralight是相互排斥的.标签可以是 MIFARE Classic标签或 MIFARE Ultralight标签,但不能同时使用.因此,例如,一个适当的技术过滤器触发包含NDEF消息的标签,并且是MIFARE Classic或MIFARE Ultralight,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
Run Code Online (Sandbox Code Playgroud)
所有<tech-list>条目与逻辑OR组合,然后:(Ndef 和 MifareClassic)或 Ndef 和 MifareUltralight).
在你的情况下,你似乎对NDEF不太感兴趣,但更多的是从MIFARE卡中获取任何东西.假设MIFARE你的意思是MIFARE Classic卡(因为你在谈论1996 ;-)),你的技术过滤器应该如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
</resources>
Run Code Online (Sandbox Code Playgroud)
但请注意,这仅适用于采用恩智浦芯片组的设备(MIFARE Classic是专有的NXP技术.虽然恩智浦将卡侧授权给其他制造商,但他们并未授权读卡器端.因此,只有1个恩智浦的读卡器产品才能读取MIFARE Classic. ).在具有Broadcom芯片组的设备上,MIFARE Classic卡2未被检测为MIFARE Classic.但是,由于MIFARE经典使用标准ISO 14443-3类型A防冲突和激活序列,这些设备通常3检测这样的卡作为NfcA:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
</resources>
Run Code Online (Sandbox Code Playgroud)
请注意,使用带MifareClassic标记技术的第二个条目将是多余的,因为检测到MIFARE Classic的每个设备都将检测到它,MifareClassic 并且 NfcA.
1)该规则有例外,因为有些制造商不同意恩智浦的立场(或者完全忽略它?),在读者端实施MIFARE Classic支持侵犯了他们的权利.
2)并且只有MIFARE Classic.这不适用于符合标准的MIFARE产品,如Ultralight,DESFire,NTAG等.
3)一些采用Broadcom NFC芯片组的三星设备,如S4,是该规则的例外.在这些设备上,三星决定完全禁用MIFARE Classic,而是显示"不支持标签"错误.正如他们声称的那样,改善用户体验,因为用户不会理解为什么他们不能将数据写入这些标签.或者正如我所解释的那样,让您的用户讨厌您作为应用程序开发人员,因为您无法使用他们的标签在您的手机上使用您的应用程序.查看ReTag应用程序或我自己的应用程序上的负面评论.
| 归档时间: |
|
| 查看次数: |
5942 次 |
| 最近记录: |