/// <summary> /// Reads 16 bytes /// </summary> /// <param name="blockNumber"> /// Block number to read /// </param> /// <param name="keyType"> /// Choose MIFARE key A or B /// </param> /// <param name="keySlotNumber"> /// Slot where key has previously been stored with a previous call to Load Keys /// </param> /// <returns> /// byte array of 16 bytes /// </returns> public async Task<byte[]> ReadAsync(ushort blockNumber, GeneralAuthenticate.GeneralAuthenticateKeyType keyType, byte keySlotNumber = 0) { var genAuthRes = await connectionObject.TransceiveAsync(new MifareStandard.GeneralAuthenticate(blockNumber, keySlotNumber, keyType)); if (!genAuthRes.Succeeded) { throw new Exception("Failure authenticating to MIFARE Standard card, " + genAuthRes.ToString()); } var readRes = await connectionObject.TransceiveAsync(new MifareStandard.Read(blockNumber)); if (!readRes.Succeeded) { throw new Exception("Failure reading MIFARE Standard card, " + readRes.ToString()); } return readRes.ResponseData; }
/// <summary> /// Wrapper method write 16 bytes at the pageAddress /// </param name="blockNumber"> /// Block number /// </param> /// <param name="keyType"> /// Choose MIFARE key A or B /// </param> /// <param name="keySlotNumber"> /// Slot where key has previously been stored with a previous call to Load Keys /// </param> /// byte array of the data to write /// </returns> public async void WriteAsync(byte blockNumber, byte[] data, GeneralAuthenticate.GeneralAuthenticateKeyType keyType, byte keySlotNumber = 0) { if (data.Length != 16) { throw new NotSupportedException(); } var genAuthRes = await connectionObject.TransceiveAsync(new MifareStandard.GeneralAuthenticate(blockNumber, keySlotNumber, keyType)); if (!genAuthRes.Succeeded) { throw new Exception("Failure authenticating to MIFARE Standard card, " + genAuthRes.ToString()); } var apduRes = await connectionObject.TransceiveAsync(new MifareStandard.Write(blockNumber, ref data)); if (!apduRes.Succeeded) { throw new Exception("Failure writing MIFARE Standard card, " + apduRes.ToString()); } }