仅适用于特定NFC标签的应用程序(Application that works only for a specific NFC tag)

编程入门 行业动态 更新时间:2024-10-26 09:20:18
适用于特定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); } }

My current Android application recognises any NFC tag and performs the required function. However, I want it to only work with a specific tag with a specific UID. What is the best way to accomplish this?

The UID for the tag in question is: 046a0b42402b84

My code for the NFC function is below:

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 /防冲突标识符/序列号为特定标签注册NFC标签检测通知。 如果您希望仅通过特定NFC标签启动应用程序(通过在ANdroidManifest.xml中注册intent过滤器),则需要存储NDEF消息(包含可以过滤的自定义NDEF记录或Android应用程序记录)

如果我通过我的应用程序编写NFC标签,请参阅如何通过我的应用程序读取NFC标签

如果您不希望应用程序由标记启动,并且只想在活动处于前台时检测标记(您的代码已经尝试通过注册前台调度系统来执行此操作),则可以接收所有通知发现标签并根据其UID简单地过滤标签。

首先,您需要正确注册前台调度系统以接收任何标记的通知(请注意,您无法使用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 onNewIntent进行过滤:

@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 does not allow NFC tag detection notifications to be registered for specific tags by their UID/anti-collision identifier/serial number. If you want your app to be started only by a specific NFC tag (through registration of an intent filter in ANdroidManifest.xml), you would need to store an NDEF message (containing a custom NDEF record that you can filter on or and Android Application Record) on the tag

See How to read NFC Tag through my Application if I wrote NFC Tag through my Application

If you do not want your application to get launched by the tag and only want to detect the tag while your activity is in the foreground (your code already tries to do this by registering for the foreground dispatch system), you can receive notifications for all discovered tags and simply filter the tags based on their UID.

First, you need to properly register for the foreground dispatch system to receive notifications for any tag (note that you cannot register the foreground dispatch with a PendingIntent for another activity!):

@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); }

You can then receive the NFC discovery intents in onNewIntent and filter them based on the tag 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 } } }

更多推荐

@Override,NFC,super,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-07-05 01:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   应用程序   标签   NFC   tag

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!