Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.01 KB | None | 0 0
  1. package com.test.testlayout;
  2.  
  3. import java.nio.charset.Charset;
  4. import java.util.Arrays;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.IntentFilter;
  9. import android.nfc.NdefMessage;
  10. import android.nfc.NdefRecord;
  11. import android.nfc.NfcAdapter;
  12. import android.nfc.Tag;
  13. import android.nfc.tech.NfcF;
  14. import android.os.Bundle;
  15. import android.os.Parcelable;
  16. import android.util.Log;
  17. import com.google.common.collect.BiMap;
  18. import com.google.common.collect.ImmutableBiMap;
  19. import com.google.common.primitives.Bytes;
  20.  
  21. import android.content.Intent;
  22. import com.unity3d.player.UnityPlayerActivity;
  23.  
  24. public class MainActivity extends UnityPlayerActivity {
  25.  
  26.     public static final String MIME_TEXT_PLAIN = "text/plain";
  27.  
  28.     private static final BiMap URI_PREFIX_MAP = ImmutableBiMap.builder()
  29.             .put((byte) 0x00, "")
  30.             .put((byte) 0x01, "http://www.")
  31.             .put((byte) 0x02, "https://www.")
  32.             .put((byte) 0x03, "http://")
  33.             .put((byte) 0x04, "https://")
  34.             .put((byte) 0x05, "tel:")
  35.             .put((byte) 0x06, "mailto:")
  36.             .put((byte) 0x07, "ftp://anonymous:anonymous@")
  37.             .put((byte) 0x08, "ftp://ftp.")
  38.             .put((byte) 0x09, "ftps://")
  39.             .put((byte) 0x0A, "sftp://")
  40.             .put((byte) 0x0B, "smb://")
  41.             .put((byte) 0x0C, "nfs://")
  42.             .put((byte) 0x0D, "ftp://")
  43.             .put((byte) 0x0E, "dav://")
  44.             .put((byte) 0x0F, "news:")
  45.             .put((byte) 0x10, "telnet://")
  46.             .put((byte) 0x11, "imap:")
  47.             .put((byte) 0x12, "rtsp://")
  48.             .put((byte) 0x13, "urn:")
  49.             .put((byte) 0x14, "pop:")
  50.             .put((byte) 0x15, "sip:")
  51.             .put((byte) 0x16, "sips:")
  52.             .put((byte) 0x17, "tftp:")
  53.             .put((byte) 0x18, "btspp://")
  54.             .put((byte) 0x19, "btl2cap://")
  55.             .put((byte) 0x1A, "btgoep://")
  56.             .put((byte) 0x1B, "tcpobex://")
  57.             .put((byte) 0x1C, "irdaobex://")
  58.             .put((byte) 0x1D, "file://")
  59.             .put((byte) 0x1E, "urn:epc:id:")
  60.             .put((byte) 0x1F, "urn:epc:tag:")
  61.             .put((byte) 0x20, "urn:epc:pat:")
  62.             .put((byte) 0x21, "urn:epc:raw:")
  63.             .put((byte) 0x22, "urn:epc:")
  64.             .put((byte) 0x23, "urn:nfc:").build();
  65.  
  66.     private NfcAdapter mNfcAdapter;
  67.  
  68.     private PendingIntent pendingIntent;
  69.  
  70.     IntentFilter[] mIntentFilter;
  71.  
  72.     String[][] techListsArray;
  73.  
  74.     private static String value = "";
  75.     public static MainActivity instance;
  76.     public static Context ctx;
  77.  
  78.  
  79.     public static MainActivity instance() {
  80.         return new MainActivity();
  81.     }
  82.  
  83.     @Override
  84.     protected void onCreate(Bundle savedInstanceState) {
  85.         super.onCreate(savedInstanceState);
  86.         MainActivity.instance = this;
  87.         ctx = this;
  88.         // Foreground Dispatch: 1. Creates a PendingIntent object so the Android system can populate it with the details of the tag when it is scanned.
  89.         pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
  90.                 new Intent(MainActivity.this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  91.         // Foreground Dispatch: 2. Declare intent filters to handle the intents that you want to intercept
  92.         mIntentFilter = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)};
  93.         // Foreground Dispatch: 3. Set up an array of tag technologies that your application wants to handle.
  94.         techListsArray = new String[][]{new String[]{NfcF.class.getName()}};
  95.  
  96.         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  97.         if (mNfcAdapter == null) {
  98.             Log.e(MainActivity.class.toString(), "This device doesn't support NFC.");
  99.             finish();
  100.             return;
  101.         }
  102.         if (!mNfcAdapter.isEnabled()) {
  103.             Log.e(MainActivity.class.toString(), "NFC is disabled.");
  104.         } else {
  105.             Log.i(MainActivity.class.toString(), "NFC reader initialized.");
  106.         }
  107.     }
  108.  
  109.     @Override
  110.     public void onResume() {
  111.         super.onResume();
  112.         // Enables the foreground dispatch when the activity regains focus.
  113.         mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilter, techListsArray);
  114.     }
  115.  
  116.     @Override
  117.     public void onPause() {
  118.         super.onPause();
  119.         // Disables the foreground dispatch when the activity loses focus.
  120.         mNfcAdapter.disableForegroundDispatch(this);
  121.     }
  122.  
  123.     @Override
  124.     protected void onNewIntent(Intent intent) {
  125.         super.onNewIntent(intent);
  126.         handleIntent(intent);
  127.     }
  128.  
  129.     private void handleIntent(Intent intent) {
  130.         Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  131.         if (tag != null) {
  132.             // Parses through all NDEF messages and their records and picks text and uri type.
  133.             Parcelable[] data = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
  134.             String s = "";
  135.             if (data != null) {
  136.                 try {
  137.                     for (int i = 0; i < data.length; i++) {
  138.                         NdefRecord[] recs = ((NdefMessage) data[i]).getRecords();
  139.                         for (int j = 0; j < recs.length; j++) {
  140.                             if (recs[j].getTnf() == NdefRecord.TNF_WELL_KNOWN
  141.                                     && Arrays.equals(recs[j].getType(), NdefRecord.RTD_TEXT)) {
  142.                                 /*
  143.                                 * See NFC forum specification for
  144.                                 * "Text Record Type Definition" at 3.2.1
  145.                                 *
  146.                                 * http://www.nfc-forum.org/specs/
  147.                                 *
  148.                                 * bit_7 defines encoding bit_6 reserved for
  149.                                 * future use, must be 0 bit_5..0 length of IANA
  150.                                 * language code
  151.                                 */
  152.                                 byte[] payload = recs[j].getPayload();
  153.                                 String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
  154.                                 int langCodeLen = payload[0] & 0077;
  155.                                 s += new String(payload, langCodeLen + 1, payload.length - langCodeLen - 1,
  156.                                         textEncoding);
  157.                             } else if (recs[j].getTnf() == NdefRecord.TNF_WELL_KNOWN
  158.                                     && Arrays.equals(recs[j].getType(), NdefRecord.RTD_URI)) {
  159.                                 /*
  160.                                 * See NFC forum specification for
  161.                                 * "URI Record Type Definition" at 3.2.2
  162.                                 *
  163.                                 * http://www.nfc-forum.org/specs/
  164.                                 *
  165.                                 * payload[0] contains the URI Identifier Code
  166.                                 * payload[1]...payload[payload.length - 1]
  167.                                 * contains the rest of the URI.
  168.                                 */
  169.                                 byte[] payload = recs[j].getPayload();
  170.                                 String prefix = (String) URI_PREFIX_MAP.get(payload[0]);
  171.                                 byte[] fullUri = Bytes.concat(prefix.getBytes(Charset.forName("UTF-8")),
  172.                                         Arrays.copyOfRange(payload, 1, payload.length));
  173.                                 s += new String(fullUri, Charset.forName("UTF-8"));
  174.                             }
  175.                         }
  176.                     }
  177.                 } catch (Exception e) {
  178.                     value = e.getMessage();
  179.                     Log.e(MainActivity.class.toString(), e.getMessage());
  180.                 }
  181.             }
  182.             Log.i(MainActivity.class.toString(), s);
  183.             value = s;
  184.         }
  185.     }
  186.  
  187.     public static String getValue() {
  188.         return value;
  189.     }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement