RoconNfcManager.java
Go to the documentation of this file.
00001 package com.ros.turtlebot.apps.rocon;
00002 
00003 import java.io.IOException;
00004 import java.lang.reflect.InvocationTargetException;
00005 import java.lang.reflect.Method;
00006 import java.nio.charset.Charset;
00007 import java.util.Locale;
00008 
00009 import android.app.Activity;
00010 import android.app.PendingIntent;
00011 import android.content.Context;
00012 import android.content.Intent;
00013 import android.content.IntentFilter;
00014 import android.content.IntentFilter.MalformedMimeTypeException;
00015 import android.nfc.FormatException;
00016 import android.nfc.NdefMessage;
00017 import android.nfc.NdefRecord;
00018 import android.nfc.NfcAdapter;
00019 import android.nfc.Tag;
00020 import android.nfc.tech.IsoDep;
00021 import android.nfc.tech.MifareClassic;
00022 import android.nfc.tech.MifareUltralight;
00023 import android.nfc.tech.Ndef;
00024 import android.nfc.tech.NdefFormatable;
00025 import android.nfc.tech.NfcA;
00026 import android.nfc.tech.NfcB;
00027 import android.nfc.tech.NfcF;
00028 import android.nfc.tech.NfcV;
00029 import android.os.Parcelable;
00030 import android.widget.Toast;
00031 
00032 import com.ros.turtlebot.apps.rocon.Util;;
00033 
00034 public class RoconNfcManager {
00035 
00036         private Context mContext = null ;
00037         private PendingIntent mPendingIntent = null ;
00038         private IntentFilter[] mFilters ;
00039         private String[][] mTechList ;
00040         private Intent mPassedIntent = null ;
00041         private NfcAdapter mNfcAdapter = null ;
00042         private String mCurrentNdefString = "";
00043         
00044         public RoconNfcManager(Context context) {
00045                 // TODO Auto-generated constructor stub
00046                 mContext = context ;
00047                 mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext);
00048                 
00049                 Intent targetIntent = new Intent(mContext, mContext.getClass());
00050         targetIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
00051         mPendingIntent = PendingIntent.getActivity(mContext, 0, targetIntent, 0);
00052 
00053         IntentFilter filter_1 = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
00054         IntentFilter filter_2 = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
00055         IntentFilter filter_3 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
00056         
00057         try {
00058                 filter_1.addDataType("*/*");
00059                 filter_2.addDataType("*/*");
00060                 filter_3.addDataType("*/*");
00061         } catch(MalformedMimeTypeException e){
00062                 throw new RuntimeException("fail", e);
00063         }
00064         
00065         mFilters = new IntentFilter[] {filter_1, filter_2, filter_3} ;
00066         mTechList = new String[][] {new String[] {NfcF.class.getName()}, 
00067                                                                 new String[] {MifareClassic.class.getName()},
00068                                                                 new String[] {NfcA.class.getName()},
00069                                                                 new String[] {NfcB.class.getName()},
00070                                                                 new String[] {NfcV.class.getName()},
00071                                                                 new String[] {Ndef.class.getName()},
00072                                                                 new String[] {NdefFormatable.class.getName()},
00073                                                                 new String[] {MifareUltralight.class.getName()},
00074                                                                 new String[] {IsoDep.class.getName()}};
00075         }
00076 
00077         public boolean checkNfcStatus()  {
00078         return mNfcAdapter.isEnabled();
00079     }
00080         
00081         public boolean changeNfcStatus(boolean enable) {
00082                 
00083                 if(mNfcAdapter == null) return false ;
00084                 
00085                 boolean success = false ;
00086                 Class<?> nfcManagerClass = null ;
00087                 Method setNfcEnabled = null, setNfcDisabled = null ;
00088                 
00089                 if(enable) {
00090                         try {
00091                                 nfcManagerClass = Class.forName(mNfcAdapter.getClass().getName());
00092                                 setNfcEnabled = nfcManagerClass.getDeclaredMethod("enable");
00093                                 setNfcEnabled.setAccessible(true);
00094                                 success = (Boolean) setNfcEnabled.invoke(mNfcAdapter);
00095                         } catch (ClassNotFoundException e) {
00096                                 // TODO Auto-generated catch block
00097                                 e.printStackTrace();
00098                         } catch (NoSuchMethodException e) {
00099                                 // TODO Auto-generated catch block
00100                                 e.printStackTrace();
00101                         } catch (IllegalArgumentException e) {
00102                                 // TODO Auto-generated catch block
00103                                 e.printStackTrace();
00104                         } catch (IllegalAccessException e) {
00105                                 // TODO Auto-generated catch block
00106                                 e.printStackTrace();
00107                         } catch (InvocationTargetException e) {
00108                                 // TODO Auto-generated catch block
00109                                 e.printStackTrace();
00110                                 System.out.println(e.toString());
00111                         }
00112                         
00113                 } else {
00114                         try {
00115                                 nfcManagerClass = Class.forName(mNfcAdapter.getClass().getName());
00116                                 setNfcDisabled = nfcManagerClass.getDeclaredMethod("disable");
00117                                 setNfcDisabled.setAccessible(true);
00118                                 success = (Boolean) setNfcDisabled.invoke(mNfcAdapter);
00119                         } catch (ClassNotFoundException e) {
00120                                 // TODO Auto-generated catch block
00121                                 e.printStackTrace();
00122                         } catch (NoSuchMethodException e) {
00123                                 // TODO Auto-generated catch block
00124                                 e.printStackTrace();
00125                         } catch (IllegalArgumentException e) {
00126                                 // TODO Auto-generated catch block
00127                                 e.printStackTrace();
00128                         } catch (IllegalAccessException e) {
00129                                 // TODO Auto-generated catch block
00130                                 e.printStackTrace();
00131                         } catch (InvocationTargetException e) {
00132                                 // TODO Auto-generated catch block
00133                                 e.printStackTrace();
00134                         }
00135                 }
00136                 
00137                 return success ;
00138         }
00139         
00140         public boolean enableForegroundDispatch()  {
00141                 if (mNfcAdapter != null) {
00142                         mNfcAdapter.enableForegroundDispatch((Activity) mContext, mPendingIntent, mFilters, mTechList);
00143                         return true ;
00144         } else {
00145                 return false ;
00146         }
00147         }
00148         
00149         public boolean disableForegroundDispatch()  {
00150                 
00151                 if (mNfcAdapter != null) {
00152                         mNfcAdapter.disableForegroundDispatch((Activity) mContext);
00153                         return true ;
00154         } else {
00155                 return false ;
00156         }
00157         }
00158         
00159         public boolean onNewIntent(Intent intent) {
00160                 
00161                 mPassedIntent = intent ;
00162                 String action = mPassedIntent.getAction();
00163                 
00164                 Toast.makeText(mContext, action, Toast.LENGTH_SHORT).show();
00165                 
00166         if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
00167                         NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
00168                         NfcAdapter.ACTION_NDEF_DISCOVERED.equalsIgnoreCase(action))
00169                 return true ;
00170         else
00171                 return false ;
00172         }
00173         
00174         public String processTag()  {
00175                 
00176                 if(mPassedIntent == null) return "NFC Tag is not discovered." ;
00177                 
00178                 Parcelable[] rawMsgs = mPassedIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
00179         
00180         if(rawMsgs == null){
00181                 return "NDEF Message is null";
00182         }
00183         
00184         mCurrentNdefString = "" ;
00185         
00186         NdefMessage[] msgs = new NdefMessage[rawMsgs.length];
00187                 
00188         for(int i = 0 ; i < rawMsgs.length ; i++) {
00189                         msgs[i] = (NdefMessage)rawMsgs[i];
00190                         mCurrentNdefString += ndefMessageToString(msgs[i]);
00191                 }
00192         
00193         return mCurrentNdefString ;
00194         }
00195         
00196         public String ndefMessageToString(NdefMessage message)  {
00197                 
00198                 String ndefString = "";
00199                 NdefRecord[] ndef_records = message.getRecords();
00200         
00201                 ndefString += "**Num of NdefRecord : " + ndef_records.length + "\n" ;
00202         
00203         for(int i = 0 ; i < ndef_records.length ; i++){
00204                 String temp = "**Record No. " + i +"\n" ;
00205                 byte[] type = ndef_records[i].getType();
00206                 byte[] id = ndef_records[i].getId();
00207                 byte[] pl = ndef_records[i].getPayload();
00208                 byte[] arr = ndef_records[i].toByteArray() ;
00209                 
00210                 temp = temp + "- TNF=" + ndef_records[i].getTnf() + 
00211                                 "\n - TYPE=" + Util.getHexString(type, type.length) + " " + new String(type) +
00212                                 "\n - ID=" + Util.getHexString(id, id.length) + " " + new String(id) +
00213                                 "\n - PayLoad=" + Util.getHexString(pl, pl.length) + " " + new String(pl) +
00214                                 "\n - ByteArray=" + Util.getHexString(arr, arr.length) + " " + new String(arr) + "\n";
00215                 
00216                 ndefString += temp ;
00217         }
00218         
00219         return ndefString ;
00220         }
00221         
00222         public String getPayload() {
00223                 
00224         String payload = "";
00225                 
00226         if(mPassedIntent == null) return null ;
00227                 
00228                 Parcelable[] rawMsgs = mPassedIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
00229         
00230         if(rawMsgs == null){
00231                 return payload;
00232         }
00233                 
00234         NdefMessage[] msgs = new NdefMessage[rawMsgs.length];
00235                 
00236         for(int i = 0 ; i < rawMsgs.length ; i++) {
00237                         msgs[i] = (NdefMessage)rawMsgs[i];
00238                 }
00239         
00240                 NdefRecord[] records = msgs[0].getRecords();
00241                 if(records.length > 0)
00242                         payload = new String(records[0].getPayload()) ;
00243                 
00244                 return payload ;
00245         }
00246         
00247         private NdefRecord createTextRecord(String text, Locale locale, boolean encodeInUtf8) {
00248                 
00249         final byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
00250         final Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
00251         final byte[] textBytes = text.getBytes(utfEncoding);
00252         final int utfBit = encodeInUtf8 ? 0 : (1 << 7);
00253         final char status = (char)(utfBit + langBytes.length);
00254         final byte[] data = Util.concat(new byte[] {(byte)status}, langBytes, textBytes);
00255         
00256         return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
00257     }
00258         
00259         public boolean writeTextNdefMessage(String payload, boolean isAAR) {
00260                 NdefRecord record = createTextRecord(payload, Locale.KOREAN, true);
00261                 NdefMessage msg = null ;
00262                 if(isAAR) 
00263                         msg = new NdefMessage(new NdefRecord[] {record, NdefRecord.createApplicationRecord(mContext.getPackageName())});
00264                 else
00265                         msg = new NdefMessage(new NdefRecord[] {record});
00266                 
00267                 return writeNdefMessage(msg);
00268         }
00269         
00270         public boolean wrtieUriNdefMessage(String payload, boolean isAAR) {
00271                 
00272                 NdefRecord record = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, NdefRecord.RTD_URI, new byte[0], payload.getBytes());
00273                 NdefMessage msg = null ;
00274                 if(isAAR) 
00275                         msg = new NdefMessage(new NdefRecord[] {record, NdefRecord.createApplicationRecord(mContext.getPackageName())});
00276                 else
00277                         msg = new NdefMessage(new NdefRecord[] {record});
00278                 
00279                 return writeNdefMessage(msg);
00280         }
00281         
00282         public boolean writeMimeNdefMessage(String payload, boolean isAAR) {
00283                 
00284                 NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, ("application/" + mContext.getPackageName()).getBytes(), new byte[0], payload.getBytes());
00285                 NdefMessage msg = null ;
00286                 if(isAAR) 
00287                         msg = new NdefMessage(new NdefRecord[] {record, NdefRecord.createApplicationRecord(mContext.getPackageName())});
00288                 else
00289                         msg = new NdefMessage(new NdefRecord[] {record});
00290                 
00291                 return writeNdefMessage(msg);
00292         }
00293         
00294         public boolean writeNdefMessage(NdefMessage message) {
00295                 
00296                 if(mPassedIntent == null) return false ;
00297                 
00298                 Tag tag = mPassedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
00299                 Ndef ndefTag = Ndef.get(tag);
00300                 
00301                 try {
00302                         ndefTag.connect();
00303                 } catch (IOException e) {
00304                         // TODO Auto-generated catch block
00305                         e.printStackTrace();
00306                         return false ;
00307                 }
00308                 
00309                 try {
00310                         ndefTag.writeNdefMessage(message);
00311                 } catch (IOException e) {
00312                         // TODO Auto-generated catch block
00313                         e.printStackTrace();
00314                         return false ;
00315                 } catch (FormatException e) {
00316                         // TODO Auto-generated catch block
00317                         e.printStackTrace();
00318                         return false ;
00319                 }
00320                 
00321                 try {
00322                         ndefTag.close();
00323                 } catch (IOException e) {
00324                         // TODO Auto-generated catch block
00325                         e.printStackTrace();
00326                         return false ;
00327                 }
00328                                 
00329                 return true ;
00330         }
00331 }


android_remocons
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Wed Aug 26 2015 10:40:28