Recevoir et envoyer des messages NFC via une application Android ne peut être fait que par un Intent. Mon application doit lire et écrire des données sur des tags. Je dois implémenter une interface qui déclare la méthode IsCardPresent (pour détecter la carte) et APDUExchange (pour envoyer et recevoir des données). Comment gérer le routage des messages de mon Intent à APDUExchange et vice versa?


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
public interface IMother  {
 
    abstract boolean isCardPresent() throws ReaderException;
 
    abstract APDUResp APDUExchange(APDUReq) throws ReaderException;
 
}
Voici la classe avec l'implementation de isCardPresent et ApduExchange
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
public class DroidReader extends Activity implements IMother {  
    private NfcAdapter mNfcAdapter;
     private boolean isCardPresent = false;
 
    @Override
    public boolean isCardPresent()throws ReaderException {
         return isCardPresent;
    }
 
    @Override
    public APDUResp APDUExchange(APDUReq req) throws ReaderException {
        //how to put and get APDU msg to the intent??
        //return null;
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        //initialize your service here
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
 
        if (mNfcAdapter == null) {
            // Stop here, we definitely need NFC
            Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
            //   finish();
            return;
        }
 
        if (!mNfcAdapter.isEnabled()) {
            //    mTextView.setText("NFC is disabled.");
        } else {
            //   mTextView.setText(R.string.explanation);
        }
 
        resolveIntent(getIntent());
 
    }
 
    private void resolveIntent(Intent intent) {
 
        String action = intent.getAction();
 
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
        // status_Data.setText("Discovered tag with intent: " + intent);
 
        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        IsoDep myTag = IsoDep.get(tagFromIntent);
 
        try {
            myTag.connect();
 
            if (myTag.isConnected()) {
                isCardPresent = true;
                APDUResp respToSendToAPDUExchange = myTag.transceive(reqFromAPDUExchange);
                **// How to root APDU for my APDUExchange method?** 
            }
        } catch (IOException e) {
            //      Log.e(TAG, e.getLocalizedMessage());
              //  showAlert(NETWORK);
        }
    }
}