Example #1
0
        //private bool isBusy = false;
        private async void btnDevConnect_ClickAsync(object sender, EventArgs e)
        {
            string strfoundId = "";
            string sDev       = "";

            btnDevConnect.Enabled = false;


            try
            {
                strfoundId = textIDDev.Text;
                if (strfoundId == "")
                {
                    btnDevConnect.Enabled = true;
                    return;
                }

                btnDevConnect.BackColor = Color.GreenYellow;

                bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(strfoundId);//.AsTask().TimeoutAfter(_timeout);

                if (bluetoothLeDevice == null)
                {
                    sDev = "Failed to connect to device.";
                    printToLog(sDev);
                }
            }
            catch (Exception ex)
            {
                sDev = "Bluetooth radio is not on";
                printToLog(sDev);
            }

            if (bluetoothLeDevice != null)
            {
                // Note: BluetoothLEDevice.GattServices property will return an empty list for unpaired devices. For all uses we recommend using the GetGattServicesAsync method.
                // BT_Code: GetGattServicesAsync returns a list of all the supported services of the device (even if it's not paired to the system).
                // If the services supported by the device are expected to change during BT usage, subscribe to the GattServicesChanged event.
                //await
                GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);

                if (result.Status == GattCommunicationStatus.Success)
                {
                    var services = result.Services;

                    sDev = "Найдено " + services.Count.ToString() + "сервис(а).";
                    printToLog(sDev);

                    int n = 0;

                    btnDevConnect.BackColor = Color.GreenYellow;

                    ServiceList.BeginUpdate();
                    ServiceList.Items.Clear();

                    foreach (var service in services)
                    {
                        ServiceCollection.Add(new BluetoothLEAttributeDisplay(service));

                        ServiceList.Items.Add(ServiceCollection[n++].Name);
                    }

                    ServiceList.Text = "Выбрать сервис:";
                    //ServiceList.SelectedIndex = 0;
                    ServiceList.EndUpdate();
                }
                else
                {
                    //rootPage.NotifyUser("Device unreachable", NotifyType.ErrorMessage);
                }
            }

            //           _exitCode = OpenDevice(strDevName);

            btnDevConnect.Enabled = true;
        }