Sunday, April 20, 2014

Example of programming Android NFC

This example send Uri between Android devices using NFC.


To using NFC on your Android app, modify AndroidManifest.xml.
  • This example target minSdkVersion="16"
  • Add permission of "android.permission.NFC"
  • Add intent-filter of "android.nfc.action.NDEF_DISCOVERED"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidnfc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.NFC"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidnfc.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"
                    android:host="android-er.blogspot.com"
                    android:pathPrefix="/" />
            </intent-filter>
            
        </activity>
    </application>

</manifest>


MainActivity.java
package com.example.androidnfc;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements 
 CreateNdefMessageCallback, OnNdefPushCompleteCallback{
 
 TextView textInfo;
 
 NfcAdapter nfcAdapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textInfo = (TextView)findViewById(R.id.info);

  nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  if(nfcAdapter==null){
   Toast.makeText(MainActivity.this, 
    "nfcAdapter==null, no NFC adapter exists", 
    Toast.LENGTH_LONG).show();
  }else{
   Toast.makeText(MainActivity.this, 
     "Set Callback(s)", 
     Toast.LENGTH_LONG).show();
   nfcAdapter.setNdefPushMessageCallback(this, this);
   nfcAdapter.setOnNdefPushCompleteCallback(this, this);
  }
 }

 @Override
 protected void onResume() {
  super.onResume();
  Intent intent = getIntent();
  String action = intent.getAction();
  if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)){
   Parcelable[] parcelables = 
    intent.getParcelableArrayExtra(
      NfcAdapter.EXTRA_NDEF_MESSAGES);
   NdefMessage inNdefMessage = (NdefMessage)parcelables[0];
   NdefRecord[] inNdefRecords = inNdefMessage.getRecords();
   NdefRecord NdefRecord_0 = inNdefRecords[0];
   String inMsg = new String(NdefRecord_0.getPayload());
   textInfo.setText(inMsg);
  }
 }

 @Override
 protected void onNewIntent(Intent intent) {
  setIntent(intent);
 }

 @Override
 public void onNdefPushComplete(NfcEvent event) {
  
  final String eventString = "onNdefPushComplete\n" + event.toString();
  runOnUiThread(new Runnable() {
   
   @Override
   public void run() {
    Toast.makeText(getApplicationContext(), 
      eventString, 
      Toast.LENGTH_LONG).show();
   }
  });

 }

 @Override
 public NdefMessage createNdefMessage(NfcEvent event) {
  NdefRecord rtdUriRecord = NdefRecord.createUri("http://android-er.blogspot.com/");

  NdefMessage ndefMessageout = new NdefMessage(rtdUriRecord);
  return ndefMessageout;
 }

}


activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidnfc.MainActivity" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


download filesDownload the files.

Related:
Communication between Android using NFC to send text

No comments: