|
1 | 1 | # Android-RFID-NFC-Card-Reader-Integration
|
2 | 2 | This article will guide you how to integrate RFID NFC Card Reader in your android application
|
| 3 | + |
| 4 | +## Getting Started |
| 5 | +RFID is the process by which items are uniquely identified using radio waves, and NFC is a specialized subset within the family of RFID technology. Specifically, NFC is a branch of High-Frequency (HF) RFID, and both operate at the 13.56 MHz frequency. NFC is designed to be a secure form of data exchange, and an NFC device is capable of being both an NFC reader and an NFC tag. This unique feature allows NFC devices to communicate peer-to-peer. |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | +Before starting the integration you need to ready these three things: |
| 9 | + |
| 10 | +1. Android Basics |
| 11 | +2. NFC Enabled Device |
| 12 | +3. NFC Tag |
| 13 | + |
| 14 | +To enable NFC on your android device, go to Settings -> More -> and enable it |
| 15 | + |
| 16 | +### Integration |
| 17 | +You need to implement changes in the following parts of your Android app for basic integration of RFID Card Reader |
| 18 | + |
| 19 | +1. Manifest |
| 20 | +2. Resource folder |
| 21 | +3. Main Activity class |
| 22 | + |
| 23 | +#### 1. Manifest |
| 24 | +Add these lines in manifest: |
| 25 | + |
| 26 | +- NFC Permission |
| 27 | + ``` |
| 28 | + <uses-permission android:name="android.permission.NFC" /> |
| 29 | + ``` |
| 30 | +- NFC Feature |
| 31 | + ``` |
| 32 | + <uses-feature android:name="android.hardware.nfc" android:required="true" /> |
| 33 | + ``` |
| 34 | +- Intent Filter to catch action NDEF_DISCOVERED |
| 35 | + ``` |
| 36 | + <intent-filter> |
| 37 | + <action android:name="android.nfc.action.NDEF_DISCOVERED" /> |
| 38 | + <category android:name="android.intent.category.DEFAULT" /> |
| 39 | + <data android:mimeType="text/plain" /> |
| 40 | + </intent-filter> |
| 41 | +
|
| 42 | + ``` |
| 43 | +- Meta Data |
| 44 | + ``` |
| 45 | + <meta-data |
| 46 | + android:name="android.nfc.action.TECH_DISCOVERED" |
| 47 | + android:resource="@xml/nfc_tech_filter" /> |
| 48 | +
|
| 49 | + ``` |
| 50 | + |
| 51 | +#### 2. Resource folder |
| 52 | +Create file named **nfc_tech_filter.xml** in *-XML*- folder with the following contents: |
| 53 | + |
| 54 | +``` |
| 55 | +<?xml version="1.0" encoding="utf-8"?> |
| 56 | +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> |
| 57 | + <tech-list> |
| 58 | + <tech>android.nfc.tech.Ndef</tech> |
| 59 | + <!-- class name --> |
| 60 | + </tech-list> |
| 61 | + </resources> |
| 62 | +
|
| 63 | +``` |
| 64 | +#### 3. Main Activity class |
| 65 | +Add these two changes & Enjoy running code with RFID NFC Card Reader Integrated |
| 66 | +- In OnCreate() method |
| 67 | + ``` |
| 68 | + nfcAdapter = NfcAdapter.getDefaultAdapter(this); |
| 69 | + if (nfcAdapter == null) { |
| 70 | + // Stop here, we definitely need NFC |
| 71 | + Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show(); |
| 72 | + finish(); |
| 73 | + } |
| 74 | + readFromIntent(getIntent()); |
| 75 | +
|
| 76 | + pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); |
| 77 | + IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); |
| 78 | + tagDetected.addCategory(Intent.CATEGORY_DEFAULT); |
| 79 | +
|
| 80 | + ``` |
| 81 | +- readFromIntent() method to Read from NFC Tag |
| 82 | + |
| 83 | + ``` |
| 84 | + private void readFromIntent(Intent intent) { |
| 85 | + String action = intent.getAction(); |
| 86 | + if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) |
| 87 | + || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) |
| 88 | + || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { |
| 89 | + Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); |
| 90 | + NdefMessage[] msgs = null; |
| 91 | + if (rawMsgs != null) { |
| 92 | + msgs = new NdefMessage[rawMsgs.length]; |
| 93 | + for (int i = 0; i < rawMsgs.length; i++) { |
| 94 | + msgs[i] = (NdefMessage) rawMsgs[i]; |
| 95 | + } |
| 96 | + } |
| 97 | + buildTagViews(msgs); |
| 98 | + } |
| 99 | + } |
| 100 | +
|
| 101 | + ``` |
| 102 | + |
0 commit comments