It specifies MemoryAccess interface and Memory class.
First we have to get object of MemoryAccess object like:
oMemAccess = Memory.getMemoryAccessInstance(Memory.MEMORY_TYPE_MIFARE
, null
, (short)0);
Memory.getMemoryAccessInstance() method has the following parameters:
- memoryType - the desired external memory subsystem. Could be MEMORY_TYPE_MIFARE or MEMORY_TYPE_EXTENDED_STORE.
- memorySize - the array containing the desired size in bytes, if applicable, in the external memory subsystem. This parameter is ignored for MIFARE memory type.
- memorySizeOffset - the offset within the memorySize array where the 32 bit memory size number in bytes is specified. This parameter is ignored.
As you can see from above parameters list we need only specify memory type to get access MIFARE memory.
Then to write data to we can use MemoryAccess.writeData() method like:
oMemAccess.writeData( dataToWrite // the source data byte array , (short) 0 // the byte offset in data buffer , (short) dataToWrite.length // the length of data , thisCardPwdArray // the byte array containing the // key (password) , (short)0 // the byte offset into the key // array where the key data begins , (short) thisCardPwdArray.length // the length in bytes of key , (short) (blocknum / 4) // sector number , blocknum) // block number
I'd like to mention one point about sector and block numbers. There are 2 type of addressing mode:
- Absolute mode where block number accepts values 0..63 and sector number will be ignored.
- Relative mode where block number accepts values 0..4 and sector number must be correctly set according to MIFARE memory layout.
To read data there is the method MemoryAccess.readData():
All parameters have the same meaning as in writeData() method.
One additional remark: if the password to access MIFARE memory is incorrect there is no retry mechanism. You have to start from the beginning. The reason why it has implemented like that is absence of key counter like in PIN key.
oMemAccess.readData( readBuf // destination buffer , (short) 0 // offset in destination buffer , thisCardPwdArray // key (password) array , (short)0 // offset in key array , (short) thisCardPwdArray.length // key length , (short)secnum // sector number , (short)blocknum // block number , DATA_LEN) // number of bytes to read
All parameters have the same meaning as in writeData() method.
One additional remark: if the password to access MIFARE memory is incorrect there is no retry mechanism. You have to start from the beginning. The reason why it has implemented like that is absence of key counter like in PIN key.