コード例 #1
0
 public MainPage()
 {
     this.InitializeComponent();
     band           = new MiBand2();
     timer          = new DispatcherTimer();
     timer.Interval = new TimeSpan(0, 0, 1);
     timer.Tick    += Timer_Tick;
 }
コード例 #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;
            }
        }
コード例 #3
0
        // set new device
        private async void SetNewDevice(DeviceInformation device)
        {
            await DisconnectDevice();

            ChosenDevice = device;                                                          // set chosen device in the public variable

            this.ConnectStatusText.Text = "Selected device:\n" + "\"" + device.Name + "\""; // change info text with the name of the chosen device


            // set tooltip to the infotext to show the id of the connected
            ToolTipService.SetToolTip(this.ConnectStatusText, new ToolTip {
                Content = ChosenDevice.Name + "\n[id: " + ChosenDevice.Id.ToString() + "]"
            });

            // change hyper link button text (pure estesic purposes)
            this.DevicesPageLink.Content = "Change device";
            AddLog("Device selected: " + ChosenDevice.Name, AppLog.LogCategory.Debug);

            // check if the device is a mi band in order to use an alternative connection method
            IsMiBand2 = false;
            if (ChosenDevice.Name.ToLower().Contains("mi band 2"))
            {
                AddLog("Mi Band 2 detected, switched to alternative connection method.", AppLog.LogCategory.Debug);
                IsMiBand2 = true;
                miBand    = new MiBand2();
            }

            ListCurrentDeviceServicesToLog();     // list all available services of the chosen device in the loglist (debug purposes)
            await CheckBluetoothStatus(true);     // check bluetooth state and if it's turned off, try to turn it on

            // enable the connect button and set a tooltip
            this.ConnectButton.IsEnabled = true;
            ToolTipService.SetToolTip(this.ConnectButton, new ToolTip {
                Content = "Press this button to connect to the choosen device"
            });
        }
コード例 #4
0
 public Authentication(MiBand2 miBand2) => _miBand2 = miBand2;
コード例 #5
0
 public HeartRate(MiBand2 miBand2) => _miBand2 = miBand2;
コード例 #6
0
        /// <summary>
        /// Executes the given command. Will send any exception that occures to the client.
        /// </summary>
        /// <param name="serverCommand">The ServerCommand containing the deviceIndex and the command.</param>
        private static async Task ExecuteCommand(ServerCommand serverCommand)
        {
            int deviceIndex = serverCommand.DeviceIndex;

            if (deviceIndex > MiBands.Count - 1)
            {
                MiBands.Add(new MiBand2(MiBands.Count));
            }
            MiBand2 miBand2 = MiBands[deviceIndex];

            try
            {
                switch (serverCommand.Command)
                {
                case Consts.Command.ConnectBand:
                    await miBand2.ConnectBandAsync();

                    SendSuccess(deviceIndex);
                    break;

                case Consts.Command.DisconnectBand:
                    miBand2.DisconnectBand();
                    SendSuccess(deviceIndex);
                    break;

                case Consts.Command.AuthenticateBand:
                    await miBand2.AuthenticateBandAsync();

                    SendSuccess(deviceIndex);
                    break;

                case Consts.Command.StartMeasurement:
                    await miBand2.StartMeasurementAsync();

                    SendSuccess(deviceIndex);
                    break;

                case Consts.Command.StopMeasurement:
                    await miBand2.StopMeasurementAsync();

                    SendSuccess(deviceIndex);
                    break;

                case Consts.Command.SubscribeToHeartRateChange:
                    miBand2.SubscribeToHeartRateChange(OnHeartRateChange);
                    SendSuccess(deviceIndex);
                    break;

                case Consts.Command.SubscribeToDeviceConnectionStatusChanged:
                    miBand2.DeviceConnectionChanged += OnDeviceConnectionStatusChanged;
                    break;

                case Consts.Command.AskUserForTouch:
                    await miBand2.AskUserForTouchAsync();

                    SendSuccess(deviceIndex);
                    break;

                case Consts.Command.StopServer:
                    if (miBand2.Connected)
                    {
                        miBand2.DisconnectBand();
                    }
                    _listenForCommands = false;
                    _server.Stop();
                    break;

                default:
                    ArgumentOutOfRangeException exception =
                        new ArgumentOutOfRangeException(nameof(serverCommand.Command), serverCommand.Command,
                                                        "Could not find command.");
                    SendData(serverCommand.DeviceIndex, new ServerResponse(exception).ToJson());
                    break;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("EXCEPTION OCCURED:");
                Console.WriteLine("Type: {0}\nMessage{1}", exception.GetType(), exception.Message);
                ServerResponse response = new ServerResponse(exception);
                SendData(serverCommand.DeviceIndex, response.ToJson());
            }
        }