仅适用于特定 NFC 标签的应用程序
Application that works only for a specific NFC tag
我当前的 Android 应用程序可以识别任何 NFC 标签并执行所需的功能。但是,我希望它只适用于具有特定 UID 的特定标签。实现此目标的最佳方法是什么?
相关标签的 UID 是:046a0b42402b84
我的NFC功能代码如下:
public class NFC extends AppCompatActivity {
NfcAdapter nfcAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String username = getIntent().getStringExtra("Username");
TextView tv = (TextView) findViewById(R.id.TVusername);
tv.setText(username);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(nfcAdapter!=null && nfcAdapter.isEnabled()) {
}else{
finish();
}
}
@Override
protected void onNewIntent(Intent intent) {
Toast.makeText(this, "NFC intent received!", Toast.LENGTH_LONG).show();
super.onNewIntent(intent);
}
@Override
protected void onResume() {
Intent intent = new Intent(this, AttendanceRegistration.class);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
IntentFilter[] intentFilter = new IntentFilter[]{};
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilter, null);
super.onResume();
}
@Override
protected void onPause() {
nfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Android 不允许通过 UID/anti-collision identifier/serial 编号为特定标签注册 NFC 标签检测通知。如果您希望您的应用仅由特定的 NFC 标签启动(通过在 ANdroidManifest.xml 中注册一个 intent 过滤器),您需要存储一个 NDEF 消息(包含一个自定义的 NDEF 记录,您可以在 or 和Android 申请记录)在标签上
- 见How to read NFC Tag through my Application if I wrote NFC Tag through my Application
如果您不希望您的应用程序通过标签启动并且只想在您的 activity 处于前台时检测标签(您的代码已经尝试通过注册前台调度来做到这一点系统),您可以收到所有已发现标签的通知,并根据标签的 UID 简单地过滤标签。
首先,您需要正确注册前台调度系统以接收任何标签的通知(请注意,您不能为另一个 activity!注册带有 PendingIntent 的前台调度):
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
然后您可以在 onNewIntent
中接收 NFC 发现意图并根据标签 UID 过滤它们:
@Override
protected void onNewIntent(Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] uid = tag.getId();
byte[] referenceUid = new byte[]{ (byte)0x04, (byte)0x6a, (byte)0x0b, (byte)0x42, (byte)0x40, (byte)0x2b, (byte)0x84 };
if (Arrays.equals(referenceUid, uid)) {
// do something
}
}
}
我当前的 Android 应用程序可以识别任何 NFC 标签并执行所需的功能。但是,我希望它只适用于具有特定 UID 的特定标签。实现此目标的最佳方法是什么?
相关标签的 UID 是:046a0b42402b84
我的NFC功能代码如下:
public class NFC extends AppCompatActivity {
NfcAdapter nfcAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String username = getIntent().getStringExtra("Username");
TextView tv = (TextView) findViewById(R.id.TVusername);
tv.setText(username);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(nfcAdapter!=null && nfcAdapter.isEnabled()) {
}else{
finish();
}
}
@Override
protected void onNewIntent(Intent intent) {
Toast.makeText(this, "NFC intent received!", Toast.LENGTH_LONG).show();
super.onNewIntent(intent);
}
@Override
protected void onResume() {
Intent intent = new Intent(this, AttendanceRegistration.class);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
IntentFilter[] intentFilter = new IntentFilter[]{};
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilter, null);
super.onResume();
}
@Override
protected void onPause() {
nfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Android 不允许通过 UID/anti-collision identifier/serial 编号为特定标签注册 NFC 标签检测通知。如果您希望您的应用仅由特定的 NFC 标签启动(通过在 ANdroidManifest.xml 中注册一个 intent 过滤器),您需要存储一个 NDEF 消息(包含一个自定义的 NDEF 记录,您可以在 or 和Android 申请记录)在标签上
- 见How to read NFC Tag through my Application if I wrote NFC Tag through my Application
如果您不希望您的应用程序通过标签启动并且只想在您的 activity 处于前台时检测标签(您的代码已经尝试通过注册前台调度来做到这一点系统),您可以收到所有已发现标签的通知,并根据标签的 UID 简单地过滤标签。
首先,您需要正确注册前台调度系统以接收任何标签的通知(请注意,您不能为另一个 activity!注册带有 PendingIntent 的前台调度):
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
然后您可以在 onNewIntent
中接收 NFC 发现意图并根据标签 UID 过滤它们:
@Override
protected void onNewIntent(Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] uid = tag.getId();
byte[] referenceUid = new byte[]{ (byte)0x04, (byte)0x6a, (byte)0x0b, (byte)0x42, (byte)0x40, (byte)0x2b, (byte)0x84 };
if (Arrays.equals(referenceUid, uid)) {
// do something
}
}
}