NFC 카드 번호 읽기 안드로이드

6601 단어
1. 권한
    
        

2. 등록(정적)
            
                
                
            

3.Activity
  • 초기화
  •         //   NfcAdapter
            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
                    //    PendingIntent,  NFC        ,     Activity  
            pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
                    .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    
  • 시작
  •     @Override
        protected void onResume() {
            super.onResume();
            mNfcAdapter.enableForegroundDispatch(this, pi, null, null); //  
        }
    
  • 데이터 확보
  •     @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            //   app        ,     intent    ,        onNewIntent    , intent    
            //             intent   NFC   intent,   ,       
            if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
                processIntent(intent);
            }
        }
    
  • 해석
  •     /**
         * Parses the NDEF Message from the intent and prints to the TextView
         */
        private void processIntent(Intent intent) {
            //     intent  TAG
            Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            String CardId =ByteArrayToHexString(tagFromIntent.getId());
        }
    private String ByteArrayToHexString(byte[] inarray) {
            int i, j, in;
            String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
                    "B", "C", "D", "E", "F" };
            String out = "";
    
    
            for (j = 0; j < inarray.length; ++j) {
                in = (int) inarray[j] & 0xff;
                i = (in >> 4) & 0x0f;
                out += hex[i];
                i = in & 0x0f;
                out += hex[i];
            }
            return out;
        }
    

    4. 전체 참조
    
    
    
        
        
    
        
    
        
            
                
                    
    
                    
                
            
            
                
                    
                    
                
                
            
        
    
    
    
    package cn.com.jslh.zjcdprogrect.saoka;
    
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.nfc.NfcAdapter;
    import android.nfc.Tag;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    
    import cn.com.jslh.zjcdprogrect.R;
    
    public class WorkActivity extends AppCompatActivity {
    
        private NfcAdapter mNfcAdapter;
        private PendingIntent pi;
        private IntentFilter tagDetected;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_work);
    
            //   NfcAdapter
            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
            //   PendingIntent
            //    PendingIntent,  NFC        ,     Activity  
            pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
                    .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            //   IntentFilter,            
    //        tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    //        tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            //   app        ,     intent    ,        onNewIntent    , intent    
            //             intent   NFC   intent,   ,       
            if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
                processIntent(intent);
            }
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mNfcAdapter.enableForegroundDispatch(this, pi, null, null);
        }
    
        /**
         * Parses the NDEF Message from the intent and prints to the TextView
         */
        private void processIntent(Intent intent) {
            //     intent  TAG
            Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            String CardId =ByteArrayToHexString(tagFromIntent.getId());
        }
    
        public static void startActivity(Context context){
            Intent intent = new Intent();
            intent.setClass(context,WorkActivity.class);
            context.startActivity(intent);
        }
    
        private String ByteArrayToHexString(byte[] inarray) {
            int i, j, in;
            String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
                    "B", "C", "D", "E", "F" };
            String out = "";
    
    
            for (j = 0; j < inarray.length; ++j) {
                in = (int) inarray[j] & 0xff;
                i = (in >> 4) & 0x0f;
                out += hex[i];
                i = in & 0x0f;
                out += hex[i];
            }
            return out;
        }
    }
    
    

    좋은 웹페이지 즐겨찾기