Exemple #1
0
        private async void buttonUnpair_Click(object sender, EventArgs e)
        {
            if (listViewDevices.SelectedItems.Count == 0)
            {
                return;
            }

            DnaBluetoothLEDevice device = (DnaBluetoothLEDevice)listViewDevices.SelectedItems[0].Tag;

            log("Unpairing " + device);
            var dev = await BluetoothLEDevice.FromIdAsync(device.DeviceId).AsTask();

            if (buttonStop.Enabled == true && ((greenBLE != null && dev.DeviceId == greenBLE.DeviceId) || (purpleBLE != null && dev.DeviceId == purpleBLE.DeviceId)))
            {
                buttonStop.PerformClick();
            }

            var result = await dev.DeviceInformation.Pairing.UnpairAsync();

            if (result == null || result.Status == DeviceUnpairingResultStatus.Unpaired)
            {
                log("Unpairing successful with" + device);
            }
            else
            {
                log("Unpairing unsuccessful with" + device, Color.Red);
            }

            listViewDevices.Invoke(new Action(() =>
            {
                if (listViewDevices.SelectedItems[0].BackColor == greenColor)
                {
                    buttonGreen.BackColor = default(Color);
                    buttonGreen.Text      = String.Empty;
                    greenBLE = null;
                }
                else if (listViewDevices.SelectedItems[0].BackColor == purpleColor)
                {
                    buttonPurple.BackColor = default(Color);
                    buttonPurple.Text      = String.Empty;
                    purpleBLE = null;
                }

                listViewDevices.SelectedItems[0].BackColor = SystemColors.Window;
                listViewDevices.SelectedIndices.Clear();
            }));
        }
Exemple #2
0
        private void buttonPair_Click(object sender, EventArgs e)
        {
            if (listViewDevices.SelectedItems.Count == 0)
            {
                return;
            }

            DnaBluetoothLEDevice      device = (DnaBluetoothLEDevice)listViewDevices.SelectedItems[0].Tag;
            DevicePairingResultStatus result = DevicePairingResultStatus.Failed;

            Task.Run(async() =>
            {
                try
                {
                    // Try and connect
                    log("Pairing with " + device);
                    result = await watcher.PairToDeviceAsync(device.DeviceId);
                    if (result == DevicePairingResultStatus.Paired)
                    {
                        log("Pairing successful with " + device);
                        findDeviceTypeAsync(device);
                    }
                    else
                    {
                        log("Pairing failed: " + result, Color.Red);
                    }
                }
                catch (Exception ex)
                {
                    // Log it out
                    log("Failed to pair with " + device);
                    Console.WriteLine("Failed to pair to Contour device.");
                    Console.WriteLine(ex);
                }
            });
        }
Exemple #3
0
        private async void findDeviceTypeAsync(DnaBluetoothLEDevice device)
        {
            var dev = await BluetoothLEDevice.FromIdAsync(device.DeviceId).AsTask();

            try
            {
                var services = await dev.GetGattServicesAsync();

                foreach (var service in services.Services)
                {
                    if (service.Uuid.ToString().Equals(GattService.FloraSensorService))
                    {
                        var characteristics = await service.GetCharacteristicsAsync();

                        foreach (var curCharacteristic in characteristics.Characteristics)
                        {
                            if (curCharacteristic.Uuid.ToString().Equals(GattService.FloraAllrCharacteristics))
                            {
                                device.caracteristic = curCharacteristic;
                                var result = await curCharacteristic.ReadValueAsync(BluetoothCacheMode.Uncached);

                                if (result.Status == GattCommunicationStatus.Success)
                                {
                                    byte[] input = new byte[18];
                                    DataReader.FromBuffer(result.Value).ReadBytes(input);
                                    Console.WriteLine(BitConverter.ToString(input));
                                    if (BitConverter.ToInt16(input, 0) != 0 && BitConverter.ToInt16(input, 12) != 0 && greenBLE == null) //isGreen
                                    {
                                        buttonGreen.Invoke(new Action(() =>
                                        {
                                            buttonGreen.BackColor = greenColor;
                                            buttonGreen.Text      = device.Name;
                                        }));
                                        listViewDevices.Invoke(new Action(() =>
                                        {
                                            for (int i = 0; i < listViewDevices.Items.Count; i++)
                                            {
                                                if (((DnaBluetoothLEDevice)listViewDevices.Items[i].Tag).DeviceId == device.DeviceId)
                                                {
                                                    listViewDevices.Items[i].BackColor = greenColor;
                                                    listViewDevices.SelectedIndices.Clear();
                                                }
                                            }
                                        }));

                                        greenBLE = device;
                                    }
                                    else if (BitConverter.ToInt16(input, 12) == 0 && BitConverter.ToInt16(input, 0) != 0 && purpleBLE == null) //isPurple
                                    {
                                        buttonPurple.Invoke(new Action(() =>
                                        {
                                            buttonPurple.BackColor = purpleColor;
                                            buttonPurple.Text      = device.Name;
                                        }));
                                        listViewDevices.Invoke(new Action(() =>
                                        {
                                            for (int i = 0; i < listViewDevices.Items.Count; i++)
                                            {
                                                if (((DnaBluetoothLEDevice)listViewDevices.Items[i].Tag).DeviceId == device.DeviceId)
                                                {
                                                    listViewDevices.Items[i].BackColor = purpleColor;
                                                    listViewDevices.SelectedIndices.Clear();
                                                }
                                            }
                                        }));

                                        purpleBLE = device;
                                    }
                                }
                            }
                        }
                    }
                }

                if (greenBLE != null && purpleBLE != null)
                {
                    buttonStart.Invoke(new Action(() =>
                    {
                        buttonStart.Enabled = true;
                    }));
                    buttonStop.Invoke(new Action(() =>
                    {
                        buttonStop.Enabled = false;
                    }));
                }
            }
            catch (Exception ex)
            {
                log(ex.Message, Color.Red);
            }
            finally
            {
                listViewDevices.Invoke(new Action(() =>
                {
                    for (int i = 0; i < listViewDevices.Items.Count; i++)
                    {
                        if (((DnaBluetoothLEDevice)listViewDevices.Items[i].Tag).DeviceId == device.DeviceId && listViewDevices.Items[i].BackColor == SystemColors.Window)
                        {
                            listViewDevices.Items[i].BackColor = Color.Cyan;
                            listViewDevices.SelectedIndices.Clear();
                        }
                    }
                }));
            }
        }