Exemplo n.º 1
0
        private async void HandleWiteTag(INFCMiFareTag nMifareTag)
        {
            byte[] data = await Read4Pages(nMifareTag, 0);

            // write
            if (data != null)
            {
                byte[] FirstTag = MainTabViewModel.Current?.DataBag.GetData();
                byte[] mem      = new byte[80];
                data.CopyTo(mem, 0);
                byte[] dstData = MainTabViewModel.MergeTagData(FirstTag, mem);

                bool result = true;
                for (byte i = 4; result && (i < 16); i++)
                {
                    result = await WritePage(nMifareTag, i, dstData.Skip(4 * i).Take(4).ToArray());
                }

                if (result)
                {
                    MainTabViewModel.Current?.DataBag.SetData(dstData);
                }
            }

            MainTabViewModel.Current.cbNFCRead  = false;
            MainTabViewModel.Current.cbNFCWrite = false;
        }
Exemplo n.º 2
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.
                }
            }
        }