Bonjour,

Je me lance dans la lecture NFC.
J'ai pas mal avancé et mon but est de lire une carte NFC dont je ne connais pas le contenu. Si elle contient quelques choses, je renvoie ce quelques choses. Si elle ne contient rien, je renvoie son id.

Voici donc mon code :
Manifest :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
<activity android:name="com.example.activity.NfcActivity" />

Mon activité permettant de detecter le NFC :
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
 
public abstract class NfcDetectorActivity extends BaseActivity {
 
	private static final String TAG = NfcDetectorActivity.class.getName();
 
	protected NfcAdapter nfcAdapter;
	protected IntentFilter[] writeTagFilters;
	protected PendingIntent nfcPendingIntent;
 
	protected boolean foreground = false;
	protected boolean intentProcessed = false;
 
	protected boolean detecting = true;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
 
		// Check for available NFC Adapter
		PackageManager pm = getPackageManager();
		if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
			initializeNfc();
		}
	}
 
	/**
         * 
         * Initialize Nfc fields
         * 
         */
	protected void initializeNfc() {
		nfcAdapter = NfcAdapter.getDefaultAdapter(this);
		nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
				this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
 
		IntentFilter tagDetected = new IntentFilter(
				NfcAdapter.ACTION_TAG_DISCOVERED);
		IntentFilter ndefDetected = new IntentFilter(
				NfcAdapter.ACTION_NDEF_DISCOVERED);
		IntentFilter techDetected = new IntentFilter(
				NfcAdapter.ACTION_TECH_DISCOVERED);
		try {
			techDetected.addDataType("*/*");
		} catch (MalformedMimeTypeException e) {
			e.printStackTrace();
		}
		writeTagFilters = new IntentFilter[] { ndefDetected, tagDetected,
				techDetected };
	}
 
	@Override
	protected void onResume() {
		super.onResume();
 
		if (nfcAdapter != null) {
			// enable foreground mode if nfc is on and we have started detecting
			boolean enabled = nfcAdapter.isEnabled();
			if (enabled && detecting) {
				enableForeground();
			}
		}
 
		if (!intentProcessed) {
			intentProcessed = true;
 
			processIntent();
		}
	}
 
	@Override
	protected void onPause() {
		super.onPause();
 
		if (nfcAdapter != null) {
			disableForeground();
		}
	}
 
	@Override
	public void onNewIntent(Intent intent) {
 
		Log.d(TAG, "onNewIntent");
 
		// onResume gets called after this to handle the intent
		intentProcessed = false;
 
		setIntent(intent);
	}
 
	protected void enableForeground() {
		if (!foreground) {
			Log.d(TAG, "Enable nfc forground mode");
 
			nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent,
					writeTagFilters, new String[][] { new String[] {
							MifareClassic.class.getName(),
							NfcA.class.getName(), Ndef.class.getName() } });
 
			foreground = true;
		}
	}
 
	/**
         * 
         * Start detecting NDEF messages
         * 
         */
	protected void startDetecting() {
		if (!detecting) {
			enableForeground();
 
			detecting = true;
		}
	}
 
	/**
         * 
         * Stop detecting NDEF messages
         * 
         */
	protected void stopDetecting() {
		if (detecting) {
			disableForeground();
 
			detecting = false;
		}
	}
 
	protected void disableForeground() {
		if (foreground) {
			Log.d(TAG, "Disable nfc forground mode");
 
			nfcAdapter.disableForegroundDispatch(this);
 
			foreground = false;
		}
	}
 
	/**
         * 
         * Process the current intent, looking for NFC-related actions
         * 
         */
	public void processIntent() {
		Intent intent = getIntent();
		// Check to see that the Activity started due to an Android Beam
		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
			Log.d(TAG, "Process NDEF discovered action");
 
			nfcIntentDetected(intent, NfcAdapter.ACTION_NDEF_DISCOVERED);
		} else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
			Log.d(TAG, "Process TAG discovered action");
 
			nfcIntentDetected(intent, NfcAdapter.ACTION_TAG_DISCOVERED);
		} else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
			Log.d(TAG, "Process TECH discovered action");
 
			nfcIntentDetected(intent, NfcAdapter.ACTION_TECH_DISCOVERED);
		} else {
			Log.d(TAG, "Ignore action " + intent.getAction());
		}
	}
 
	/**
         * 
         * Launch an activity for nfc (or wireless) settings, so that the user might
         * enable or disable nfc
         * 
         */
 
	@SuppressLint("InlinedApi")
	protected void startNfcSettingsActivity() {
		if (android.os.Build.VERSION.SDK_INT >= 16) {
			startActivity(new Intent(
					android.provider.Settings.ACTION_NFC_SETTINGS));
		} else {
			startActivity(new Intent(
					android.provider.Settings.ACTION_WIRELESS_SETTINGS));
		}
	}
 
	/**
         * 
         * Incoming NFC communication (in form of tag or beam) detected
         * 
         */
	protected abstract void nfcIntentDetected(Intent intent, String action);
 
}
Mon activité permettant de traduire la réponse et héritant de la classe précédente :
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
public abstract class NfcActivity extends NfcDetectorActivity {
 
	private static final String TAG = NfcReaderActivity.class.getName();
 
	@Override
	public void nfcIntentDetected(Intent intent, String action) {
		Log.d(TAG, "nfcIntentDetected: " + action);
		Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
		String id = bytesToHexString(tag.getId());
 
		String res = "";
		// Lecture mifare
		MifareClassic mifare = MifareClassic.get(tag);
		if (mifare != null) {
			try {
				mifare.connect();
				for (int i = 0; i < mifare.getBlockCount(); i++) {
					try {
						res += new String(mifare.readBlock(i));
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			} finally {
				try {
					mifare.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
 
		Log.d("MIFARE", "Mifare : " + res);
		if (res.equals("")) {
			Ndef ndef = Ndef.get(tag);
			if (ndef != null) {
				try {
					ndef.connect();
					for (int i = 0; i < ndef.getCachedNdefMessage().getRecords().length; i++) {
						res += new String(
								ndef.getNdefMessage().getRecords()[i]
										.getPayload());
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						ndef.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			Log.d("NDEF", "Ndef : " + res);
			if (res.equals(""))
			{
				readNdefTagId(id);
			}
			else
				readNdefTagId(res);
		} else
			readNdefTagId(res);
	}
 
	private String bytesToHexString(byte[] src) {
		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 < src.length; ++j) {
			in = (int) src[j] & 0xff;
			i = (in >> 4) & 0x0f;
			out += hex[i];
			i = in & 0x0f;
			out += hex[i];
		}
		return out;
	}
 
	/**
         * An NDEF Tag id
         * 
         * @param id
         *            the tag id
         */
	protected abstract void readNdefTagId(String id);
}
Voila tout.

Mes questions sont simple :
- 1) Voyez vous une erreur dans mon code ?
- 2) Pensez vous qu'il me manque des cas ?

Pour information, toutes les cartes que j'ai à ma disposition me renvoie que l'id. Peut être qu'il n'y a aucun contenu ou peut être que mon code a un problème...

Merci à tous


EDIT : Il manque les cartes Mifare Ultralight!