How to Read RFID Card in Android


In today's world, RFID (Radio-Frequency Identification) technology is widely used for various applications like access control, inventory management, and contactless payments. In this blog post, we will explore how to read RFID cards in Android devices using NFC (Near Field Communication) technology.

Prerequisites

Before we begin, ensure that your Android device supports NFC and your project has the necessary permissions to access NFC functionality. You will also need an RFID reader compatible with Android devices, which supports NFC communication.

Step 1: Set Up Project Dependencies

Open your Android project in Android Studio and navigate to the Gradle file. Add the following dependency to your app-level build.gradle file:

    
dependencies {
  implementation 'com.android.support:appcompat-v7:28.0.0'
  implementation 'com.android.support:support-v4:28.0.0'
  implementation 'com.github.skjolber:nfc-reader-library:2.1.1'
}
    
  

Step 2: Initialize NFC Adapter

In your Android activity or fragment, initialize the NFC adapter and check if it's available:

    
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
  // NFC is not supported on this device
  return;
}
    
  

Step 3: Handle NFC Intent

Override the onNewIntent method in your activity or fragment to handle NFC intents:

    
@Override
protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
  if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
    // NFC tag discovered, handle the data
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    // Read the RFID card data from the tag and process it
    // ...
  }
}
    
  

Step 4: Read RFID Card Data

To read the RFID card data, we need to convert the tag data to a readable format. Add the following method to your activity or fragment:

    
private String readRFIDCardData(Tag tag) {
  Ndef ndef = Ndef.get(tag);
  if (ndef == null) {
    // Tag is not formatted as NDEF
    // You can implement different logic based on the tag type
    return null;
  }
  
  NdefMessage ndefMessage = ndef.getCachedNdefMessage();
  if (ndefMessage == null) {
    // NDEF message not found
    return null;
  }
  
  // Read the records from the NDEF message
  NdefRecord[] records = ndefMessage.getRecords();
  StringBuilder sb = new StringBuilder();
  
  for (NdefRecord record : records) {
    byte[] payload = record.getPayload();
    
    // Skip the first byte (usually the language code)
    for (int i = 1; i < payload.length; i++) {
      sb.append((char) payload[i]);
    }
  }
  
  return sb.toString();
}
    
  

Step 5: Request NFC Foreground Dispatch

To receive NFC intents when your app is in the foreground, you need to request foreground dispatch. Add the following code to your activity or fragment:

    
@Override
protected void onResume() {
  super.onResume();
  IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[] { intentFilter }, null);
}

@Override
protected void onPause() {
  super.onPause();
  nfcAdapter.disableForegroundDispatch(this);
}
    
  

Step 6: Process RFID Card Data

Finally, you can process the read RFID card data as per your application requirements. You can display it to the user, store it in a database, or perform any other desired actions.

Conclusion

Reading RFID cards in Android using NFC technology is not complex with the right tools and knowledge. By following the steps outlined in this blog post, you should now have a good understanding of how to read RFID cards in your Android application. Remember to handle errors gracefully and consider the security aspects of handling sensitive RFID card data.

Handheld Computers

Portable and handheld for multi-domain applications.

Basic Functions, Better Performance

Android 12

Dual SIM card slots

2G/3G/4G/5G

2.4G/5G WiFi 802.11ax Wi-Fi6

Bluetooth 5.1 +BLE

4GB+64GB 6GB+128GB 8GB+256GB (Optional)

MediaTek Dimensity 900

LCD 6-inch IPS

QR / bar code scanner(optional)

Corning GG3 reinforced glass cover

NFC, Magnetic(optional)

Finger Print

B2B Wholesale Kingtop Enterprise PDA

·Powerful, Durable and Enterprise-Ready
·Seamless Device and Data Management
·Rugged and Powerful Handheld Computer for Business
·Cost-Saving Bulk Deals for Large Volume Purchases

Why Choose Us?

We’re here to help tailor our comprehensive business solutions to your specific needs.

5G Fast Connectivity

Our tablet devices are equipped with advanced 5G modules that support various network bands and protocols, which allows you to enjoy fast and stable internet access anytime and anywhere.

Rich Production Experience

We have been focusing on the production of intelligent mobile devices for 15 years, and we have a deep understanding of the industry trends and customer needs. We can provide you with high-quality products that meet your expectations and requirements.

Trouble Shooting

We have a professional and responsive customer service team that can solve any problems you encounter within 24 hours. You can also contact our engineers directly for technical support and guidance.

OEM

We can customize your tablet devices according to your specifications and preferences. You can choose the size, color, logo, software, hardware and accessories of your tablet devices. We will offer you the best solution that suits your budget and needs.

Prouduct Selection

We have a wide range of tablet devices for you to choose from, with different features, functions and prices. Our professional sales team will recommend the most suitable and cost-effective products for you based on your needs and preferences.

ODM

We have a professional R&D and design team that can develop innovative and unique tablet devices for you. We have 15 years of experience in software and hardware development, and we can create solutions that satisfy your customers and the market.Don’t miss this opportunity to get the best 5G tablet device for your business or personal use. Contact us today and get a free quote and sample!

Which PDA is right for you?

Powerful device management tools.

Talk to us >        SAMPLE TEST >

KT-KP18

4G LTE / 5G

2.2 Gigabit Carrier Speed

5.7-inch

HD+,720*1440

5mp

5mp front, 13mp rear

4000mAh

Removable 4000mAh battery

Android™12.0

Android™12.0 System

Full specs→

KT-KP18(Medical)

5G

2.2 Gigabit Carrier Speed

5.7-inch

HD+,720*1440

5mp

5mp front, 13mp rear

4000mAh

Removable 4000mAh battery

Android™12.0

Android™12.0 System

Full specs→

KT-KP26

4G LTE / 5G

2.2 Gigabit Carrier Speed

6-inch

HD+,720*1520

8mp

8mp front, 48mp rear

5000mAh

Removable 5000mAh battery

Android™11.0

Android™11.0 System

Full specs→

KT-KP36

4G LTE

1Gigabit Carrier Speed

4-inch

HD+,800*480

5mp

5mp front, 13mp rear

5000mAh

Removable 5000mAh battery

Android™11.0

Android™11.0 System

Full specs→