2016-08-11 5 views
0

私は、電話nfcリーダーで非接触スマートカードにAPDUを送信するサンプルコードを書いて、それはうまくいった。今、私は、これらの関数は、非接触型スマートカードに接続し、それにapduを送信し、結果を返す必要がありますなどの確認PINなどのいくつかの操作を実行するアンドロイドライブラリを書いてみたい。問題はここにある:(アンドロイドライブラリのアンドロイドnfcアダプタを使用

これは働く私のサンプルコードです:NFCフィールドにスマートカードを検出するために、enableForegroundDispatch機能を使用

public class MainActivity extends Activity { 

    private NfcAdapter mAdapter = null; 
    static IsoDep myTag; 
    private TextView lblStatus; 
    private Button btnSelectApplet; 
    private PendingIntent mPendingIntent; 
    boolean mFirstDetected=false; 
    boolean mShowAtr=false; 

    private String[][] mTechLists; 
    private IntentFilter[] mFilters; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     lblStatus = (TextView) findViewById(R.id.lblStatus); 
     btnSelectApplet = (Button) findViewById(R.id.btnSelect); 
     btnSelectApplet.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       byte[] select = new byte[] {0x00, (byte)0xa4, 0x04, 0x00, 0x09, (byte)0xa0, 0x00, 
         0x00, 0x03, 0x08, 0x00, 0x00, 0x10, 0x00, 0x1a}; 

       byte[] res = transceives(select); 
      } 
     }); 
     resolveIntent(getIntent()); 
     mAdapter = NfcAdapter.getDefaultAdapter(this); 
     mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
     IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
     try 
     { 
      ndef.addDataType("*/*"); 
     } 
     catch (IntentFilter.MalformedMimeTypeException e) 
     { 
      throw new RuntimeException("fail", e); 
     } 
     mFilters = new IntentFilter[] { ndef, }; 
     mTechLists = new String[][] { new String[] { IsoDep.class.getName() } }; 

    } 

    private static byte[] transceives (byte[] data) 
    { 
     byte[] ra = null; 
     try 
     { 
      ra = myTag.transceive(data); 
     } 
     catch (IOException e) 
     { 
     } 
     try 
     { 

     } 
     catch (Exception e1) 
     { 
      e1.printStackTrace(); 
     } 

     return (ra); 
    } 


    private void resolveIntent(Intent intent) 
    { 
     String action = intent.getAction(); 
     if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { 
      Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
      final Tag t = (Tag) tag; 
      myTag = IsoDep.get(t); 
      mFirstDetected = true; 
      if (myTag != null) { 
       if (!myTag.isConnected()) { 
        try { 
         myTag.connect(); 
         myTag.setTimeout(5000); 
        } catch (IOException e) { 
         e.printStackTrace(); 
         return; 
        } 
       } 
       if (myTag.isConnected()) { 
        String szATR = null; 
        try { 
         mShowAtr = true; 
         szATR = " 3B " + getATRLeString(myTag.getHistoricalBytes()) + "80 01 " + getHexString(myTag.getHistoricalBytes()) + "" + getATRXorString(myTag.getHistoricalBytes()); 
        } catch (Exception e) { 
         mShowAtr = false; 
         szATR = "CARD DETECTED "; 
        } 
        lblStatus.setText(szATR); 
       } else { 
        lblStatus.setText("Not Connected!"); 
       } 
       if (mFirstDetected == true && myTag.isConnected()) { 

       } else { 
        lblStatus.setText("Not Connected!"); 
       } 
      } 
     } 
    } 

    private static String getATRLeString(byte[] data) throws Exception 
    { 
     return String.format("%02X ", data.length | 0x80); 
    } 

    private static String getATRXorString(byte[] b) throws Exception 
    { 
     int Lrc=0x00; 
     Lrc = b.length | 0x80; 
     Lrc = Lrc^0x81; 
     for (int i=0; i < b.length; i++) 
     { 
      Lrc = Lrc^(b[i] & 0xFF); 
     } 
     return String.format("%02X ", Lrc); 
    } 

    private static String getHexString(byte[] data) throws Exception 
    { 
     String szDataStr = ""; 
     for (int ii=0; ii < data.length; ii++) 
     { 
      szDataStr += String.format("%02X ", data[ii] & 0xFF); 
     } 
     return szDataStr; 
    } 

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

    @Override 
    public void onResume() 
    { 
     super.onResume(); 
     if (mAdapter != null) 
     { 
      mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); 
     } 
    } 

}

サンプルコード検出スマートカードすると、onNewIntentがあります。

すべてのコードをアンドロイドライブラリモジュールXに入れて、アクティビティYで使用します。

Activity Y extend X 

または

X x = new X() 

や関数を呼び出します。どうすれば実装できますか? 私はこのコードを使用している場合

mAdapter = NfcAdapter.getDefaultAdapter(this); 

を呼び出したいとき、私は問題がある:それは使用しないので、

mAdapter = NfcAdapter.getDefaultAdapter(context);// context pass from Y Activity to X(Android library) 

機能

mAdapter.enableForegroundDispatch(this , mPendingIntent, mFilters, mTechLists); // In X (Android library) 

が正しく動作しませんがY活性(X axtivityを用いた)。 どうすればすべてのコードがX(アンドロイドライブラリ)になるような方法で問題を解決できますか?

ありがとうございました。私の理解あたりとして

答えて

0

、あなたは他の活動からのメソッドの一部にアクセスしようとしている、ここで

  1. 私は
  2. 、あなたがそのNFCモジュール方式のための第三のクラスを作成することをお勧め返すシングルトンメソッドを作成しますそのモデルクラスの静的インスタンスです。
  3. アクティビティからそのインスタンスにアクセスできます。
  4. クラス内のコールバックと、別のアクティビティクラスからの別のメソッド呼び出しを設定することもできます。

希望すると、これが役立ちます。何かが明確でないか尋ねる。

ハッピーコーディング:)

+0

返信ありがとうございます。 「enableForegroundDispatch(アクティビティアクティビティ、PendingIntentインテント、IntentFilter []フィルタ、String [] [] techLists)」をアクティビティではないシングルトンクラスで使用できますか? –

関連する問題