mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-03-13 23:31:48 +01:00
67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
|
using System;
|
|||
|
using System.Threading;
|
|||
|
using CoreNFC;
|
|||
|
using Foundation;
|
|||
|
using NFC;
|
|||
|
|
|||
|
namespace Borepin.iOS.CNFC
|
|||
|
{
|
|||
|
public class Card : ICard
|
|||
|
{
|
|||
|
private NFCTagReaderSession _session;
|
|||
|
private INFCMiFareTag _tag;
|
|||
|
|
|||
|
public Card(NFCTagReaderSession session, INFCMiFareTag tag)
|
|||
|
{
|
|||
|
_session = session;
|
|||
|
_tag = tag;
|
|||
|
}
|
|||
|
|
|||
|
public void Connect()
|
|||
|
{
|
|||
|
var counter = new CountdownEvent(1);
|
|||
|
NSError err = null;
|
|||
|
|
|||
|
_session.ConnectTo(_tag, (error) =>
|
|||
|
{
|
|||
|
err = error;
|
|||
|
counter.Signal();
|
|||
|
});
|
|||
|
|
|||
|
counter.Wait();
|
|||
|
|
|||
|
if (err != null)
|
|||
|
{
|
|||
|
throw new Exception(err.LocalizedDescription);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void Disconnect()
|
|||
|
{
|
|||
|
// TODO: decide on which should be used
|
|||
|
//_session.RestartPolling();
|
|||
|
_session.InvalidateSession("card disconnect");
|
|||
|
}
|
|||
|
|
|||
|
public APDUResponse Transmit(APDUCommand cmd)
|
|||
|
{
|
|||
|
var counter = new CountdownEvent(1);
|
|||
|
byte[] buf = null;
|
|||
|
|
|||
|
_tag.SendMiFareIso7816Command(new NFCIso7816Apdu(NSData.FromArray(cmd.Data)), (response, sw1, sw2, NSError) =>
|
|||
|
{
|
|||
|
// reassembly the original apdu message
|
|||
|
buf = new byte[response.Length + 2];
|
|||
|
response.ToArray().CopyTo(buf, 0);
|
|||
|
buf[response.Length + 0] = sw1;
|
|||
|
buf[response.Length + 1] = sw2;
|
|||
|
counter.Signal();
|
|||
|
});
|
|||
|
|
|||
|
counter.Wait();
|
|||
|
|
|||
|
return new APDUResponse(buf);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|