Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize NFC swiping Card Reading by Android Development

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to realize NFC swiping card reading in Android development. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

①: Manifest registration method: this method is mainly in the Manifest file corresponding to the activity, configure the filter to respond to different types of NFC Action. In this way, when swiping a card, if multiple applications in the phone have the NFC implementation scheme, the system will pop up a list of applications that can respond to NFC events for users to choose from, and users need to click on the target application to respond to the NFC swiping event. At present, this logic is used in the project of our company, which is relatively simple. Here we first post the implementation logic of this way.

Manifest configuration:

...

Nfc_tech.xml: this file is what TECH_DISCOVERED needs to configure, where tech-list is logic or relationship, and tech is logic and relationship, which is similar to the principle and purpose of techLists in scheme ②.

Android.nfc.tech.Ndef android.nfc.tech.NfcA android.nfc.tech.NfcB android.nfc.tech.NfcF

NfcActivity:

Public class NfcActivity extends Activity {@ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_nfc); initData ();} / * * initialization data * / private void initData () {NfcAdapter adapter = NfcAdapter.getDefaultAdapter (this) If (null = = adapter) {Toast.makeText (this, "NFC functionality is not supported", Toast.LENGTH_SHORT) .show ();} else if (! adapter.isEnabled ()) {Intent intent = new Intent (Settings.ACTION_NFC_SETTINGS); / / Open the corresponding configuration interface startActivity (intent) according to the package name } / / in my project, I took the id data from the tag of the NFC card, depending on the specific situation. / / you can check the data in the NfcAdapter source code. What data can you get Tag tag = getIntent () .getParcelableExtra (NfcAdapter.EXTRA_TAG); String id = bytesToHex (tag.getId ()) / / TODO currently in my project, after getting the data, distribute it to the corresponding activity through EventBus. Of course, you can also use other distribution response methods. / / close the animation. After all, for users, swiping a card should be an imperceptible process: overridePendingTransition (0,0); finish ();} @ Override protected void onNewIntent (Intent intent) {super.onNewIntent (intent). InitData ();} / * 2 transfer 10 * @ param src * @ return * / private static String bytesToTenNum (byte [] src) {StringBuilder stringBuilder = new StringBuilder (); if (src = = null | | src.length > > 4) & 0x0F, 16) Buffer [0] = Character.toUpperCase (Character.forDigit (src [I] & 0x0F, 16); stringBuilder.append (buffer);} stringBuilder.reverse (); BigInteger bigi = new BigInteger (stringBuilder.toString (), 16); return bigi.toString () } / * 2 to 16 * @ param src * @ return * / private static String bytesToHex (byte [] src) {StringBuffer sb = new StringBuffer (); if (src = = null | | src.length

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report