Esempio n. 1
0
 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);
 }
Esempio n. 2
0
        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();
            }
        }
Esempio n. 3
0
        internal void CheckForNfcMessage(Intent intent)
        {
            if (!Enabled)
            {
                return;
            }

            if (intent.Action != NfcAdapter.ActionTechDiscovered)
            {
                return;
            }

            if (!(intent.GetParcelableExtra(NfcAdapter.ExtraTag) is Tag tag))
            {
                return;
            }

            try
            {
                var ev1 = MifareUltralight.Get(tag);

                //TagDetected?.Invoke(tag);

                ev1.Connect();
                byte[] FirstTag = MainTabViewModel.Current?.DataBag.GetData();
                byte[] mem      = new byte[80];

                for (int i = 0; i < 20; i += 4)
                {
                    byte[] payload = ev1.ReadPages(i);
                    Buffer.BlockCopy(payload, 0, mem, 4 * i, 16);
                }

                if (WriteMode)
                {
                    byte[] dstData = MainTabViewModel.MergeTagData(FirstTag, mem);


                    // password auth
                    // var response = ev1.Transceive(new byte[]{
                    //            (byte) 0x1B, // PWD_AUTH
                    //            0,0,0,0 });

                    // Check if PACK is matching expected PACK
                    // This is a (not that) secure method to check if tag is genuine
                    //if ((response != null) && (response.Length >= 2))
                    //{
                    //}

                    for (int i = 4; i < 16; i++)
                    {
                        ev1.WritePage(i, dstData.Skip(4 * i).Take(4).ToArray());
                    }

                    MainTabViewModel.Current?.DataBag.SetData(dstData);
                    WriteMode = false;
                }
                else
                {
                    MainTabViewModel.Current?.SetControlsVisibility(mem[41]);
                    MainTabViewModel.Current?.DataBag.SetData(mem);
                }
                ev1.Close();
                MainTabViewModel.Current.cbNFCRead  = false;
                MainTabViewModel.Current.cbNFCWrite = false;

                try
                {
                    // Use default vibration length
                    Vibration.Vibrate();
                }
                catch (FeatureNotSupportedException ex)
                {
                    // Feature not supported on device
                }
                catch (Exception ex)
                {
                    // Other error has occurred.
                }
            }
            catch (Exception e)
            {
                try
                {
                    Vibration.Vibrate();
                    Thread.Sleep(1000);
                    Vibration.Vibrate();
                }
                catch (Exception ex)
                {
                    // Other error has occurred.
                }
            }
        }