Esempio n. 1
0
 public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
 {
     int user_id = 0;            
     try
     {                                
         if (messages != null && messages.Length > 0)
         {
             var first = messages[0];
             string messa = GetRecords(first.Records);
             string[] variables = messa.Split('=');
             string[] depura_userid = variables[1].Split('&');
             string tag_id = variables[2];
             user_id = Convert.ToInt32(depura_userid[0]);
             //if (write_tag.modo_escritura == false) { Imprime_box.Consulta_user(user_id.ToString(), tag_id); }                    
             Imprime_box.Consulta_user(user_id.ToString(), tag_id);
         }                
     }
     catch (Exception e) 
     {
         Console.WriteLine(e);
     }            
      
     //user_id_tag = user_id.ToString();
     //if (write_tag.modo_escritura == true)
     //{
     //    session.InvalidateSession();
     //    session.Dispose();
     //    Thread.Sleep(7000);
     //    write_tag.modo_escritura = false;
     //    write_tag myobject = new write_tag();
     //    myobject.ScanWriteAsync();
     //}
     session.InvalidateSession();           
     Session.InvalidateSession();
 }        
        public async Task <Box> GetBoxDefault(int id)
        {
            if (id == null || id == 0)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.UserNotFound,
                    Languages.Accept);

                return(null);
            }
            Box = new Box();
            var connection = await this.apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    connection.Message,
                    Languages.Accept);

                return(null);
            }

            var apiSecurity  = Application.Current.Resources["APISecurity"].ToString();
            var UserFornaneo = await this.apiService.GetUserId(
                apiSecurity,
                "/api",
                "/Users/",
                id);

            if (UserFornaneo == null)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.UserNotFound,
                    Languages.Accept);

                return(null);
            }
            var BoxL = await this.apiService.GetBoxDefault <Box>(
                apiSecurity,
                "/api",
                "/Boxes/GetBoxDefault",
                id);

            if (BoxL == null)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.UserWithoutBoxes,
                    Languages.Accept);

                return(null);
            }

            int UserIdToSend = UserFornaneo.UserId;

            //Perfil predeterminado, que es el perfil de Mynfo y box predeterminada de Ese perfil
            if (UserFornaneo.Share != true)
            {
                UserIdToSend = 1;
                BoxL         = await apiService.GetBoxDefault <Box>(
                    apiSecurity,
                    "/api",
                    "/Boxes/GetBoxDefault",
                    UserIdToSend);

                if (BoxL == null)
                {
                    await Application.Current.MainPage.DisplayAlert(
                        Languages.Error,
                        Languages.UserWithoutBoxes,
                        Languages.Accept);

                    return(null);
                }
            }

            Imprime_box.InsertForeignData(UserIdToSend, BoxL.BoxId);
            return(Box);
        }
Esempio n. 3
0
        protected override void OnResume()
        {
            base.OnResume();
            try
            {
                if (TAGPage.write_nfc == true)
                {
                    //PopupNavigation.Instance.PopAsync();
                    PopupNavigation.Instance.PushAsync(new ConfigStikerPage());
                    int user_id = 0;
                    if (NfcAdapter.ActionNdefDiscovered.Equals(Intent.Action))
                    {
                        //Get the NFC ID
                        var myTag = Intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);

                        var msg    = (NdefMessage)myTag[0];
                        var record = msg.GetRecords()[0];
                        //If the NFC Card ID is not null
                        if (record != null)
                        {
                            if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/
                            {
                                // Get the transfered data
                                var    data   = Encoding.ASCII.GetString(record.GetPayload());
                                string result = data.Substring(1);

                                string[] variables     = result.Split('=');
                                string[] depura_userid = variables[1].Split('&');
                                user_id = Convert.ToInt32(depura_userid[0]);
                            }
                        }
                    }

                    string dominio = "boxweb.azurewebsites.net/";
                    string user    = MainViewModel.GetInstance().User.UserId.ToString();
                    string tag_id  = "";
                    if (user_id == Convert.ToInt32(user) || 0 == user_id)
                    {
                        string url = dominio + "index3.aspx?user_id=" + user + "&tag_id=" + tag_id;
                        //http://localhost:58951/index.aspx?user_id=7
                        var tag = Intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
                        if (tag != null)
                        {
                            Ndef ndef = Ndef.Get(tag);
                            if (ndef != null && ndef.IsWritable)
                            {
                                /*var payload = Encoding.ASCII.GetBytes(url);
                                 * var mimeBytes = Encoding.ASCII.GetBytes("text/html");
                                 * var record = new NdefRecord(NdefRecord.TnfWellKnown, mimeBytes, new byte[0], payload);
                                 * var ndefMessage = new NdefMessage(new[] { record });
                                 * ndef.Connect();
                                 * ndef.WriteNdefMessage(ndefMessage);
                                 * ndef.Close();*/

                                ndef.Connect();
                                NdefRecord mimeRecord = NdefRecord.CreateUri("http://" + url);
                                ndef.WriteNdefMessage(new NdefMessage(mimeRecord));
                                ndef.Close();
                            }
                        }
                        TAGPage.write_nfc = false;

                        var duration = TimeSpan.FromMilliseconds(2000);
                        Vibration.Vibrate(duration);
                    }
                    else
                    {
                        System.Threading.Tasks.Task task = App.DisplayAlertAsync("¡Este Tag esta vinculado con otro usuario!");
                    }
                    PopupNavigation.Instance.PopAsync();
                    PopupNavigation.Instance.PushAsync(new Stickerconfig());
                    Thread.Sleep(4000);
                    PopupNavigation.Instance.PopAsync();
                }
                else
                {
                    if (NfcAdapter.ActionNdefDiscovered.Equals(Intent.Action))
                    {
                        //Get the NFC ID
                        var myTag = Intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);

                        var msg    = (NdefMessage)myTag[0];
                        var record = msg.GetRecords()[0];
                        //If the NFC Card ID is not null
                        if (record != null)
                        {
                            if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/
                            {
                                // Get the transfered data
                                var    data   = Encoding.ASCII.GetString(record.GetPayload());
                                string result = data.Substring(1);

                                string[] variables     = result.Split('=');
                                string[] depura_userid = variables[1].Split('&');
                                string   tag_id        = variables[2];
                                string   user_id       = depura_userid[0];

                                Imprime_box.Consulta_user(user_id, tag_id);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }