Code example for MifareClassic

Methods: authenticateSectorWithKeyA, close, connect, readBlock, writeBlock

2
    	byte[] balance_bytes = new byte[2];
    	int balance = 0;
//    	byte[] myKeyA = {(byte)0x59, (byte)0xBB, (byte)0xB4, (byte)0xD7, (byte)0xC1, (byte)0x9F};				 
//    	byte[] myKeyB = {(byte)0x8F, (byte)0x69, (byte)0xF5, (byte)0x45, (byte)0xF9, (byte)0x79}; 
    	byte[] myKeyA = mainActivity.mSectors.get(4).getKeyA();
    	mfc.connect();
    	if (mfc.authenticateSectorWithKeyA(4, myKeyA)) {
    		data = mfc.readBlock(0 + 4 * 4);
			System.arraycopy(data, 0, balance_bytes, 0, 2);
			balance = (balance_bytes[1] & 0x0FF) * 256 + (balance_bytes[0]  & 0x0FF);
			publishProgress("First 16 bytes on section 4:"); 
			publishProgress(formatHex(data));
			publishProgress(String.format("Original balance %.2f", balance / 100.00));
    	} 
    	if (mfc.authenticateSectorWithKeyA(4, myKeyA)) {
    		balance += this.offset * 100;
    		publishProgress(String.format("Proposed balance %.2f", balance / 100.00));
    		if (balance > 0 && balance < 100000) {
        		balance_bytes[1] = (byte)((balance / 256) & 0x0FF);
        		balance_bytes[0] = (byte)((balance % 256) & 0x0FF);
        		System.arraycopy(balance_bytes, 0, data, 0, 2);
        		data[4] = (byte) (0xFF - data[0]);
        		data[5] = (byte) (0xFF - data[1]);
    			publishProgress("Changed data:"); 
    			publishProgress(formatHex(data));
    			try { 
    				mfc.writeBlock(0 + 4 * 4, data);
    				publishProgress("Successfully wrote new balance. Cheers!"); 
    			} 
    			catch (IOException e) {
    				publishProgress("Cannot write!"); 
    			} 
    		} 
    		else { 
    			publishProgress("Cowardly refusing to write balance that's out of scope."); 
    		} 
    	} 
    	else{ 
    		this.publishProgress("Cowardly refusing to write due to lack of Key B.");
    	} 
    	mfc.close();
		return null; 
    } 
     
    @Override 
	protected Void doInBackground(MifareClassic... params) {