/// <summary> /// Raise tag detected event /// </summary> /// <param name="nfcTagType">NFC tag type</param> /// <param name="conn">Connection instance to NFC tag</param> private void OnTagDetected(NfcTagType nfcTagType, NfcTagConnection conn) { if (this.TagDetected != null) { this.TagDetected(this, new NfcTagEventArgs(nfcTagType, conn)); } }
/// <summary> /// Thread for tag detecting /// </summary> private void DetectionThread() { while (this.isRunning) { byte[] target = this.pn532.InListPassiveTarget(this.targetType); // target detected if (target != null) { switch (this.targetType) { case PN532.TargetType.Iso14443TypeA: // no current tag set if (this.nfcTagConn == null) { // get ATQA and SAK byte[] ATQA = new byte[2]; Array.Copy(target, PN532.ISO14443A_SENS_RES_OFFSET + 2, ATQA, 0, ATQA.Length); // +2 for 4B and NbTg (pn532um.pdf, pag. 116) byte SAK = target[PN532.ISO14443A_SEL_RES_OFFSET + 2]; // +2 for 4B and NbTg (pn532um.pdf, pag. 116) // get tag ID byte[] tagId = new byte[target[PN532.ISO14443A_IDLEN_OFFSET + 2]]; // +2 for 4B and NbTg (pn532um.pdf, pag. 116) Array.Copy(target, PN532.ISO14443A_IDLEN_OFFSET + 2 + 1, tagId, 0, tagId.Length); // check ATQA and SAK for Mifare Classic 1k if (ATQA[0] == 0x00 && ATQA[1] == 0x04 && SAK == 0x08) { this.nfcTagType = NfcTagType.MifareClassic1k; this.nfcTagConn = new NfcMifareTagConnection(this, tagId); this.OnTagDetected(this.nfcTagType, this.nfcTagConn); } // check ATQA and SAK for Mifare Ultralight else if (ATQA[0] == 0x00 && ATQA[1] == 0x44 && SAK == 0x00) { this.nfcTagType = NfcTagType.MifareUltralight; this.nfcTagConn = new NfcMifareUlTagConnection(this, tagId); this.OnTagDetected(this.nfcTagType, this.nfcTagConn); } } break; default: break; } } // no target detected else { // if current tag set and no target detected... if (this.nfcTagConn != null) { // ...current tag is lost this.OnTagLost(this.nfcTagType, this.nfcTagConn); this.nfcTagConn = null; } } } }
public void Open(NfcTagType nfcTagType) { this.nfcTagType = nfcTagType; // map NFC tag type to PN532 target type (baud rate/protocol) this.targetType = this.NfcToPN532Type(nfcTagType); this.isRunning = true; this.nfcTagConn = null; // get info and configure PN532 ic this.pn532.GetFirmwareVersion(); this.pn532.SAMConfiguration(PN532.SamMode.NormalMode, false); // start thread for tag detection this.detectionThread = new Thread(this.DetectionThread); this.detectionThread.Start(); }
public async Task Open(NfcTagType nfcTagType) { this._nfcTagType = nfcTagType; // map NFC tag type to PN532 target type (baud rate/protocol) _targetType = NfcToPn532Type(nfcTagType); _nfcTagConn = null; // get info and configure PN532 ic await _pn532.GetFirmwareVersion(); await _pn532.SAMConfiguration(PN532.SamMode.NormalMode, false); _isRunning = true; // start thread for tag detection await Task.Factory.StartNew(DetectionThread, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); }
/// <summary> /// Constructor /// </summary> /// <param name="nfcTagType">NFC tag type</param> /// <param name="conn">Connection instance to NFC tag</param> public NfcTagEventArgs(NfcTagType nfcTagType, NfcTagConnection conn) { this.NfcTagType = nfcTagType; this.Connection = conn; }
public void Close() { this.nfcTagConn = null; this.isRunning = false; }
public void Close() { _pn532.Close(); _nfcTagConn = null; _isRunning = false; }
/// <summary> /// Raise tag lost event /// </summary> /// <param name="nfcTagType">NFC tag type</param> /// <param name="conn">Connection instance to NFC tag</param> private void OnTagLost(NfcTagType nfcTagType, NfcTagConnection conn) { TagLost?.Invoke(this, new NfcTagEventArgs(nfcTagType, conn)); }
/// <summary> /// Thread for tag detecting /// </summary> private async Task DetectionThread() { while (_isRunning) { try { var target = await _pn532.InListPassiveTarget(_targetType); // target detected if (target != null) { switch (_targetType) { case PN532.TargetType.Iso14443TypeA: // no current tag set if (_nfcTagConn == null) { // get ATQA and SAK byte[] atqa = new byte[2]; Array.Copy(target, PN532.ISO14443A_SENS_RES_OFFSET + 2, atqa, 0, atqa.Length); // +2 for 4B and NbTg (pn532um.pdf, pag. 116) byte sak = target[PN532.ISO14443A_SEL_RES_OFFSET + 2]; // +2 for 4B and NbTg (pn532um.pdf, pag. 116) // get tag ID byte[] tagId = new byte[target[PN532.ISO14443A_IDLEN_OFFSET + 2]]; // +2 for 4B and NbTg (pn532um.pdf, pag. 116) Array.Copy(target, PN532.ISO14443A_IDLEN_OFFSET + 2 + 1, tagId, 0, tagId.Length); // check ATQA and SAK for Mifare Classic 1k if (atqa[0] == 0x00 && atqa[1] == 0x04 && sak == 0x08) { _nfcTagType = NfcTagType.MifareClassic1K; _nfcTagConn = new NfcMifareTagConnection(this, tagId); OnTagDetected(_nfcTagType, _nfcTagConn); } // check ATQA and SAK for Mifare Ultralight else if (atqa[0] == 0x00 && atqa[1] == 0x44 && sak == 0x00) { _nfcTagType = NfcTagType.MifareUltralight; _nfcTagConn = new NfcMifareUlTagConnection(this, tagId); OnTagDetected(_nfcTagType, _nfcTagConn); } } break; default: break; } } // no target detected else { // if current tag set and no target detected... if (_nfcTagConn != null) { // ...current tag is lost OnTagLost(_nfcTagType, _nfcTagConn); _nfcTagConn = null; } } } catch (Exception) { //TODO LOG ex _isRunning = false; } } }
/// <summary> /// Raise tag lost event /// </summary> /// <param name="nfcTagType">NFC tag type</param> /// <param name="conn">Connection instance to NFC tag</param> private void OnTagLost(NfcTagType nfcTagType, NfcTagConnection conn) { if (this.TagLost != null) this.TagLost(this, new NfcTagEventArgs(nfcTagType, conn)); }
/// <summary> /// Raise tag detected event /// </summary> /// <param name="nfcTagType">NFC tag type</param> /// <param name="conn">Connection instance to NFC tag</param> private void OnTagDetected(NfcTagType nfcTagType, NfcTagConnection conn) { if (this.TagDetected != null) this.TagDetected(this, new NfcTagEventArgs(nfcTagType, conn)); }