/// <summary>
        /// This function is called when any discovered device
        /// is tapped from devicelistview. First it creates a StackLayout
        /// with UuidListView and GattLabel on a ContentPage.
        /// Then creates a new NavigationPage using the ContentPage,
        /// because NaviagtionPage manages navigation and user-experience
        /// of a stack of other pages. And then it pushes this NaviagationPage
        /// on top of MainPage. Once the page is rendered it registers for the
        /// GattConnectionStateChanged event handler. Then it issues
        /// GATT connection to the tapped device. It waits until GATT connection
        /// is successfull, then it fetches all the service uuids it supports
        /// and renders it in the new page. Finally it does GATT disconnect and
        /// all postprocessing.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">ItemTappedEventArgs</param>
        public async void DeviceTapped(object sender, ItemTappedEventArgs e)
        {
            if (e.Item != null)
            {
                Device device = e.Item as Device;

                if (UuidListView == null)
                {
                    CreateUUIDListView();
                }

                ContentPage content = new ContentPage
                {
                    Title   = "UUID List",
                    Content = new StackLayout
                    {
                        VerticalOptions = LayoutOptions.Center,
                        Children        =
                        {
                            GattLabel,
                            UuidListView,
                        }
                    },
                };

                GattLabel.Text           = "GattConnect initiated";
                GattLabel.FontAttributes = FontAttributes.Bold;
                GattLabel.FontSize       = 24;

                UuidPage = new NavigationPage(content);
                await MainPage.Navigation.PushAsync(UuidPage);

                GattClient = BluetoothGattClient.CreateClient(device.Address);
                await GattClient.ConnectAsync(false);

                GattClient.ConnectionStateChanged += Device_GattConnectionStateChanged;

                await WaitStateChangedFlag();

                GattLabel.Text = "GattConnected";
                IEnumerable <BluetoothGattService> srv_list;
                srv_list = GattClient.GetServices();

                foreach (BluetoothGattService srv in srv_list)
                {
                    UuidList.Add(srv.Uuid);
                }

                UuidListView.ItemsSource = null;
                UuidListView.ItemsSource = UuidList;
                GattClientExit(device.Ledevice);
            }
        }
Example #2
0
        ///<summary>
        /// Called after tapping device from DeviceListView.
        /// Creates new CirclePage with list of tapped device
        /// service UUIDs.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event arguments</param>
        public async void DeviceTapped(object sender, ItemTappedEventArgs e)
        {
            if (e.Item != null)
            {
                Device device = e.Item as Device;

                if (UuidListView == null)
                {
                    CreateUUIDListView();
                }
                UuidList.Clear();

                CirclePage content = new CirclePage
                {
                    RotaryFocusObject = UuidListView,
                    Content           = new StackLayout
                    {
                        Padding = new Thickness(0, 30),

                        Children =
                        {
                            GattLabel,
                            UuidListView,
                        }
                    },
                };
                NavigationPage.SetHasNavigationBar(content, false);

                GattLabel.Text = "GattConnect\n" +
                                 "initiated";
                GattLabel.HorizontalTextAlignment = TextAlignment.Center;

                UuidPage = content;
                await MainPage.Navigation.PushAsync(UuidPage);

                device.Ledevice.GattConnectionStateChanged += Device_GattConnectionStateChanged;

                GattClient = device.Ledevice.GattConnect(false);
                await WaitStateChangedFlag();

                GattLabel.Text = "GattConnected";

                foreach (BluetoothGattService srv in GattClient.GetServices())
                {
                    UuidList.Add(srv.Uuid);
                }

                UuidListView.ItemsSource = null; // Refreshing does not work without it
                UuidListView.ItemsSource = UuidList;
                GattClientExit(device.Ledevice);
            }
        }
        async void WaiterReaderConnecter()
        {
            await WaitFlag();

            //WaiterWatcher();
            try
            {
                foreach (BluetoothGattService serv in client.GetServices())
                {
                    /*if(serv.Uuid != 0x1809)
                     * {
                     *
                     * }*/
                    services.Add(serv);
                    if (/*serv.Uuid.StartsWith(Variables.serviceuuid_start)*/ true)
                    {
                        foreach (BluetoothGattCharacteristic chara in serv.GetCharacteristics())
                        {
                            Tizen.Log.Info("COREUUID", "COREUUID=" + chara.Uuid);
                            if (chara.Uuid.ToLower().StartsWith(Variables.characteristicuuid_start))
                            {
                                DataLBL.FontSize             = 10;
                                DataLBL.Text                 = "Waiting for response";
                                characteristic               = chara;
                                characteristic.ValueChanged += Characteristic_ValueChanged;


                                //DisplayAlert("OK", "OK", "OK");
                            }
                            GC.Collect();
                        }
                    }


                    // DataLBL.Text = serv.Uuid;
                }
            }
            catch { if (characteristic != null)
                    {
                        DisplayAlert("Error", "Error", "Error");
                    }
                    else
                    {
                        DisplayAlert("Error", "Is Null ?", "Retry");
                    } WaiterReaderConnecter(); }
            //DataLBL.Text = "Done";
        }