public async Task ReadNdefMessage(Reader reader, NdefMessage message, byte userMemoryPage = 4) { byte[] com = { 0xff, 0xb0, 0x00, userMemoryPage, 0x00 }; ApduCommand command = new ApduCommand(com); List <byte[]> receivedByte = new List <byte[]>(); ExecStatus success = ExecStatus.Error; byte length = 0; int index = 0; await SendAPDUCommand(reader, command, (s, r, c, rec) => { if (s == ExecStatus.Success) { receivedByte.Add(rec); } success = s; }); if (success != ExecStatus.Success || receivedByte[index][0] != 0x03) { if (Global.SyncContext != null) { Global.SyncContext.Post((state) => { ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state; if (OnReadNdefMessage != null) { OnReadNdefMessage(false, t.Item1, t.Item2); } }, new ValueTuple <Reader, NdefMessage>(reader, message)); } else { if (OnReadNdefMessage != null) { OnReadNdefMessage(false, reader, message); } } return; } length = (byte)(receivedByte[index][1] + 2); int currentLength = length - 16; int numRead = 1; while (currentLength > 0) { com[3] += 0x04; command = new ApduCommand(com); success = ExecStatus.Error; await SendAPDUCommand(reader, command, (s, r, c, rec) => { if (s == ExecStatus.Success) { receivedByte.Add(rec); } success = s; numRead++; }); if (success == ExecStatus.Success) { if (Global.SyncContext != null) { Global.SyncContext.Post((state) => { ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state; if (OnReadNdefMessage != null) { OnReadNdefMessage(false, t.Item1, t.Item2); } }, new ValueTuple <Reader, NdefMessage>(reader, message)); } else { if (OnReadNdefMessage != null) { OnReadNdefMessage(false, reader, message); } } return; } currentLength -= 16; } byte[] binary = new byte[numRead * 16]; for (int i = 0; i < receivedByte.Count; i++) { Array.Copy(receivedByte[i], 0, binary, 16 * i, 16); } message.FromBinaryData(binary); if (Global.SyncContext != null) { Global.SyncContext.Post((state) => { ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state; if (OnReadNdefMessage != null) { OnReadNdefMessage(true, t.Item1, t.Item2); } }, new ValueTuple <Reader, NdefMessage>(reader, message)); } else { if (OnReadNdefMessage != null) { OnReadNdefMessage(true, reader, message); } } }
public async Task WriteNdefMessage(Reader reader, NdefMessage message, byte userMemoryPage = 4, byte writeUnitSize = 4) { byte[] binary = message.ToBinaryData(); int length = binary.Length; ApduCommand command; int currentIndex = 0; int numWrite = 0; while (length > 0) { byte[] com = new byte[6 + writeUnitSize]; com[0] = 0xff; com[1] = 0xd6; com[2] = 0x00; com[3] = (byte)(userMemoryPage + numWrite); com[4] = writeUnitSize; int l = writeUnitSize; if (length < l) { l = (byte)length; } Array.Copy(binary, currentIndex, com, 5, l); command = new ApduCommand(com); ExecStatus success = ExecStatus.Error; await SendAPDUCommand(reader, command, (s, r, c, rec) => { success = s; }); if (success == ExecStatus.Error) { if (Global.SyncContext != null) { Global.SyncContext.Post((state) => { ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state; if (OnWriteNdefMessage != null) { OnWriteNdefMessage(false, t.Item1, t.Item2); } }, new ValueTuple <Reader, NdefMessage>(reader, message)); } else { if (OnWriteNdefMessage != null) { OnWriteNdefMessage(false, reader, message); } } return; } length -= writeUnitSize; currentIndex += writeUnitSize; numWrite++; } if (Global.SyncContext != null) { Global.SyncContext.Post((state) => { ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state; if (OnWriteNdefMessage != null) { OnWriteNdefMessage(true, t.Item1, t.Item2); } }, new ValueTuple <Reader, NdefMessage>(reader, message)); } else { if (OnWriteNdefMessage != null) { OnWriteNdefMessage(true, reader, message); } } }