Bonjour à tous,

Après le Bluetooth voilà que je m'attaque au NFC.

J'ai lu le tutoriels à disposition mais à nouveau je rencontre un souci.

Mon objectif est de réaliser une application, basique dans un premier temps, qui me permettra de lire l'id des TAG mifare 1k dont je dispose.

Mon application se lance bien. Mais une fois que je présente un TAG, mon application repasse par onPause et onResume.

voici mon manifest :

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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.furlan.isib.easytag"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="10" />
 
    <uses-permission android:name="android.permission.NFC" />
 
    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true" />
 
    <application
        android:icon="@drawable/easytag"
        android:label="@string/app_name" >
        <activity
            android:name=".EasyTag"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
 
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
et voici mon code :

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
65
66
67
 
public class EasyTag extends Activity {
    private final boolean DEBUG = true;
    private final String TAG = "EasyTag Debug";
    NfcAdapter mNfcAdapter;
    TextView textID;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        if(DEBUG)Log.i(TAG, "onCreate");
        textID = (TextView)findViewById(R.id.textViewID);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mNfcAdapter = NfcAdapter.getDefaultAdapter();
        if(DEBUG)Log.i(TAG, "NfcAdapter.getDefaultAdapter()");
        if(mNfcAdapter == null){
            if(DEBUG)Log.i(TAG, "no NFC Adapter");
            Toast.makeText(this, "no NFC Adapter", Toast.LENGTH_LONG)
            .show();
            finish();
        }
 
 
 
    }
    @Override
    protected void onStart() {
        if(DEBUG)Log.i(TAG, "onStart");
        // TODO Auto-generated method stub
        super.onStart();
    }
    @Override
    protected void onPause() { 
        if(DEBUG)Log.i(TAG, "onPause");
        // TODO Auto-generated method stub
        super.onPause();
    }
    @Override
    protected void onResume() { 
        if(DEBUG)Log.i(TAG, "onResume");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
        detection(this.getIntent());
        super.onResume();
    }
    @Override
    protected void onStop() {
        if(DEBUG)Log.i(TAG, "onStop");
        // TODO Auto-generated method stub
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        if(DEBUG)Log.i(TAG, "onDestroy");
        // TODO Auto-generated method stub
        super.onDestroy();
    }
 
    public void detection(Intent intent){
        String action = intent.getAction();
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
            if(DEBUG)Log.i(TAG, "TAG_DISCOVERED");
            Tag tagID = intent.getParcelableExtra(NfcAdapter.EXTRA_ID);
            textID.setText("ID: "+tagID.getId());
        }
 
    }
J'ai également quelques questions au sujet du NFC sous Android mais j'aurai surement mes réponses une fois que j'aurai résolu ce premier problème.

Merci d'avance pour votre aide.