/// <summary>
 /// Receive BatteryStatus-updates continuously.
 /// </summary>
 public void EnableRealTimeBattery(Action <BatteryData> callback)
 {
     RealTimeDisposible?.Dispose();
     RealTimeDisposible = _miBand3.GetCharacteristic(MiBand3Resource.GuidBatteryInfo).RegisterAndNotify().Subscribe(
         x => callback(CreateBatteryObject(x.Characteristic.Value))
         );
 }
 /// <summary>
 /// Add a callback to run everytime the user manually measures their heartrate
 /// </summary>
 /// <param name="callback"></param>
 public void EnableRealTimeHeartrate(Action <HeartrateData> callback)
 {
     RealtimeDisposible?.Dispose();
     RealtimeDisposible = _miBand3.GetCharacteristic(MiBand3Resource.GuidHeartrate).RegisterAndNotify().Subscribe(
         x => callback(new HeartrateData(x.Characteristic.Value))
         );
 }
예제 #3
0
        /// <summary>
        /// Setup the disposables for the fetch operation
        /// </summary>
        /// <param name="date"></param>
        private async Task InitiateFetching(DateTime date)
        {
            _samplenumber = 0;
            //Dispose all DIsposables to prevent double data
            _charActivitySub?.Dispose();
            _charUnknownSub?.Dispose();

            // Subscribe to the unknown and activity characteristics
            _charUnknownSub  = _miBand3.GetCharacteristic(MiBand3Resource.GuidSamplesRequest).RegisterAndNotify().Subscribe(HandleUnknownChar);
            _charActivitySub = _miBand3.GetCharacteristic(MiBand3Resource.GuidActivityData).RegisterAndNotify().Subscribe(HandleActivityChar);

            // Write the date and time from which to receive samples to the Mi Band
            await WriteDateBytes(date);
        }
        public void SetTime(DateTime time)
        {
            //Convert time to bytes
            byte[] timeToSet = ConversionHelper.GetTimeBytes(time, ConversionHelper.TimeUnit.Seconds);

            //Send to MiBand
            _miBand3.GetCharacteristic(MiBand3Resource.GuidCurrentTime).Write(timeToSet).Subscribe(result =>
            {
                Console.WriteLine("Time set to " + time.ToString());
            });
        }
예제 #5
0
        /// <summary>
        /// Authenticates Mi Band 3 devices
        /// </summary>
        /// <exception cref="NullReferenceException">Throws exception if AuthCharacteristic could not be found.</exception>
        /// <exception cref="ConnectionException">Throws exception if authentication went wrong.</exception>
        public async Task Authenticate()
        {
            _authCharacteristic = _miBand3.GetCharacteristic(MiBand3Resource.GuidCharacteristicAuth);
            if (_authCharacteristic != null)
            {
                //Fired when Mi Band 3 is tapped
                AuthenticationDisposable?.Dispose();
                AuthenticationDisposable = _authCharacteristic.RegisterAndNotify().Subscribe(async result =>
                {
                    var data = result.Data;
                    if (data == null)
                    {
                        _miBand3.ConnectionCallback(ConnectionResult.Failed, null);
                        throw new NullReferenceException("No data found in authentication-result.");
                    }

                    //Check if response is valid
                    if (data[0] == MiBand3Resource.AuthResponse && data[2] == MiBand3Resource.AuthSuccess)
                    {
                        if (data[1] == MiBand3Resource.AuthSendKey)
                        {
                            await RequestAuthorizationNumber();
                        }
                        else if (data[1] == MiBand3Resource.AuthRequestRandomAuthNumber)
                        {
                            await RequestRandomEncryptionKey(data);
                        }
                        else if (data[1] == MiBand3Resource.AuthSendEncryptedAuthNumber)
                        {
                            Trace.WriteLine("Authenticated & Connected!");
                            _miBand3.Authenticated = true;
                            _miBand3.ConnectionCallback(ConnectionResult.Succeeded, _miBand3.SecretKey);
                            AuthenticationDisposable.Dispose();
                            return;
                        }
                    }
                    else
                    {
                        _miBand3.Authenticated = false;
                        _miBand3.ConnectionCallback(ConnectionResult.Failed, null);
                        _miBand3.Disconnect();
                    }
                },
                                                                                             exception =>
                {
                    _miBand3.ConnectionCallback(ConnectionResult.Failed, null);
                    throw new ConnectionException(exception.Message);
                });

                if (_miBand3.SecretKey == null)
                {
                    //Triggers vibration on device
                    await TriggerAuthentication();
                }
                else
                {
                    //Continues session with authorization-number
                    await RequestAuthorizationNumber();
                }
            }
            else
            {
                _miBand3.ConnectionCallback(ConnectionResult.Failed, null);
                throw new NullReferenceException("AuthCharacteristic is null!");
            }
        }
예제 #6
0
 /// <summary>
 /// Add a callback to run everytime the Mi Band updates its step count
 /// </summary>
 /// <param name="callback"></param>
 public void EnableRealTimeSteps(Action <StepData> callback)
 {
     realtimeDisposable?.Dispose();
     realtimeDisposable = _miBand3.GetCharacteristic(MiBand3Resource.GuidStepsInfo).RegisterAndNotify().Subscribe(
         x => callback(new StepData(x.Characteristic.Value)));
 }