Example #1
0
        // ping the device to verify if is still reachable
        private async void PingDevice()
        {
            GattCommunicationStatus status = GattCommunicationStatus.ProtocolError;

            // repeat the notify request
            if (IsMiBand2)
            {
                status = await miBand.HeartRate.SetRealtimeHeartRateMeasurement(MiBand2SDK.Enums.RealtimeHeartRateMeasurements.ENABLE);
            }
            else
            {
                status = await HRReaderCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
            }

            if (status == GattCommunicationStatus.Success)
            {
                if (DeviceConnected == false)
                {
                    DeviceConnected = true;//the device is reachable
                    AddLog("Device \"" + ChosenDevice.Name + "\" is reachable again.", AppLog.LogCategory.Info);
                }
            }
            else
            {
                if (DeviceConnected == true)
                {
                    DeviceConnected = false; //the device is not reachable
                    AddLog("Device \"" + ChosenDevice.Name + "\" is NOT reachable. Is it still nearby?", AppLog.LogCategory.Warning);
                }
            }
        }
Example #2
0
        private async Task DisconnectDevice()
        {
            if (ChosenDevice != null)
            {
                DeviceInformation delDev = ChosenDevice;
                if (IsMiBand2 && miBand != null)
                {
                    string bandName = ChosenDevice.Name;
                    AddLog($"Disconnecting from {bandName}...", AppLog.LogCategory.Debug);

                    if (miBand.IsConnected())
                    {
                        try {
                            await miBand.HeartRate.UnsubscribeFromHeartRateNotificationsAsync(MiBandHRValueChanged);

                            await miBand.HeartRate.SetRealtimeHeartRateMeasurement(MiBand2SDK.Enums.RealtimeHeartRateMeasurements.DISABLE);
                        } catch {
                            AddLog($"Device not connected.", AppLog.LogCategory.Debug);
                        }
                    }

                    miBand    = null;
                    IsMiBand2 = false;
                }
                else
                {
                    AddLog($"Disconnectiong from {ChosenDevice.Name}...", AppLog.LogCategory.Info);
                    try {
                        await HRReaderCharacteristic.
                        WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None);          // notify the device the deisconnection

                        HRReaderCharacteristic.Service.Dispose();
                        HRReaderService.Dispose();
                        BluetoothDevice.Dispose();
                    } catch {
                        AddLog("Device comunication problem. Can't send disconnect signal.", AppLog.LogCategory.Debug);
                    }
                }

                DispatcherTimer.Stop();
                HrTimerController.Stop();

                ChosenDevice = null;
                InitializeUIComponents();
                AddLog($"{delDev.Name} sucessfully disconnected.", AppLog.LogCategory.Info);
            }
            else
            {
                AddLog($"There is no connected device.", AppLog.LogCategory.Info);
                return;
            }
        }
Example #3
0
        async void BeginBluetoothReadProcess()
        {
            GattCommunicationStatus communicationStatus = GattCommunicationStatus.ProtocolError;

            try {
                AddLog("Sending notify request to device", AppLog.LogCategory.Debug);
                communicationStatus = await HRReaderCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
                    GattClientCharacteristicConfigurationDescriptorValue.Notify);
            } catch (Exception e) {
                // the device refuses the notification request
                AddLog("Request refused by device. Please retry.", AppLog.LogCategory.Warning);
                AddLog($"Exception StackTrace: \n+ {e.StackTrace}\nMessage: {e.Message}", AppLog.LogCategory.Debug);
            }

            DeviceConnected = false;

            switch (communicationStatus)
            {
            case GattCommunicationStatus.Success: {
                AddLog($"Notify request activated. " +
                       $"\n[Gatt Comunication Status {communicationStatus.ToString()}", AppLog.LogCategory.Debug); // log

                DeviceConnected = true;                                                                            // change connection status

                DispatcherTimerSetup();
                ReadFromDevice();
                break;
            }

            default: {
                AddLog($"Notify request error" +
                       $"\n[Gatt Comunication Status {communicationStatus.ToString()}", AppLog.LogCategory.Debug);       // log
                break;
            }
            }
        }