Implemented NFC Card Reader Code from https://git.sterul.com/student-projects/dhbw...
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / nfcreader / DHBWCard.java
1 package de.dhbwloe.campusapp.nfcreader;
2
3 import android.nfc.tech.IsoDep;
4
5 import de.dhbwloe.campusapp.nfcreader.MifareDESFire.CommunicationError;
6 import de.dhbwloe.campusapp.nfcreader.MifareDESFire.DesfireManufacturingData;
7 import de.dhbwloe.campusapp.nfcreader.MifareDESFire.MifareDESFireWrapper;
8 import de.dhbwloe.campusapp.nfcreader.MifareDESFire.ValueFileSettings;
9
10 import java.text.DecimalFormat;
11
12 /**
13  * Created by stefan on 20.01.16.
14  */
15 public class DHBWCard {
16
17 //    /* Commands */
18 //    public static final byte GET_VERSION_INFO    = (byte) 0x60;
19 //    public static final byte GET_APPLICATION_DIRECTORY = (byte) 0x6A;
20 //    public static final byte GET_ADDITIONAL_FRAME      = (byte) 0xAF;
21 //    public static final byte SELECT_APPLICATION        = (byte) 0x5A;
22 //    public static final byte READ_DATA                 = (byte) 0xBD;
23 //    public static final byte READ_RECORD               = (byte) 0xBB;
24 //    public static final byte GET_FILES                 = (byte) 0x6F;
25 //    public static final byte GET_FILE_SETTINGS         = (byte) 0xF5;
26 //
27 //    public static final byte INIT_AUTH         = (byte) 0x0a;
28 //    public static final byte FINISH_AUTH         = (byte) 0xAF;
29 //
30 //    public static final byte GET_KEY_VERSION = (byte) 0x64;
31
32     private MifareDESFireWrapper nfcCard;
33
34     public DHBWCard(IsoDep tag) {
35         this.nfcCard = new MifareDESFireWrapper(tag);
36     }
37
38
39     /**
40      * Reads the current uniqueid on the card.
41      * Throws a UndefinedResponseException if the card returns an unexpected response (e.g. used another card)
42      *
43      * @return Returns the unique id
44      * @throws UndefinedResponseException
45      */
46     public int readUniqueId() throws UndefinedResponseException, CommunicationError {
47         // Select application for balance
48         DesfireManufacturingData manufacturingData = nfcCard.readManufacturingData();
49         return manufacturingData.uid;
50     }
51
52     /**
53      * Reads the current balance on the card.
54      * Throws a UndefinedResponseException if the card returns an unexpected response (e.g. used another card)
55      *
56      * @return Returns the balance
57      * @throws UndefinedResponseException
58      */
59     public int readBalance() throws UndefinedResponseException, CommunicationError {
60         // Select application for balance
61         nfcCard.selectApplication("5F 84 15");
62         int value = nfcCard.readValueFile("01");
63         return value / 10;
64     }
65
66
67     /**
68      * Reads the last transaction by reading the limited credit value
69      * @return Returns the last transaction of the card
70      * @throws UndefinedResponseException
71      */
72     public int readLastTransaction() throws UndefinedResponseException, CommunicationError {
73         nfcCard.selectApplication("5F 84 15");
74         ValueFileSettings valueFileSettings = (ValueFileSettings) nfcCard.readFileSettings("01");
75         return valueFileSettings.getLimitedCreditValue() / 10;
76     }
77 }