/** * Callback when a new tag is discovered by the system. * * <p>Communication with the card should take place here. * * @param tag Discovered tag */ public void OnTagDiscovered(Android.Nfc.Tag tag) { Console.WriteLine("New tag discovered"); IsoDep isoDep = IsoDep.Get(tag); if (isoDep != null) { try { // Connect to the remote NFC device isoDep.Connect(); byte[] command = new byte[] { (byte)0x00, /* CLA = 00 (first interindustry command set) */ (byte)0xA4, /* INS = A4 (SELECT) */ (byte)0x04, /* P1 = 04 (select file by DF name) */ (byte)0x0C, /* P2 = 0C (first or only file; no FCI) */ (byte)0x07, /* Lc = 7 (data/AID has 7 bytes) */ /* AID = D6 16 00 00 30 01 01: */ (byte)0xD6, (byte)0x16, (byte)0x00, (byte)0x00, (byte)0x30, (byte)0x01, (byte)0x01, (byte)0x00 }; byte[] result = isoDep.Transceive(command); // If AID is successfully selected, 0x9000 is returned as the status word (last 2 // bytes of the result) by convention. Everything before the status word is // optional payload, which is used here to hold the account number. int resultLength = result.Length; byte[] statusWord = { result[resultLength - 2], result[resultLength - 1] }; byte[] payload = new byte[resultLength - 2]; Array.Copy(result, payload, resultLength - 2); bool arrayEquals = SELECT_OK_SW.Length == statusWord.Length; for (int i = 0; i < SELECT_OK_SW.Length && i < statusWord.Length && arrayEquals; i++) { arrayEquals = (SELECT_OK_SW[i] == statusWord[i]); if (!arrayEquals) { break; } } if (arrayEquals) { // The remote NFC device will immediately respond with its stored account number string info = System.Text.Encoding.UTF8.GetString(result); Console.WriteLine("Received: " + info); // Inform CardReaderFragment of received account number InfoCallback ourCallback; if (callback.TryGetTarget(out ourCallback)) { ourCallback.InfoRecieved(info); } } } catch (Exception e) { Console.WriteLine("Error communicating with card: " + e.Message); } } }
protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); var tag = (Tag)intent.GetParcelableExtra(NfcAdapter.ExtraTag); if (tag != null) { var desfire = IsoDep.Get(tag); desfire.Connect(); var readCommand = new byte[] { 0x6C, 0x01 }; var selectCommand = new byte[] { 0x5A, 0x5F, 0x84, 0x15 }; desfire.Transceive(selectCommand); var result = BitConverter.ToInt32(desfire.Transceive(readCommand), 1); desfire.Close(); var alert = new AlertDialog.Builder(this); alert.SetTitle("Guthaben"); alert.SetMessage(String.Format("{0:0.00} €", ((double)result) / 1000.0d)); alert.SetPositiveButton("Ok", new EventHandler <DialogClickEventArgs>((sender, args) => { })); var dialog = alert.Create(); dialog.Show(); } }
/// <summary> /// Callback when a new tag is discovered by the system. /// Communication with the card should take place here. /// </summary> /// <param name="tag">Discovered tag</param> public void OnTagDiscovered(Tag tag) { Log.Info(TAG, "New tag discovered"); var isoDep = IsoDep.Get(tag); if (isoDep != null) { try { // Connect to the remote NFC device isoDep.Connect(); // Build SELECT AID command for our loyalty card service. // This command tells the remote device which service we wish to communicate with. Log.Info(TAG, "Requesting remote AID: " + SAMPLE_LOYALTY_CARD_AID); var command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID); // Send command to remote device Log.Info(TAG, "Sending: " + ByteArrayToHexString(command)); var result = isoDep.Transceive(command); // If AID is successfully selected, 0x9000 is returned as the status word (last 2 // bytes of the result) by convention. Everything before the status word is // optional payload, which is used here to hold the account number. var resultLength = result.Length; byte[] statusWord = { result[resultLength - 2], result[resultLength - 1] }; var payload = new byte[resultLength - 2]; Array.Copy(result, payload, resultLength - 2); var arrayEquals = SELECT_OK_SW.Length == statusWord.Length; for (var i = 0; i < SELECT_OK_SW.Length && i < statusWord.Length && arrayEquals; i++) { arrayEquals = (SELECT_OK_SW[i] == statusWord[i]); if (!arrayEquals) { break; } } if (arrayEquals) { // The remote NFC device will immediately respond with its stored account number var accountNumber = System.Text.Encoding.UTF8.GetString(payload); Log.Info(TAG, "Received: " + accountNumber); // Inform CardReaderFragment of received account number if (messageCallback.TryGetTarget(out var accountCallback)) { accountCallback.OnMessageRecieved(accountNumber); } } } catch (Exception e) { Log.Error(TAG, "Error communicating with card: " + e.Message); } } }
// Faz a leitura do ID do cartão protected void LerCartaoNfc() { mifareClassic = MifareClassic.Get(tag); if (mifareClassic == null) { isoDep = IsoDep.Get(tag); } Contador += 1; txtLeitura.Text = "Leitura: " + Contador.ToString() + "\nId do Cartão: " + idCartao(); }
private void HandleIntent(Intent intent) { if (intent.Action == NfcAdapter.ActionTechDiscovered) { Tag tag = (Tag)intent.GetParcelableExtra(NfcAdapter.ExtraTag); IsoDep card = IsoDep.Get(tag); AndroidSmartCard smartCard = new AndroidSmartCard(card); MessagingCenter.Send(new CardAddedMessage { Card = smartCard }, ""); } }
public void OnTagDiscovered(Tag tag) { IsoDep isoDep = IsoDep.Get(tag); if (isoDep == null) { Console.WriteLine("[CODE] Unsupported Tag"); //navigator.failureNavigation("Unsupported Tag"); } else { Console.WriteLine("[CODE] IsoDep Found"); isoDep.Connect(); byte[] command = BuildSelectApdu(ACCESS_CARD_AID); //Once we find a tag, trancieve the command try { byte[] result = isoDep.Transceive(command); // If the matching AID is successfully found, 0x9000 is returned from the device as the status word (last 2 // bytes of the result) (0x9000 is the OK Status Word - this is returned if the phone is authorised and the scan was successful). Everything before the status word is // payload, in this case will be the user ID - this means result will be dynamic int resultLength = result.Length; byte[] statusWord = { result[resultLength - 2], result[resultLength - 1] }; //Grab last two bytes for the status word byte[] payload = new byte[resultLength - 2]; //Initialise empty array payload Array.Copy(result, payload, resultLength - 2); //We know the lengths, so copy from those indexes into a new array bool arrayEquals = SELECT_OK_SW.Length == statusWord.Length; for (int i = 0; i < SELECT_OK_SW.Length && i < statusWord.Length && arrayEquals; i++) { arrayEquals = (SELECT_OK_SW[i] == statusWord[i]); //Compare byte by byte, ISO-DEP communcation is in bytes - we're looking for 0x90, 0x00 if (!arrayEquals) { break; } } if (arrayEquals) //Only if the status word is OK, unauthenticated devices will not respond with the "OK" status { // The NFC device will respond with the userId as a payload string payloadString = System.Text.Encoding.UTF8.GetString(payload); Console.WriteLine($"Recieved Payload: {payloadString}"); OnPayloadReceived(payloadString); } } catch (TagLostException e) //If something happens or the tag moves away, this is thrown. Close communication and inform user that an error occurred { isoDep.Close(); Console.WriteLine("[CODE] Caught tag loss error"); Device.BeginInvokeOnMainThread(async() => { await Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Error", $"Tag Lost Error", "OK"); }); } } }
protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); Tag tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; SetResult(Result.Canceled, intent); if (tag != null) { IsoDep isoTag = IsoDep.Get(tag); try { isoTag.Connect(); isoTag.Timeout = 30000; byte[] resp = isoTag.Transceive(selectCommand); int length = resp.Length; if (resp[length - 2] == (byte)0x90 && resp[length - 1] == 0x00) { DoChallengeYubiKey(isoTag, slot, challenge); } else { Toast.MakeText(this, Resource.String.yubichallenge_tag_error, ToastLength.Long) .Show(); } isoTag.Close(); } catch (TagLostException e) { Toast.MakeText(this, Resource.String.yubichallenge_lost_tag, ToastLength.Long) .Show(); } catch (IOException e) { Toast.MakeText(this, GetString(Resource.String.yubichallenge_tag_error) + e.Message, ToastLength.Long).Show(); } } else { SetResult(Result.Canceled, intent); } Finish(); }
public void OnTagDiscovered(Tag tag) { //OnTagRemoved();//TODO: fix this hack isoDep = IsoDep.Get(tag); if (isoDep != null) { try { isoDep.Connect(); OnCardPutInField(new EventArgs()); } catch (Exception e) { throw e; } } }
private bool LoadCurrentIsoDept() { if (currentIsoDept != null) { ConnectCurrentIsoDept(); return(true); } if (currentTag == null) { return(false); } currentIsoDept = IsoDep.Get(currentTag); if (currentIsoDept == null) { return(false); } ConnectCurrentIsoDept(); return(true); }
public (TagInfo?, NFCMessage) ReadInfo(Tag tag) { if (tag == null) { return(null, NFCMessage.NFC_NULL_TAG); } return(new TagInfo { Uid = tag.GetId(), Tag = tag, IsoDep = IsoDep.Get(tag), MifareClassic = MifareClassic.Get(tag), MifareUltralight = MifareUltralight.Get(tag), NfcV = NfcV.Get(tag), Ndef = Ndef.Get(tag), NdefFormatable = NdefFormatable.Get(tag), NfcA = NfcA.Get(tag), NfcB = NfcB.Get(tag), NfcF = NfcF.Get(tag), NfcBarcode = NfcBarcode.Get(tag), TechList = tag.GetTechList() }, NFCMessage.NFC_NO_ERROR); }
public async void OnTagDiscovered(Tag tag) { IsoDep isoDep = IsoDep.Get(tag); if (isoDep != null) { try { isoDep.Connect(); var aidLength = (byte)(SAMPLE_LOYALTY_CARD_AID.Length / 2); var aidBytes = StringToByteArray(SAMPLE_LOYALTY_CARD_AID); var command = SELECT_APDU_HEADER .Concat(new byte[] { aidLength }) .Concat(aidBytes) .ToArray(); var result = isoDep.Transceive(command); var resultLength = result.Length; byte[] statusWord = { result[resultLength - 2], result[resultLength - 1] }; var payload = new byte[resultLength - 2]; Array.Copy(result, payload, resultLength - 2); var arrayEquals = SELECT_OK_SW.Length == statusWord.Length; if (Enumerable.SequenceEqual(SELECT_OK_SW, statusWord)) { var msg = Encoding.UTF8.GetString(payload); await App.DisplayAlertAsync(msg); } } catch (Exception e) { await App.DisplayAlertAsync("Error communicating with card: " + e.Message); } } }
protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); Toast.MakeText(this, "Leyendo NFC", ToastLength.Short).Show(); var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; /*if (_inWriteMode) * { * _inWriteMode = false; * * if (tag == null) * { * return; * } * * // These next few lines will create a payload (consisting of a string) * // and a mimetype. NFC record are arrays of bytes. * var payload = Encoding.ASCII.GetBytes("YeeSaurio"); * var mimeBytes = Encoding.ASCII.GetBytes("application/NFCFighters"); * var apeRecord = new NdefRecord(NdefRecord.TnfMimeMedia, mimeBytes, new byte[0], payload); * var ndefMessage = new NdefMessage(new[] { apeRecord }); * * if (!TryAndWriteToTag(tag, ndefMessage)) * { * // Maybe the write couldn't happen because the tag wasn't formatted? * TryAndFormatTagWithMessage(tag, ndefMessage); * } * } * * /*string[] techList = tag.GetTechList(); * info.Text = ""; * foreach (string tech in techList) * { * info.Text += "\n\t" + tech; * }*/ NfcA nfca = NfcA.Get(tag); NdefFormatable ndeff = NdefFormatable.Get(tag); IsoDep iso = IsoDep.Get(tag); MifareUltralight mifU = MifareUltralight.Get(tag); /*try * { * nfca.Connect(); * short s = nfca.Sak; * byte[] a = nfca.GetAtqa(); * string atqa = Encoding.ASCII.GetString(a); * info.Text += "\nSAK = " + s + "\nATQA = " + atqa; * nfca.Close(); * } catch (Exception e) * { * Toast.MakeText(this, "NfcA" + e.Message, ToastLength.Short).Show(); * }*/ /*try * { * iso.Connect(); * * iso.Close(); * } * catch (Exception e) * { * Toast.MakeText(this, "IsoDep" + e.Message, ToastLength.Short).Show(); * }*/ /*try * { * ndeff.Connect(); * //info.Text += "\nType: " + ndeff.GetType(); * //info.Text += "\nConnected: " + ndeff.IsConnected; * var rawMsgs = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages); * var msg = (NdefMessage)rawMsgs[0]; * var record = msg.GetRecords()[0]; * * info.Text += "\n\tNdefFormatted: " + Encoding.ASCII.GetString(record.GetPayload()); * ndeff.Close(); * } * catch (Exception e) * { * Toast.MakeText(this, "NdefFormatted " + e.Message, ToastLength.Short).Show(); * }*/ try { mifU.Connect(); byte[] mPag = mifU.ReadPages(12); StringBuilder aux = new StringBuilder(); String cont_mpag = ""; for (int i = 0; i < mPag.Length; i++) { aux.Append(mPag[i]); } cont_mpag = aux.ToString(); //info.Text = Charset.AvailableCharsets().ToString(); //var mifM = new String(cont_mpag, Charset.ForName("US-ASCII")); string mifM = Encoding.ASCII.GetString(mPag); info.Text += "\nTu personaje es " + mifM; mifU.Close(); } catch (Exception e) { //Toast.MakeText(this, "MifareUltralight" + e.Message, ToastLength.Short).Show(); } }
public async void OnTagDiscovered(Tag tag) { IsoDep isoDep = IsoDep.Get(tag); // NfcA nfcA = NfcA.Get(tag); // //byte[] response = nfcA.Transceive(new byte[] { (byte)0x30, (byte)0x00 }); // nfcA.Connect(); // var aidLength2 = (byte)(SAMPLE_LOYALTY_CARD_AID.Length / 2); // var aidBytes2 = StringToByteArray(SAMPLE_LOYALTY_CARD_AID); // var command2 = SELECT_APDU_HEADER // .Concat(new byte[] { aidLength2 }) // .Concat(aidBytes2) // .ToArray(); // var result2 = nfcA.Transceive(new byte[] { // (byte)0x30, /* CMD = READ */ // (byte)0x10 /* PAGE = 16 */ //}); // string TagUid = ByteArrayToString(result2); // var resultLength2 = result2.Length; // byte[] statusWord2 = { result2[resultLength2 - 2], result2[resultLength2 - 1] }; // var payload2 = new byte[resultLength2 - 2]; // Array.Copy(result2, payload2, resultLength2 - 2); // var arrayEquals2 = SELECT_OK_SW.Length == statusWord2.Length; // var msg2 = Encoding.UTF8.GetString(payload2); // if (Enumerable.SequenceEqual(SELECT_OK_SW, statusWord2)) // { // var msg = Encoding.UTF8.GetString(payload2); // await App.DisplayAlertAsync(msg); // } if (isoDep != null) { try { isoDep.Connect(); var aidLength = (byte)(SAMPLE_LOYALTY_CARD_AID.Length / 2); var aidBytes = StringToByteArray(SAMPLE_LOYALTY_CARD_AID); var command = SELECT_APDU_HEADER .Concat(new byte[] { aidLength }) .Concat(aidBytes) .ToArray(); var result = isoDep.Transceive(command); var resultLength = result.Length; byte[] statusWord = { result[resultLength - 2], result[resultLength - 1] }; var payload = new byte[resultLength - 2]; Array.Copy(result, payload, resultLength - 2); var arrayEquals = SELECT_OK_SW.Length == statusWord.Length; if (Enumerable.SequenceEqual(SELECT_OK_SW, statusWord)) { var msg = Encoding.UTF8.GetString(payload); Imprime_box.Consulta_user(msg, ""); } } catch (Exception e) { Console.WriteLine(e.Message); //await App.DisplayAlertAsync("Error communicating with card: " + e.Message); } } }
// public LoyaltyCardReader (WeakReference<AccountCallback> accountCallback) // { // mAccountCallback = accountCallback; // } /** * Callback when a new tag is discovered by the system. * * <p>Communication with the card should take place here. * * @param tag Discovered tag */ public void OnTagDiscovered(Android.Nfc.Tag tag) { isoDep = IsoDep.Get(tag); if (isoDep != null) { try{ // Connect to the remote NFC device isoDep.Connect(); Log.Info(TAG, "Tag connected"); // Build SELECT AID command for our loyalty card service. // This command tells the remote device which service we wish to communicate with. //byte[] command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID); // Send command to remote device //byte[] result = isoDep.Transceive(command); // If AID is successfully selected, 0x9000 is returned as the status word (last 2 // bytes of the result) by convention. Everything before the status word is // optional payload, which is used here to hold the account number. // int resultLength = result.Length; // byte[] statusWord = {result[resultLength-2], result[resultLength-1]}; // byte[] payload = new byte[resultLength-2]; // Array.Copy(result, payload, resultLength-2); // bool arrayEquals = SELECT_OK_SW.Length == statusWord.Length; // // for (int i = 0; i < SELECT_OK_SW.Length && i < statusWord.Length && arrayEquals; i++) // { // arrayEquals = (SELECT_OK_SW[i] == statusWord[i]); // if (!arrayEquals) // break; // } // if (arrayEquals) { // //Console.WriteLine ("aaa"); // // The remote NFC device will immediately respond with its stored account number // string accountNumber = System.Text.Encoding.UTF8.GetString (payload); // MainActivity.chatService.Write (payload); // //bluetoothChat.conversationArrayAdapter.Add ("Account: " + accountNumber); // // Inform CardReaderFragment of received account number // AccountCallback accountCallback; // if(mAccountCallback.TryGetTarget(out accountCallback)) // { // accountCallback.OnAccountRecieved(accountNumber); // } // } // else { //command = HexStringToByteArray("00A40000023F00"); // Send command to remote device /*Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); * result = isoDep.Transceive(command); * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result)); * * * string strCommand = StringToHexString(PPSE_CARD_AID); * command = BuildSelectApdu(strCommand); * // Send command to remote device * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); * result = isoDep.Transceive(command); * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result)); * * command = HexStringToByteArray("00B2010C00"); * // Send command to remote device * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); * result = isoDep.Transceive(command); * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result)); * //}*/ // command = BuildSelectApdu(UPAY2_CARD_AID); // // Send command to remote device // Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); // result = isoDep.Transceive(command); // Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result)); //MainActivity.conversationArrayAdapter.Add ("command: " + command.ToString()); /*command = HexStringToByteArray("00B2011400"); * // Send command to remote device * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); * result = isoDep.Transceive(command); * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result)); * * command = BuildSelectApdu(UPAY1_CARD_AID); * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); * result = isoDep.Transceive(command); * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result)); * * command = BuildSelectApdu(SPTC_CARD_AID); * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); * result = isoDep.Transceive(command); * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result)); * * command = HexStringToByteArray("00B2011400"); * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); * result = isoDep.Transceive(command); * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));*/ /*command = BuildSelectApdu("A000000333010106"); * Console.WriteLine("Sending PBOC: " + ByteArrayToHexString(command)); * result = isoDep.Transceive(command); * Console.WriteLine("PBOC returns: " + ByteArrayToHexString(result));*/ //MainActivity.chatService.Write (Encoding.UTF8.GetBytes(ByteArrayToHexString(result))); /*string accountNumber = System.Text.Encoding.UTF8.GetString (result); * AccountCallback accountCallback; * if(mAccountCallback.TryGetTarget(out accountCallback)) * { * accountCallback.OnAccountRecieved(accountNumber); * }*/ // } } catch (Exception e) { } } }