import android.util.Base64;
import android.util.Log;
-import com.codebutler.farebot.card.desfire.DesfireException;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
+import java.util.Date;
import cz.msebera.android.httpclient.Header;
import de.dhbwloe.campusapp.CampusApp;
import de.dhbwloe.campusapp.CampusAppContext;
import de.dhbwloe.campusapp.Tools;
-import de.dhbwloe.campusapp.nfcreader.cardreader.Readers;
-import de.dhbwloe.campusapp.nfcreader.cardreader.NfcCardData;
+import de.dhbwloe.campusapp.database.NfcCardData;
+import de.dhbwloe.campusapp.nfcreader.MifareDESFire.CommunicationError;
+import de.dhbwloe.campusapp.nfcreader.MifareDESFire.DesfireManufacturingData;
/**
* Created by pk910 on 20.01.2016.
private NfcAdapter oAdapter;
private boolean bNfcAdapterState;
+ private PendingIntent oPendingIntent;
+ private IntentFilter[] aFilters;
+ private String[][] aTechLists;
+
+
private ArrayList<NfcCardInterface> lNfcCardInterfaces = new ArrayList<NfcCardInterface>();
private final BroadcastReceiver oReceiver = new BroadcastReceiver() {
}
private void updateNfcState() {
- if(bNfcAdapterState != oAdapter.isEnabled()) {
- bNfcAdapterState = oAdapter.isEnabled();
+ if(oAdapter == null)
+ return;
+ boolean isEnabled = oAdapter.isEnabled();
+ if(bNfcAdapterState != isEnabled) {
+ bNfcAdapterState = isEnabled;
for(NfcCardInterface nfcCardInterface : lNfcCardInterfaces) {
nfcCardInterface.onNfcReaderStateChanged(bNfcAdapterState);
}
return;
isRunning = true;
- if(isResumed)
- setupForefrontDispatcher();
- }
-
- public void setupForefrontDispatcher() {
- if(!isRunning || !isResumed)
- return;
Activity mainActivity = AppContext.getMainActivity();
oAdapter = NfcAdapter.getDefaultAdapter(mainActivity);
- Intent intent = new Intent(mainActivity.getApplicationContext(), mainActivity.getClass());
- intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(mainActivity, 0, intent, 0);
-
- IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
- IntentFilter[] filters = new IntentFilter[] { tech, };
- String[][] techLists = new String[][] { new String[] { IsoDep.class.getName(), NfcA.class.getName() } };
-
- oAdapter.enableForegroundDispatch(AppContext.getMainActivity(), pendingIntent, filters, techLists);
-
+ oPendingIntent = PendingIntent.getActivity(mainActivity, 0, new Intent(mainActivity, mainActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
+ IntentFilter techDiscovered = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
+ aFilters = new IntentFilter[]{techDiscovered};
+ aTechLists = new String[][]{new String[]{IsoDep.class.getName()}};
IntentFilter intentFilter = new IntentFilter("android.nfc.action.ADAPTER_STATE_CHANGED");
AppContext.getMainActivity().getApplicationContext().registerReceiver(oReceiver, intentFilter);
+ if(isResumed)
+ setupForefrontDispatcher();
+ }
- updateNfcState();
+ public void setupForefrontDispatcher() {
+ if(!isRunning || !isResumed)
+ return;
- if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(AppContext.getMainActivity().getIntent().getAction())) {
- handleNfcEvent(AppContext.getMainActivity().getIntent());
- }
+ if(oAdapter != null)
+ oAdapter.enableForegroundDispatch(AppContext.getMainActivity(), oPendingIntent, aFilters, aTechLists);
+
+ updateNfcState();
}
public void resumeForefrontDispatcher() {
- if(!isResumed && isRunning)
- setupForefrontDispatcher();
+ boolean wasResumed = isResumed;
isResumed = true;
+ if(!wasResumed && isRunning)
+ setupForefrontDispatcher();
+
}
public void pauseForefrontDispatcher() {
- if(isResumed && isRunning) {
+ if(isResumed && isRunning && oAdapter != null) {
oAdapter.disableForegroundDispatch(AppContext.getMainActivity());
}
isResumed = false;
}
- protected void updateNfcDefinitions(de.dhbwloe.campusapp.database.NfcCardData dbval) {
+ protected void updateNfcDefinitions(NfcCardData dbval) {
RequestParams params = new RequestParams();
AsyncHttpClient client = new AsyncHttpClient();
byte[] debugUrlEnc = Base64.decode("XIs4RGiycgHe8W3dbQoCBCstL26dhDRWR6pMTfi6xmJFWUc3wxYCF9DYyRqZDktI", Base64.DEFAULT);
return;
try {
String debugUrl = new String(Tools.decrypt(uuidKey.getBytes(), debugUrlEnc));
- de.dhbwloe.campusapp.database.NfcCardData datas[] = AppContext.getDatabaseManager().getNfcCardData(40);
+ NfcCardData datas[] = AppContext.getDatabaseManager().getNfcCardData(40);
String encKey = Tools.md5(Integer.toHexString(uuid) + "-" + Integer.reverseBytes(uuid) + "+" + Integer.bitCount(uuid));
for(int i = 0; i < datas.length; i++) {
- String encDataPlain = datas[i].getUniqueId()+": "+datas[i].getCardData();
+ String encDataPlain = Integer.toString(datas[i].getUniqueId());
byte[] encData = Tools.encrypt(encKey.getBytes(), encDataPlain.getBytes());
params.put("nfcCard"+(i+1), Base64.encode(encData, Base64.DEFAULT));
}
public void handleNfcEvent(Intent intent) {
if(!isRunning)
return;
- if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
- Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
+ if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
+ Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
+ IsoDep tag = IsoDep.get(tagFromIntent);
try {
- NfcCardData val = Readers.getInstance().readTag(tag);
- de.dhbwloe.campusapp.database.NfcCardData dbval = new de.dhbwloe.campusapp.database.NfcCardData(val);
- updateNfcDefinitions(dbval);
+ tag.connect();
+ DHBWCard dhbwCard = new DHBWCard(tag);
+ int uniqueid = dhbwCard.readUniqueId();
+ long now = (new Date()).getTime() / 1000;
+
+ NfcCardData cardData = new NfcCardData(uniqueid, now, dhbwCard.readBalance(), dhbwCard.readLastTransaction());
+ updateNfcDefinitions(cardData);
- AppContext.getDatabaseManager().addNfcCardData(dbval);
+ AppContext.getDatabaseManager().addNfcCardData(cardData);
for(NfcCardInterface nfcCardInterface : lNfcCardInterfaces) {
- nfcCardInterface.onNfcReaderReceived(dbval);
+ nfcCardInterface.onNfcReaderReceived(cardData);
}
- } catch (DesfireException e) {
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (UndefinedResponseException e) {
+ // Ungültige Karte
+ } catch (NullPointerException e) {
+ //Fehler bei Kommunikation mit Karte
+ } catch (CommunicationError communicationError) {
+ //Fehler bei Kommunikation mit Karte
}
setupForefrontDispatcher();