Blame view

PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java 5.57 KB
c8059613   JLo'w   Re-fonte de l'app...
1
2
3
  package com.example.app_10p5;
  
  import android.app.Activity;
6feece85   JLo'w   ajout NFC
4
5
  import android.app.PendingIntent;
  import android.content.Intent;
6feece85   JLo'w   ajout NFC
6
7
8
  import android.nfc.NfcAdapter;
  import android.nfc.Tag;
  import android.nfc.tech.MifareClassic;
c8059613   JLo'w   Re-fonte de l'app...
9
  import android.os.Bundle;
6feece85   JLo'w   ajout NFC
10
11
12
  import android.widget.Toast;
  
  import java.io.IOException;
c8059613   JLo'w   Re-fonte de l'app...
13
14
15
16
17
  
  /**
   * Created by beaus on 24/04/2016.
   */
  public class CarteActivite extends Activity {
6feece85   JLo'w   ajout NFC
18
19
  
      private NfcAdapter mNfcAdapter;
6feece85   JLo'w   ajout NFC
20
  
c8059613   JLo'w   Re-fonte de l'app...
21
22
23
24
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layout_carte);
6feece85   JLo'w   ajout NFC
25
  
6feece85   JLo'w   ajout NFC
26
27
          mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  
93f90e35   JLo'w   NFC et foreground
28
29
30
31
32
          if (mNfcAdapter == null) {
              // Stop here, we definitely need NFC
              Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
              finish();
              return;
6feece85   JLo'w   ajout NFC
33
          }
6feece85   JLo'w   ajout NFC
34
  
93f90e35   JLo'w   NFC et foreground
35
          handleIntent(getIntent());
6feece85   JLo'w   ajout NFC
36
37
      }
  
93f90e35   JLo'w   NFC et foreground
38
39
      @Override
      protected void onResume() {
6feece85   JLo'w   ajout NFC
40
          super.onResume();
93f90e35   JLo'w   NFC et foreground
41
          setupForegroundDispatch(this, mNfcAdapter);
6feece85   JLo'w   ajout NFC
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
      }
  
      // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63").
      private String ByteArrayToHexString(byte [] inarray) {
          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 < inarray.length ; ++j)
          {
              in = (int) inarray[j] & 0xff;
              i = (in >> 4) & 0x0f;
              out += hex[i];
              i = in & 0x0f;
              out += hex[i];
          }
          return out;
      }
  
      @Override
93f90e35   JLo'w   NFC et foreground
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
      protected void onNewIntent(Intent intent) {
          handleIntent(intent);
      }
  
      public void taFonction(String id_carte, String login)
      {
          //code fonction
      }
  
      @Override
      protected void onPause() {
          stopForegroundDispatch(this, mNfcAdapter);
          super.onPause();
      }
  
      private void handleIntent(Intent intent){
          if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
              Toast toast;
6feece85   JLo'w   ajout NFC
79
              String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
93f90e35   JLo'w   NFC et foreground
80
81
              toast = Toast.makeText(getApplicationContext(), "ID Carte : " + id_carte, Toast.LENGTH_SHORT);
              toast.show();
6feece85   JLo'w   ajout NFC
82
83
84
85
86
87
88
89
90
91
  
              //Lecture des données
              Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
              MifareClassic mfc = MifareClassic.get(tag);
              byte[] data;
              String prenom = null, nom = null, login = null;
  
              if (mfc != null) {
                  try {
                      mfc.connect();
6feece85   JLo'w   ajout NFC
92
93
94
95
  
                      //Clé A
                      byte[] cleA = new byte[]{(byte) 0xa0, (byte) 0xa1, (byte) 0xa2,
                              (byte) 0xa3, (byte) 0xa4, (byte) 0xa5};
93f90e35   JLo'w   NFC et foreground
96
  
6feece85   JLo'w   ajout NFC
97
98
                      //On veut juste lire le secteur 12
                      boolean estConnecte = mfc.authenticateSectorWithKeyA(12, cleA);
93f90e35   JLo'w   NFC et foreground
99
  
6feece85   JLo'w   ajout NFC
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
                      if (estConnecte) {
                          //Il y a 4 blocs dans le secteur 12 -> INE, numéro étudiant, prénom, nom
                          //On ne veut que prénom et nom, donc on passe les deux premiers
                          for (int i = 2, bIndex = mfc.sectorToBlock(12) + i; i < 4; i++, bIndex++) {
                              //sectorToBlock : Renvoie l'indice (global, parmis tous les blocs de
                              // tous les secteurs) du premier bloc du secteur 12
                              //Lit les données du bloc
                              data = mfc.readBlock(bIndex);
  
                              //Convertit les bytes en String
                              String dataStr = new String(data);
                              if (i == 2) //Prénom
                                  prenom = dataStr;
                              else if (i == 3) //Nom
                                  nom = dataStr;
93f90e35   JLo'w   NFC et foreground
115
116
                              toast = Toast.makeText(getApplicationContext(), "Données lues : " + dataStr, Toast.LENGTH_SHORT);
                              toast.show();
6feece85   JLo'w   ajout NFC
117
118
                          }
                      } else {
93f90e35   JLo'w   NFC et foreground
119
120
                          toast = Toast.makeText(getApplicationContext(), "Erreur lors de la connection au secteur 12.", Toast.LENGTH_SHORT);
                          toast.show();
6feece85   JLo'w   ajout NFC
121
122
123
124
125
126
127
128
129
130
                      }
                      mfc.close();
                  } catch (IOException e) {
                      System.out.println(e.getLocalizedMessage());
                  }
  
                  //Concaténation des données récupérées en login
                  login = prenom;
                  login.concat(".");
                  login.concat(nom);
6feece85   JLo'w   ajout NFC
131
              } else {
93f90e35   JLo'w   NFC et foreground
132
133
                  toast = Toast.makeText(getApplicationContext(), "Pas de connection possible à la technologie Mifare Classic.", Toast.LENGTH_SHORT);
                  toast.show();
6feece85   JLo'w   ajout NFC
134
135
              }
  
93f90e35   JLo'w   NFC et foreground
136
137
              toast = Toast.makeText(getApplicationContext(), "Login Lille 1 : " + login, Toast.LENGTH_SHORT);
              toast.show();
6feece85   JLo'w   ajout NFC
138
139
140
141
  
              //Éxécution de la fonction
              taFonction(id_carte, login);
          }
c8059613   JLo'w   Re-fonte de l'app...
142
143
      }
  
93f90e35   JLo'w   NFC et foreground
144
145
146
147
148
      public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter){
          final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
          intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
          final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
          adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
6feece85   JLo'w   ajout NFC
149
150
      }
  
93f90e35   JLo'w   NFC et foreground
151
152
      public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
          adapter.disableForegroundDispatch(activity);
6feece85   JLo'w   ajout NFC
153
      }
c8059613   JLo'w   Re-fonte de l'app...
154
  }