Ejemplo n.º 1
0
        private async void ConnectGenericDevice()
        {
            if (ChosenDevice != null)
            {
                await CheckBluetoothStatus(true);     // check bluetooth status and activate it if is turned off

                if (!IsBluetoothEnabled)
                {
                    AddLog("Can not connect with bluetooth disabled. Turn on the Bluetooth and try again.", AppLog.LogCategory.Warning);
                    return;
                }

                // request access to the selected device
                BluetoothDevice = await BluetoothLEDevice.FromIdAsync(ChosenDevice.Id);

                DeviceAccessStatus accessStatus = await BluetoothDevice.RequestAccessAsync();

                // log
                AddLog("[Connection: " + accessStatus.ToString() + "]" + " Connecting to " + BluetoothDevice.Name + "...", AppLog.LogCategory.Debug);
                AddLog("Connecting to " + BluetoothDevice.Name + "...", AppLog.LogCategory.Info);

                GattCharacteristicsResult hrGattCHaracteristics = null;

                // try to read the device charateristics
                try {
                    var gattDeviceServicesResult = await BluetoothDevice.GetGattServicesForUuidAsync(HRserviceGuid);        // get services with the HR service GUID

                    // for each service withe the given GUID try to read get
                    foreach (GattDeviceService service in gattDeviceServicesResult.Services)
                    {
                        AddLog("[" + ChosenDevice.Name + "] Found service. " +
                               "\n - Handle: " + service.AttributeHandle.ToString() +
                               "\n - UUID: " + service.Uuid.ToString(), AppLog.LogCategory.Debug);    // log

                        if (await service.GetCharacteristicsForUuidAsync(HRCharacteristicGuid) != null)
                        {
                            hrGattCHaracteristics = await service.GetCharacteristicsForUuidAsync(HRCharacteristicGuid);

                            HRReaderService = service;
                            break;
                        }
                    }
                } catch {
                    AddLog("Device \"" + ChosenDevice.Name + "\" does not support HR service." +
                           "\nSelect another one from the devices list.", AppLog.LogCategory.Warning);

                    return;
                }

                // get the HR reader characteristic
                if (hrGattCHaracteristics != null)
                {
                    foreach (GattCharacteristic characteristic in hrGattCHaracteristics.Characteristics.Where(c => c.Uuid.Equals(HRCharacteristicGuid)))
                    {
                        HRReaderCharacteristic = characteristic;
                    }
                }
                else
                {
                    // log something
                    return;
                }

                // if HR characteristic can't be found, show an error and return
                if (HRReaderCharacteristic == null)
                {
                    AddLog("Heart rate monitor characteristic NOT found.", AppLog.LogCategory.Debug);
                    AddLog("Could not connect to Heart Rate service of the device \"" + ChosenDevice.Name + "\".", AppLog.LogCategory.Warning);
                    return;
                }
                // if HR characteristic have been found, then start reading process
                else
                {
                    AddLog("Heart rate monitor characteristic found [Handle: " +
                           HRReaderCharacteristic.AttributeHandle.ToString() + "]", AppLog.LogCategory.Debug);

                    BeginBluetoothReadProcess();

                    SwitchConnectButtonToDisconnectButton();
                }
            }
            else
            {
                AddLog("The button should be disabled. Kowalski analisis.", AppLog.LogCategory.Debug);
                return;
            }
        }