private void OnServiceSelected(Object sender, SelectedItemChangedEventArgs e)
        {
            if (e.SelectedItem != null)
            {
                m_bleServiceSelected            = ((BleGattServiceViewModel)e.SelectedItem);
                ((ListView)sender).SelectedItem = null;

                bleScanViewModel.StopScan();



                // String json = JsonConvert.SerializeObject(m_bleServiceSelected);

                //  String value = "\\";

                //  String tempjson = json.Replace(value, "");


                //  BleGattServiceViewModel account = JsonConvert.DeserializeObject<BleGattServiceViewModel>(tempjson);

                // BleGattServiceViewModel account = new BleGattServiceViewModel("2cf42000-7992-4d24-b05d-1effd0381208");


                //  Application.Current.MainPage.Navigation.PushAsync(new BleGattServicePage(m_bleServiceSelected));
            }
        }
        // Event for Menu Item selection, here we are going to handle navigation based
        // on user selection in menu ListView
        private async Task OnMenuItemSelectedAsync(object sender, SelectedItemChangedEventArgs e)
        {
            // if (Settings.IsConnectedBLE)
            //  {
            var    item = (PageItem)e.SelectedItem;
            String page = item.TargetType;

            switch (page)
            {
            case "ReadMTU":



                await Task.Run(async() =>
                {
                    await Task.Delay(1000); Device.BeginInvokeOnMainThread(() =>
                    {
                        Guid value = new Guid("2cf42000-7992-4d24-b05d-1effd0381208");
                        BleGattServiceViewModel account = new BleGattServiceViewModel(value, savedServer.returnConnect(), dialogsSaved);
                        Application.Current.MainPage.Navigation.PushAsync(new BleGattServicePage(account, savedServer.returnConnect(), dialogsSaved));
                        //    ReadMtuMethod(account, savedServer, dialogsSaved);


                        background_scan_page.Opacity        = 1;
                        background_scan_page_detail.Opacity = 1;
                        ContentNav.Opacity = 0;


                        ContentNav.IsVisible = false;


                        navigationDrawerList.SelectedItem = null;
                        navigationDrawerList.BeginRefresh();
                        navigationDrawerList.SelectedItem = null;
                        navigationDrawerList.EndRefresh();
                    });
                });

                //  Application.Current.MainPage.Navigation.PushAsync(new BleGattServicePage(account, bleGattServerViewModel.returnConnect(), dialogsSaved));

                break;
            }


            background_scan_page.Opacity = 1;

            ContentNav.Opacity = 0;


            ContentNav.IsVisible = false;



            // }
        }
 public BleGattServicePage(BleGattServiceViewModel model)
 {
     InitializeComponent();
     BindingContext = model;
 }
        public BleGattServicePage(BleGattServiceViewModel model, IBleGattServerConnection gattServer, IUserDialogs dialogs)
        {
            InitializeComponent();
            BindingContext = model;

            model_saved = model;

            cargarMTU();


            NavigationPage.SetHasNavigationBar(this, false); //Turn off the Navigation bar

            back_button.Tapped += returntomain;



            Task.Run(async() =>
            {
                await Task.Delay(2000); Device.BeginInvokeOnMainThread(() =>
                {
                    model_saved.Characteristic.RemoveAt(0);



                    Guid ServicioIndicate      = new Guid("2cf42000-7992-4d24-b05d-1effd0381208");
                    Guid CaracterisicoIndicate = new Guid("00000003-0000-1000-8000-00805f9b34fb");

                    BleGattCharacteristicViewModel gattCharacteristicViewModelIndicate = new BleGattCharacteristicViewModel(ServicioIndicate, CaracterisicoIndicate, gattServer, dialogs);

                    var viewModelIndicate = (BleGattCharacteristicViewModel)gattCharacteristicViewModelIndicate;

                    if (viewModelIndicate.EnableNotificationsCommand.CanExecute(null))
                    {
                        viewModelIndicate.EnableNotificationsCommand.Execute(null);
                    }



                    model_saved.Characteristic.Add(gattCharacteristicViewModelIndicate);

                    BindingContext = model_saved;

                    lista.BeginRefresh();

                    lista.EndRefresh();



                    Task.Run(async() =>
                    {
                        await Task.Delay(1000); Device.BeginInvokeOnMainThread(() =>
                        {
                            model_saved.Characteristic.RemoveAt(0);

                            Guid ServicioWrite      = new Guid("2cf42000-7992-4d24-b05d-1effd0381208");
                            Guid CaracterisicoWrite = new Guid("00000002-0000-1000-8000-00805f9b34fb");
                            // String valorDato = "000005258000015a";

                            BleGattCharacteristicViewModel gattCharacteristicViewModelWrite = new BleGattCharacteristicViewModel(ServicioWrite, CaracterisicoWrite, gattServer, dialogs);

                            var viewModelWrite = (BleGattCharacteristicViewModel)gattCharacteristicViewModelWrite;
                            if (viewModelWrite.WriteCurrentBytesGUIDCommand.CanExecute(null))
                            {
                                viewModelWrite.WriteCurrentBytesGUIDCommand.Execute(null);
                            }



                            model_saved.Characteristic.Add(gattCharacteristicViewModelWrite);

                            BindingContext = model_saved;

                            lista.BeginRefresh();

                            lista.EndRefresh();


                            Task.Run(async() =>
                            {
                                await Task.Delay(1000); Device.BeginInvokeOnMainThread(() =>
                                {
                                    BleGattCharacteristicViewModel[] array = new BleGattCharacteristicViewModel[10];
                                    model_saved.Characteristic.CopyTo(array, 0);



                                    Byte[] value1 = array[0].ValueAsHexBytes;

                                    int longitud = value1.Length;

                                    int contador = 0;


                                    byte[] listTotal    = new byte[1024];
                                    int listTotalLength = 0;
                                    int cuantoDato      = 0;

                                    while (contador < longitud)
                                    {
                                        byte[] array2 = new byte[20];

                                        Array.Copy(value1, contador, array2, 0, 20);

                                        cuantoDato = array2[2];
                                        if (cuantoDato > 0)
                                        {
                                            Array.Copy(array2, 3, listTotal, listTotalLength, cuantoDato);
                                            listTotalLength += cuantoDato;
                                        }

                                        contador = contador + 20;
                                    }


                                    //Identificador
                                    byte[] identificador = new byte[4];
                                    Array.Copy(listTotal, 6 + 5, identificador, 0, 4);

                                    long identificador_valor = (long)(identificador[3] * Math.Pow(2, 24)
                                                                      + identificador[2] * Math.Pow(2, 16)
                                                                      + identificador[1] * Math.Pow(2, 8)
                                                                      + identificador[0] * Math.Pow(2, 0));


                                    //oneWayTx
                                    byte[] oneWayTx = new byte[4];
                                    Array.Copy(listTotal, 10 + 5, oneWayTx, 0, 4);


                                    long oneWayTx_valor = (long)(oneWayTx[3] * Math.Pow(2, 24)
                                                                 + oneWayTx[2] * Math.Pow(2, 16)
                                                                 + oneWayTx[1] * Math.Pow(2, 8)
                                                                 + oneWayTx[0] * Math.Pow(2, 0));


                                    //TwoWayTx
                                    byte[] TwoWayTx = new byte[4];
                                    Array.Copy(listTotal, 14 + 5, TwoWayTx, 0, 4);


                                    long TwoWayTx_valor = (long)(TwoWayTx[3] * Math.Pow(2, 24)
                                                                 + TwoWayTx[2] * Math.Pow(2, 16)
                                                                 + TwoWayTx[1] * Math.Pow(2, 8)
                                                                 + TwoWayTx[0] * Math.Pow(2, 0));

                                    //TwoWayRx
                                    byte[] TwoWayRx = new byte[4];
                                    Array.Copy(listTotal, 18 + 5, TwoWayRx, 0, 4);


                                    long TwoWayRx_valor = (long)(TwoWayRx[3] * Math.Pow(2, 24)
                                                                 + TwoWayRx[2] * Math.Pow(2, 16)
                                                                 + TwoWayRx[1] * Math.Pow(2, 8)
                                                                 + TwoWayRx[0] * Math.Pow(2, 0));

                                    String listatotla = listTotal.EncodeToBase16String();
                                    valorHEX.Text     = listatotla.Substring(0, listTotalLength * 2);

                                    cargarValoresMTU(identificador_valor.ToString(), oneWayTx_valor.ToString(), TwoWayTx_valor.ToString(), TwoWayRx_valor.ToString());
                                });
                            });
                        });
                    });
                });
            });
        }