Commit 6feece8514cb1ee8c37f40041533166bcd2b6853
1 parent
e3e40761
ajout NFC
Showing
3 changed files
with
161 additions
and
5 deletions
Show diff stats
PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java
1 | package com.example.app_10p5; | 1 | package com.example.app_10p5; |
2 | 2 | ||
3 | import android.app.Activity; | 3 | import android.app.Activity; |
4 | +import android.app.PendingIntent; | ||
5 | +import android.content.Intent; | ||
6 | +import android.content.IntentFilter; | ||
7 | +import android.nfc.NfcAdapter; | ||
8 | +import android.nfc.Tag; | ||
9 | +import android.nfc.tech.MifareClassic; | ||
10 | +import android.nfc.tech.NfcA; | ||
4 | import android.os.Bundle; | 11 | import android.os.Bundle; |
12 | +import android.widget.Toast; | ||
13 | + | ||
14 | +import java.io.IOException; | ||
5 | 15 | ||
6 | /** | 16 | /** |
7 | * Created by beaus on 24/04/2016. | 17 | * Created by beaus on 24/04/2016. |
8 | */ | 18 | */ |
9 | public class CarteActivite extends Activity { | 19 | public class CarteActivite extends Activity { |
20 | + | ||
21 | + private NfcAdapter mNfcAdapter; | ||
22 | + private PendingIntent mPendingIntent; | ||
23 | + private IntentFilter[] mFilters; | ||
24 | + private String[][] mTechLists; | ||
25 | + private Toast mToast; | ||
26 | + | ||
27 | + | ||
10 | @Override | 28 | @Override |
11 | protected void onCreate(Bundle savedInstanceState) { | 29 | protected void onCreate(Bundle savedInstanceState) { |
12 | super.onCreate(savedInstanceState); | 30 | super.onCreate(savedInstanceState); |
13 | setContentView(R.layout.layout_carte); | 31 | setContentView(R.layout.layout_carte); |
32 | + | ||
33 | + ////Code NFC -- Nécessaire | ||
34 | + mNfcAdapter = NfcAdapter.getDefaultAdapter(this); | ||
35 | + | ||
36 | + mPendingIntent = PendingIntent.getActivity(this, 0, | ||
37 | + new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); | ||
38 | + | ||
39 | + IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); | ||
40 | + | ||
41 | + try { | ||
42 | + ndef.addDataType("text/plain"); | ||
43 | + } catch (IntentFilter.MalformedMimeTypeException e) { | ||
44 | + throw new RuntimeException("fail", e); | ||
45 | + } | ||
46 | + mFilters = new IntentFilter[] { | ||
47 | + ndef, | ||
48 | + }; | ||
49 | + | ||
50 | + // Setup a tech list for all NfcF tags | ||
51 | + mTechLists = new String[][]{new String[]{NfcA.class.getName(), MifareClassic.class.getName()}}; | ||
52 | + } | ||
53 | + | ||
54 | + public void onResume() { | ||
55 | + super.onResume(); | ||
56 | + if(mNfcAdapter != null) | ||
57 | + mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); | ||
58 | + } | ||
59 | + | ||
60 | + // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63"). | ||
61 | + private String ByteArrayToHexString(byte [] inarray) { | ||
62 | + int i, j, in; | ||
63 | + String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; | ||
64 | + String out= ""; | ||
65 | + for(j = 0 ; j < inarray.length ; ++j) | ||
66 | + { | ||
67 | + in = (int) inarray[j] & 0xff; | ||
68 | + i = (in >> 4) & 0x0f; | ||
69 | + out += hex[i]; | ||
70 | + i = in & 0x0f; | ||
71 | + out += hex[i]; | ||
72 | + } | ||
73 | + return out; | ||
74 | + } | ||
75 | + | ||
76 | + @Override | ||
77 | + public void onNewIntent(Intent intent) { | ||
78 | + mToast = Toast.makeText(getApplicationContext(), "ID Carte : ", Toast.LENGTH_SHORT); | ||
79 | + mToast.show(); | ||
80 | + if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) { | ||
81 | + String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); | ||
82 | + mToast = Toast.makeText(getApplicationContext(), "ID Carte : " + id_carte, Toast.LENGTH_SHORT); | ||
83 | + mToast.show(); | ||
84 | + System.out.println("ID Carte : " + id_carte); | ||
85 | + | ||
86 | + //Lecture des données | ||
87 | + Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); | ||
88 | + MifareClassic mfc = MifareClassic.get(tag); | ||
89 | + byte[] data; | ||
90 | + String prenom = null, nom = null, login = null; | ||
91 | + | ||
92 | + if (mfc != null) { | ||
93 | + try { | ||
94 | + mfc.connect(); | ||
95 | + String cardData = null; | ||
96 | + | ||
97 | + //Clé A | ||
98 | + byte[] cleA = new byte[]{(byte) 0xa0, (byte) 0xa1, (byte) 0xa2, | ||
99 | + (byte) 0xa3, (byte) 0xa4, (byte) 0xa5}; | ||
100 | + //On veut juste lire le secteur 12 | ||
101 | + boolean estConnecte = mfc.authenticateSectorWithKeyA(12, cleA); | ||
102 | + if (estConnecte) { | ||
103 | + //Il y a 4 blocs dans le secteur 12 -> INE, numéro étudiant, prénom, nom | ||
104 | + //On ne veut que prénom et nom, donc on passe les deux premiers | ||
105 | + for (int i = 2, bIndex = mfc.sectorToBlock(12) + i; i < 4; i++, bIndex++) { | ||
106 | + //sectorToBlock : Renvoie l'indice (global, parmis tous les blocs de | ||
107 | + // tous les secteurs) du premier bloc du secteur 12 | ||
108 | + //Lit les données du bloc | ||
109 | + data = mfc.readBlock(bIndex); | ||
110 | + | ||
111 | + //Convertit les bytes en String | ||
112 | + String dataStr = new String(data); | ||
113 | + if (i == 2) //Prénom | ||
114 | + prenom = dataStr; | ||
115 | + else if (i == 3) //Nom | ||
116 | + nom = dataStr; | ||
117 | + mToast = Toast.makeText(getApplicationContext(), "Données lues : " + dataStr, Toast.LENGTH_SHORT); | ||
118 | + mToast.show(); | ||
119 | + System.out.println("Données lues : " + dataStr); | ||
120 | + } | ||
121 | + } else { | ||
122 | + mToast = Toast.makeText(getApplicationContext(), "Erreur lors de la connection au secteur 12.", Toast.LENGTH_SHORT); | ||
123 | + mToast.show(); | ||
124 | + System.out.println("Erreur lors de la connection au secteur 12."); | ||
125 | + } | ||
126 | + mfc.close(); | ||
127 | + } catch (IOException e) { | ||
128 | + System.out.println(e.getLocalizedMessage()); | ||
129 | + } | ||
130 | + | ||
131 | + //Concaténation des données récupérées en login | ||
132 | + login = prenom; | ||
133 | + login.concat("."); | ||
134 | + login.concat(nom); | ||
135 | + System.out.println("Prenom : " + prenom); | ||
136 | + System.out.println("Nom : " + nom); | ||
137 | + } else { | ||
138 | + System.out.println("Pas de connection possible à la technologie Mifare Classic."); | ||
139 | + mToast = Toast.makeText(getApplicationContext(), "Pas de connection possible à la technologie Mifare Classic.", Toast.LENGTH_SHORT); | ||
140 | + mToast.show(); | ||
141 | + } | ||
142 | + | ||
143 | + mToast = Toast.makeText(getApplicationContext(), "Login Lille 1 : " + login, Toast.LENGTH_SHORT); | ||
144 | + mToast.show(); | ||
145 | + System.out.println("Login Lille 1 : " + login); | ||
146 | + | ||
147 | + | ||
148 | + //Éxécution de la fonction | ||
149 | + taFonction(id_carte, login); | ||
150 | + } | ||
14 | } | 151 | } |
15 | 152 | ||
153 | + public void taFonction(String id_carte, String login) | ||
154 | + { | ||
155 | + //code fonction | ||
156 | + } | ||
157 | + | ||
158 | + @Override | ||
159 | + public void onPause() { | ||
160 | + super.onPause(); | ||
161 | + if(mNfcAdapter != null) | ||
162 | + mNfcAdapter.disableForegroundDispatch(this); | ||
163 | + } | ||
16 | } | 164 | } |
PremiereActivite/app/src/main/res/values/strings.xml
@@ -3,11 +3,6 @@ | @@ -3,11 +3,6 @@ | ||
3 | <string name="action_settings">Settings</string> | 3 | <string name="action_settings">Settings</string> |
4 | <string name="identifiant" /> | 4 | <string name="identifiant" /> |
5 | <string name="password" /> | 5 | <string name="password" /> |
6 | - <string name="menu_search">Test1</string> | ||
7 | - <string name="menu_refresh">Test2</string> | ||
8 | - <string name="menu_about">Test4</string> | ||
9 | - <string name="menu_settings">Test5</string> | ||
10 | - <string name="menu_help">Test3</string> | ||
11 | <string name="validate">Valider</string> | 6 | <string name="validate">Valider</string> |
12 | <string name="quant">Quantité</string> | 7 | <string name="quant">Quantité</string> |
13 | <string name="montant">Montant</string> | 8 | <string name="montant">Montant</string> |
@@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | ||
2 | +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> | ||
3 | + <tech-list> | ||
4 | + <!-- Pour la carte Lille1 sur des tablettes non-compatibles Mifare Classic, | ||
5 | + ie. les NEXUS 7 version 2013 --> | ||
6 | + <tech>android.nfc.tech.NfcA</tech> | ||
7 | + </tech-list> | ||
8 | + <tech-list> | ||
9 | + <!-- Pour la carte Lille1 sur des tablettes compatibles Mifare Classic --> | ||
10 | + <tech>android.nfc.tech.NfcA</tech> | ||
11 | + <tech>android.nfc.tech.MifareClassic</tech> | ||
12 | + </tech-list> | ||
13 | +</resources> | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |