Blame view

App10p5/app/src/main/java/net/plil/clubinfo/app10p5/MainActivity.java 6.58 KB
01212adb   marianne-butaye   Application andro...
1
2
3
4
5
6
  package net.plil.clubinfo.app10p5;
  
  import android.app.PendingIntent;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.nfc.NfcAdapter;
9e7a63d8   marianne-butaye   Version avec la t...
7
8
  import android.nfc.Tag;
  import android.nfc.tech.MifareClassic;
01212adb   marianne-butaye   Application andro...
9
10
11
12
13
14
  import android.nfc.tech.NfcA;
  import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
9e7a63d8   marianne-butaye   Version avec la t...
15
  import android.util.Log;
01212adb   marianne-butaye   Application andro...
16
17
18
19
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  
9e7a63d8   marianne-butaye   Version avec la t...
20
21
  import java.io.IOException;
  
01212adb   marianne-butaye   Application andro...
22
23
24
25
26
27
28
29
30
31
32
33
34
  public class MainActivity extends AppCompatActivity {
  
      private NfcAdapter mNfcAdapter;
      private PendingIntent mPendingIntent;
      private IntentFilter[] mFilters;
      private String[][] mTechLists;
  
      public void onResume() {
          super.onResume();
          if(mNfcAdapter != null)
              mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
      }
  
9e7a63d8   marianne-butaye   Version avec la t...
35
      // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63").
01212adb   marianne-butaye   Application andro...
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
      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;
      }
  
      //La fonction se lance une fois au démarrage de l'application.
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
              }
          });
  
          ////Code NFC -- Nécessaire
          mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  
          mPendingIntent = PendingIntent.getActivity(this, 0,
                  new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  
          IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
  
          try {
              ndef.addDataType("text/plain");
          } catch (IntentFilter.MalformedMimeTypeException e) {
              throw new RuntimeException("fail", e);
          }
          mFilters = new IntentFilter[] {
                  ndef,
          };
  
          // Setup a tech list for all NfcF tags
9e7a63d8   marianne-butaye   Version avec la t...
86
          mTechLists = new String[][]{new String[]{NfcA.class.getName(), MifareClassic.class.getName()}};
01212adb   marianne-butaye   Application andro...
87
88
89
90
91
92
93
      }
  
      @Override
      public void onNewIntent(Intent intent) {
          if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
              String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
              System.out.println("NFC Tag : " + id_carte);
9e7a63d8   marianne-butaye   Version avec la t...
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
  
              //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();
                      String cardData = null;
  
                      //Clé A
                      byte[] cleA = new byte[] { (byte)0xa0, (byte)0xa1, (byte)0xa2,
                              (byte)0xa3, (byte)0xa4, (byte)0xa5 };
                      //On veut juste lire le secteur 12
                      boolean estConnecte = mfc.authenticateSectorWithKeyA(12, cleA);
                      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;
                              System.out.println("Données lues : " + dataStr);
                          }
                      } else {
                          System.out.println("Erreur lors de la connection au secteur 12.");
                      }
                      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);
              }
              else
                  System.out.println("Pas de connection possible à la technologie Mifare Classic.");
  
              //Éxécution de la fonction
              taFonction(id_carte, login);
01212adb   marianne-butaye   Application andro...
146
147
148
          }
      }
  
9e7a63d8   marianne-butaye   Version avec la t...
149
      public void taFonction(String id_carte, String login)
01212adb   marianne-butaye   Application andro...
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
      {
          //code fonction
      }
  
      @Override
      public void onPause() {
          super.onPause();
          if(mNfcAdapter != null)
              mNfcAdapter.disableForegroundDispatch(this);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  }