4 * Copyright (C) 2014 Jakob Wenzel
7 * Jakob Wenzel <jakobwenzel92@gmail.com>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package de.dhbwloe.campusapp.nfcreader.cardreader;
25 import android.nfc.Tag;
26 import android.nfc.tech.IsoDep;
27 import android.util.Log;
29 import com.codebutler.farebot.card.desfire.DesfireException;
30 import com.codebutler.farebot.card.desfire.DesfireProtocol;
32 import java.io.IOException;
34 public class Readers implements ICardReader {
35 private static final String TAG = Readers.class.getName();
36 private static Readers instance;
37 private ICardReader[] readers = new ICardReader[]{
38 new MagnaCartaReader(),
39 new IntercardReader()};
43 public NfcCardData readCard(DesfireProtocol card) throws DesfireException {
44 Log.i(TAG, "Trying all readers");
45 for (ICardReader reader : readers) {
46 Log.i(TAG, "Trying " + reader.getClass().getSimpleName());
47 NfcCardData val = reader.readCard(card);
55 public NfcCardData readTag(Tag tag) throws DesfireException {
56 Log.i(TAG, "Loading tag");
57 IsoDep tech = IsoDep.get(tag);
61 } catch (IOException e) {
62 //Tag was removed. We fail silently.
68 DesfireProtocol desfireTag = new DesfireProtocol(tech);
71 //Android has a Bug on Devices using a Broadcom NFC chip. See
72 // http://code.google.com/p/android/issues/detail?id=58773
73 //A Workaround is to connected to the tag, issue a dummy operation and then reconnect...
75 desfireTag.selectApp(0);
76 }catch (ArrayIndexOutOfBoundsException e) {
77 //Exception occurs because the actual response is shorter than the error response
78 Log.i(TAG, "Broadcom workaround was needed");
84 NfcCardData val = Readers.getInstance().readCard(desfireTag);
86 } catch (IOException e) {
87 //This can only happen on tag close. we ignore this.
91 if (tech.isConnected())
94 } catch (IOException e) {
101 public static Readers getInstance() {
102 if (instance == null)
103 instance = new Readers();