Commit 93f90e35e33f34b47d169c1cef7d7315b50a8efd
1 parent
b3d818d0
NFC et foreground
Le NFC fonctionne désormais uniquement sur l'écran où on en a besoin. Grâce au ForegroundDispatch.
Showing
9 changed files
with
88 additions
and
92 deletions
Show diff stats
PremiereActivite/app/src/main/AndroidManifest.xml
... | ... | @@ -2,8 +2,9 @@ |
2 | 2 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
3 | 3 | package="com.example.app_10p5"> |
4 | 4 | |
5 | + <!-- Nécessaire pour utiliser les cartes NFC --> | |
5 | 6 | <uses-permission android:name="android.permission.NFC" /> |
6 | - <uses-sdk android:minSdkVersion="22"/> | |
7 | + <uses-sdk android:minSdkVersion="20"/> | |
7 | 8 | <uses-feature android:name="android.hardware.nfc" android:required="true" /> |
8 | 9 | |
9 | 10 | <application |
... | ... | @@ -13,32 +14,20 @@ |
13 | 14 | android:supportsRtl="true" |
14 | 15 | android:theme="@style/AppTheme"> |
15 | 16 | <activity |
16 | - android:name="com.example.app_10p5.MainActivite" | |
17 | - android:label="10p5"> | |
17 | + android:name=".CarteActivite" | |
18 | + android:launchMode="singleTask" | |
19 | + android:label="CarteActivite" | |
20 | + android:parentActivityName="com.example.app_10p5.MainActivite" > | |
21 | + </activity> | |
22 | + | |
23 | + <activity | |
24 | + android:name=".MainActivite" | |
25 | + android:label="MainActivite"> | |
18 | 26 | <intent-filter> |
19 | 27 | <action android:name="android.intent.action.MAIN" /> |
20 | 28 | <category android:name="android.intent.category.LAUNCHER" /> |
21 | 29 | </intent-filter> |
22 | 30 | </activity> |
23 | - <activity | |
24 | - android:name="com.example.app_10p5.CarteActivite" | |
25 | - android:label="CarteActivite" | |
26 | - android:parentActivityName="com.example.app_10p5.MainActivite" > | |
27 | - <meta-data | |
28 | - android:name="android.support.PARENT_ACTIVITY" | |
29 | - android:value="com.example.app_10p5.MainActivite" | |
30 | - android:resource="@xml/tech" /> | |
31 | - <intent-filter> <!-- Prioritaire mais je n'ai pas réussi à le faire fonctionner --> | |
32 | - <action android:name="android.nfc.action.NDEF_DISCOVERED"/> | |
33 | - <category android:name="android.intent.category.DEFAULT"/> | |
34 | - <data android:mimeType="text/plain" /> | |
35 | - </intent-filter> | |
36 | - <intent-filter> <!-- Regardé juste après les NDEF --> | |
37 | - <action android:name="android.nfc.action.TECH_DISCOVERED"/> | |
38 | - <category android:name="android.intent.category.DEFAULT"/> | |
39 | - </intent-filter> | |
40 | - </activity> | |
41 | - | |
42 | 31 | </application> |
43 | 32 | |
44 | -</manifest> | |
45 | 33 | \ No newline at end of file |
34 | +</manifest> | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java
... | ... | @@ -3,11 +3,9 @@ package com.example.app_10p5; |
3 | 3 | import android.app.Activity; |
4 | 4 | import android.app.PendingIntent; |
5 | 5 | import android.content.Intent; |
6 | -import android.content.IntentFilter; | |
7 | 6 | import android.nfc.NfcAdapter; |
8 | 7 | import android.nfc.Tag; |
9 | 8 | import android.nfc.tech.MifareClassic; |
10 | -import android.nfc.tech.NfcA; | |
11 | 9 | import android.os.Bundle; |
12 | 10 | import android.widget.Toast; |
13 | 11 | |
... | ... | @@ -19,42 +17,28 @@ import java.io.IOException; |
19 | 17 | public class CarteActivite extends Activity { |
20 | 18 | |
21 | 19 | private NfcAdapter mNfcAdapter; |
22 | - private PendingIntent mPendingIntent; | |
23 | - private IntentFilter[] mFilters; | |
24 | - private String[][] mTechLists; | |
25 | - private Toast mToast; | |
26 | - | |
27 | 20 | |
28 | 21 | @Override |
29 | 22 | protected void onCreate(Bundle savedInstanceState) { |
30 | 23 | super.onCreate(savedInstanceState); |
31 | 24 | setContentView(R.layout.layout_carte); |
32 | 25 | |
33 | - ////Code NFC -- Nécessaire | |
34 | 26 | mNfcAdapter = NfcAdapter.getDefaultAdapter(this); |
35 | 27 | |
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); | |
28 | + if (mNfcAdapter == null) { | |
29 | + // Stop here, we definitely need NFC | |
30 | + Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show(); | |
31 | + finish(); | |
32 | + return; | |
45 | 33 | } |
46 | - mFilters = new IntentFilter[] { | |
47 | - ndef, | |
48 | - }; | |
49 | 34 | |
50 | - // Setup a tech list for all NfcF tags | |
51 | - mTechLists = new String[][]{new String[]{NfcA.class.getName(), MifareClassic.class.getName()}}; | |
35 | + handleIntent(getIntent()); | |
52 | 36 | } |
53 | 37 | |
54 | - public void onResume() { | |
38 | + @Override | |
39 | + protected void onResume() { | |
55 | 40 | super.onResume(); |
56 | - if(mNfcAdapter != null) | |
57 | - mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); | |
41 | + setupForegroundDispatch(this, mNfcAdapter); | |
58 | 42 | } |
59 | 43 | |
60 | 44 | // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63"). |
... | ... | @@ -74,14 +58,27 @@ public class CarteActivite extends Activity { |
74 | 58 | } |
75 | 59 | |
76 | 60 | @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())) { | |
61 | + protected void onNewIntent(Intent intent) { | |
62 | + handleIntent(intent); | |
63 | + } | |
64 | + | |
65 | + public void taFonction(String id_carte, String login) | |
66 | + { | |
67 | + //code fonction | |
68 | + } | |
69 | + | |
70 | + @Override | |
71 | + protected void onPause() { | |
72 | + stopForegroundDispatch(this, mNfcAdapter); | |
73 | + super.onPause(); | |
74 | + } | |
75 | + | |
76 | + private void handleIntent(Intent intent){ | |
77 | + if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { | |
78 | + Toast toast; | |
81 | 79 | 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); | |
80 | + toast = Toast.makeText(getApplicationContext(), "ID Carte : " + id_carte, Toast.LENGTH_SHORT); | |
81 | + toast.show(); | |
85 | 82 | |
86 | 83 | //Lecture des données |
87 | 84 | Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); |
... | ... | @@ -92,13 +89,14 @@ public class CarteActivite extends Activity { |
92 | 89 | if (mfc != null) { |
93 | 90 | try { |
94 | 91 | mfc.connect(); |
95 | - String cardData = null; | |
96 | 92 | |
97 | 93 | //Clé A |
98 | 94 | byte[] cleA = new byte[]{(byte) 0xa0, (byte) 0xa1, (byte) 0xa2, |
99 | 95 | (byte) 0xa3, (byte) 0xa4, (byte) 0xa5}; |
96 | + | |
100 | 97 | //On veut juste lire le secteur 12 |
101 | 98 | boolean estConnecte = mfc.authenticateSectorWithKeyA(12, cleA); |
99 | + | |
102 | 100 | if (estConnecte) { |
103 | 101 | //Il y a 4 blocs dans le secteur 12 -> INE, numéro étudiant, prénom, nom |
104 | 102 | //On ne veut que prénom et nom, donc on passe les deux premiers |
... | ... | @@ -114,14 +112,12 @@ public class CarteActivite extends Activity { |
114 | 112 | prenom = dataStr; |
115 | 113 | else if (i == 3) //Nom |
116 | 114 | 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); | |
115 | + toast = Toast.makeText(getApplicationContext(), "Données lues : " + dataStr, Toast.LENGTH_SHORT); | |
116 | + toast.show(); | |
120 | 117 | } |
121 | 118 | } 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."); | |
119 | + toast = Toast.makeText(getApplicationContext(), "Erreur lors de la connection au secteur 12.", Toast.LENGTH_SHORT); | |
120 | + toast.show(); | |
125 | 121 | } |
126 | 122 | mfc.close(); |
127 | 123 | } catch (IOException e) { |
... | ... | @@ -132,33 +128,27 @@ public class CarteActivite extends Activity { |
132 | 128 | login = prenom; |
133 | 129 | login.concat("."); |
134 | 130 | login.concat(nom); |
135 | - System.out.println("Prenom : " + prenom); | |
136 | - System.out.println("Nom : " + nom); | |
137 | 131 | } 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(); | |
132 | + toast = Toast.makeText(getApplicationContext(), "Pas de connection possible à la technologie Mifare Classic.", Toast.LENGTH_SHORT); | |
133 | + toast.show(); | |
141 | 134 | } |
142 | 135 | |
143 | - mToast = Toast.makeText(getApplicationContext(), "Login Lille 1 : " + login, Toast.LENGTH_SHORT); | |
144 | - mToast.show(); | |
145 | - System.out.println("Login Lille 1 : " + login); | |
146 | - | |
136 | + toast = Toast.makeText(getApplicationContext(), "Login Lille 1 : " + login, Toast.LENGTH_SHORT); | |
137 | + toast.show(); | |
147 | 138 | |
148 | 139 | //Éxécution de la fonction |
149 | 140 | taFonction(id_carte, login); |
150 | 141 | } |
151 | 142 | } |
152 | 143 | |
153 | - public void taFonction(String id_carte, String login) | |
154 | - { | |
155 | - //code fonction | |
144 | + public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter){ | |
145 | + final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); | |
146 | + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
147 | + final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); | |
148 | + adapter.enableForegroundDispatch(activity, pendingIntent, null, null); | |
156 | 149 | } |
157 | 150 | |
158 | - @Override | |
159 | - public void onPause() { | |
160 | - super.onPause(); | |
161 | - if(mNfcAdapter != null) | |
162 | - mNfcAdapter.disableForegroundDispatch(this); | |
151 | + public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) { | |
152 | + adapter.disableForegroundDispatch(activity); | |
163 | 153 | } |
164 | 154 | } | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java
1 | 1 | package com.example.app_10p5; |
2 | 2 | |
3 | +import android.app.PendingIntent; | |
3 | 4 | import android.content.Intent; |
5 | +import android.content.IntentFilter; | |
6 | +import android.nfc.NfcAdapter; | |
7 | +import android.nfc.Tag; | |
8 | +import android.nfc.tech.MifareClassic; | |
9 | +import android.nfc.tech.NfcA; | |
4 | 10 | import android.os.Bundle; |
5 | 11 | import android.support.design.widget.TabLayout; |
6 | 12 | import android.support.v4.app.FragmentActivity; |
... | ... | @@ -8,6 +14,9 @@ import android.support.v4.app.FragmentManager; |
8 | 14 | import android.support.v4.app.Fragment; |
9 | 15 | import android.support.v4.view.ViewPager; |
10 | 16 | import android.view.View; |
17 | +import android.widget.Toast; | |
18 | + | |
19 | +import java.io.IOException; | |
11 | 20 | |
12 | 21 | /** |
13 | 22 | * Created by beaus on 24/04/2016. |
... | ... | @@ -55,4 +64,5 @@ public class MainActivite extends FragmentActivity { |
55 | 64 | Intent intent = new Intent(this, CarteActivite.class); |
56 | 65 | startActivity(intent); |
57 | 66 | } |
67 | + | |
58 | 68 | } | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment1.java
... | ... | @@ -6,6 +6,10 @@ import android.view.LayoutInflater; |
6 | 6 | import android.view.View; |
7 | 7 | import android.view.ViewGroup; |
8 | 8 | |
9 | +/** | |
10 | + * Created by beaus on 24/04/2016. | |
11 | + */ | |
12 | + | |
9 | 13 | public class TabFragment1 extends Fragment { |
10 | 14 | @Override |
11 | 15 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment2.java
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment3.java
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment4.java
... | ... | @@ -6,6 +6,10 @@ import android.view.LayoutInflater; |
6 | 6 | import android.view.View; |
7 | 7 | import android.view.ViewGroup; |
8 | 8 | |
9 | +/** | |
10 | + * Created by beaus on 24/04/2016. | |
11 | + */ | |
12 | + | |
9 | 13 | public class TabFragment4 extends Fragment { |
10 | 14 | @Override |
11 | 15 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment5.java
... | ... | @@ -6,6 +6,10 @@ import android.view.LayoutInflater; |
6 | 6 | import android.view.View; |
7 | 7 | import android.view.ViewGroup; |
8 | 8 | |
9 | +/** | |
10 | + * Created by beaus on 24/04/2016. | |
11 | + */ | |
12 | + | |
9 | 13 | public class TabFragment5 extends Fragment { |
10 | 14 | @Override |
11 | 15 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ... | ... |
PremiereActivite/app/src/main/res/xml/tech.xml deleted
... | ... | @@ -1,13 +0,0 @@ |
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> | |
14 | 0 | \ No newline at end of file |