본문 바로가기
Programming/Android Java

NFC, Well-known Type URI, Absolute URI, Text 태그 데이터 가져오기 소스

by 개Foot/Dog발?! 2014. 9. 25.

 

        private NfcAdapter mAdapter;

        private PendingIntent mPendingIntent;

        private IntentFilter[] mFilters;

 

        @Override

        protected void onCreate(Bundle savedInstanceState) {

               super.onCreate(savedInstanceState);

 

//<생략>

               // NFC 관련 객체 생성

               mAdapter = NfcAdapter.getDefaultAdapter(this);

               Intent targetIntent = new Intent(this, ReadActivity.class);

               targetIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

               mPendingIntent = PendingIntent.getActivity(this, 0, targetIntent, 0);

 

               IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

               try {

                       ndef.addDataType("*/*");

               } catch (MalformedMimeTypeException e) {

                       throw new RuntimeException("fail", e);

               }

 

               mFilters = new IntentFilter[] { ndef, };

 

//<NDEF 외에 Spec 대한 Filter 관련 생략>

 

        }

 

        public void onResume() {

               super.onResume();

 

               if (mAdapter != null) {

                       mAdapter.enableForegroundDispatch(this, mPendingIntent, mFiltersNULL);

               }

        }

 

        public void onPause() {

               super.onPause();

 

               if (mAdapter != null) {

                       mAdapter.disableForegroundDispatch(this);

               }

        }

 

        // NFC 태그 스캔시 호출되는 메소드

        public void onNewIntent(Intent passedIntent) {

               // NFC 태그

               if (passedIntent != null) {

                        Tag tag = passedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

                        if (tag != null) {

                               byte[] tagId = tag.getId();

                               Log.d(TAG,”TAG ID : “ + toHexString(tagId));

                        }

              

                       processTag(passedIntent); // processTag 메소드 호출

               }

        }

 

        private void processTag(Intent passedIntent) {

               Parcelable[] rawMsgs = passedIntent

                              .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

               if (rawMsgs == null) {

                       return;

               }

 

               NdefMessage[] msgs;

               if (rawMsgs != null) {

                       msgs = new NdefMessage[rawMsgs.length];

                       for (int i = 0; i < rawMsgs.length; i++) {

                              msgs[i] = (NdefMessage) rawMsgs[i];

                              show(msgs[i]); // showTag 메소드 호출

                       }

               }

        }

 

        public void show(NdefMessage message) {

                NdefRecord[] records = message.getRecords();

 

                for (NdefRecord record : records) {

                        if (Uri uri = getUri(record)) {

                                Log.d(TAG,”URI : “ + uri.getUri().toString();

                        } else if (String text = getText(record)) {

                                Log.d(TAG,”TEXT : “ + text);

                        }

                }

        }

 

        public Uri getUri(NdefRecord record) {

               short tnf = record.getTnf();

               Uri uri = null;

 

               if (tnf == NdefRecord.TNF_WELL_KNOWN) { //URL prefix

                       if(Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {

                              byte[] payload = record.getPayload();

                              String prefix = URI_PREFIX_MAP.get(payload[0]);

                              byte[] fullUri = Bytes.concat(prefix.getBytes(Charset.forName("UTF-8")),

                                             Arrays.copyOfRange(payload, 1, payload.length));

                              uri = Uri.parse(new String(fullUri, Charset.forName("UTF-8")));

                       }

               } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) { //full URL

                       byte[] payload = record.getPayload();

                       uri = Uri.parse(new String(payload, Charset.forName("UTF-8")));

               }

 

               Return uri;

        }

        public String getText(NdefRecord record) {

                String text = null;

 

               if(record.getTnf() == NdefRecord.TNF_WELL_KNOWN) {

                   if(Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) {

                               byte[] payload = record.getPayload();

 

// REF : Text Record Type Definition Technical Specification NFC ForumTM RTD-Text 1.0 NFCForum-TS-RTD_Text_1.0

                               String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";

                              int languageCodeLength = payload[0] & 0077;

                              String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");

                               text = new String(payload, languageCodeLength + 1,

                                             payload.length - languageCodeLength - 1, textEncoding);

                       }

               }

 

               return text;

        }

 

'Programming > Android Java' 카테고리의 다른 글

Android NFC  (0) 2014.09.26
안드로이드 NFC 읽고/쓰기 구현  (0) 2014.09.26
wi-fi 절전모드를 끄는 소스코드@androidpub  (0) 2014.09.21
앱 완전종료[테스트 필수]  (0) 2014.09.18
Bluetooth@android.com  (0) 2014.09.14