//
        // Sensor event handlers
        //
        private void EnsureSensorsCreated()
        {
            IBandSensorManager sensorMgr = Model.Instance.Client.SensorManager;

            if (accelerometerSensor == null)
            {
                accelerometerSensor = sensorMgr.CreateAccelerometerSensor();
                accelerometerSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var accelerometerEvent = e.SensorReading;
                        mTextAccX.Text = string.Format("{0:F3} m/s/s", accelerometerEvent.AccelerationX);
                        mTextAccY.Text = string.Format("{0:F3} m/s/s", accelerometerEvent.AccelerationY);
                        mTextAccZ.Text = string.Format("{0:F3} m/s/s", accelerometerEvent.AccelerationZ);
                    });
                };
            }

            if (contactSensor == null)
            {
                contactSensor = sensorMgr.CreateContactSensor();
                contactSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var contactEvent = e.SensorReading;
                        mTextContact.Text = contactEvent.ContactState.ToString();
                    });
                };
            }

            if (distanceSensor == null)
            {
                distanceSensor = sensorMgr.CreateDistanceSensor();
                distanceSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var distanceEvent = e.SensorReading;
                        mTextTotalDistance.Text = string.Format("{0:D} cm", distanceEvent.TotalDistance);
                        mTextSpeed.Text = string.Format("{0:F2} cm/s", distanceEvent.Speed);
                        mTextPace.Text = string.Format("{0:F2} ms/m", distanceEvent.Pace);
                        mTextPedometerMode.Text = distanceEvent.MotionType.ToString();
                    });
                };
            }

            if (gyroscopeSensor == null)
            {
                gyroscopeSensor = sensorMgr.CreateGyroscopeSensor();
                gyroscopeSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var gyroscopeEvent = e.SensorReading;
                        mTextGyroAccX.Text = string.Format("{0:F3} m/s/s", gyroscopeEvent.AccelerationX);
                        mTextGyroAccY.Text = string.Format("{0:F3} m/s/s", gyroscopeEvent.AccelerationY);
                        mTextGyroAccZ.Text = string.Format("{0:F3} m/s/s", gyroscopeEvent.AccelerationZ);
                        mTextGyroAngX.Text = string.Format("{0:F2} deg/s", gyroscopeEvent.AngularVelocityX);
                        mTextGyroAngY.Text = string.Format("{0:F2} deg/s", gyroscopeEvent.AngularVelocityY);
                        mTextGyroAngZ.Text = string.Format("{0:F2} deg/s", gyroscopeEvent.AngularVelocityZ);
                    });
                };
            }

            if (heartRateSensor == null)
            {
                heartRateSensor = sensorMgr.CreateHeartRateSensor();
                heartRateSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var heartRateEvent = e.SensorReading;
                        mTextHeartRate.Text = string.Format("{0:D} beats/min", heartRateEvent.HeartRate);
                        mTextHeartRateQuality.Text = heartRateEvent.Quality.ToString();
                    });
                };
            }

            if (pedometerSensor == null)
            {
                pedometerSensor = sensorMgr.CreatePedometerSensor();
                pedometerSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var pedometerEvent = e.SensorReading;
                        mTextTotalSteps.Text = string.Format("{0:D} steps", pedometerEvent.TotalSteps);
                    });
                };
            }

            if (skinTemperatureSensor == null)
            {
                skinTemperatureSensor = sensorMgr.CreateSkinTemperatureSensor();
                skinTemperatureSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var skinTemperatureEvent = e.SensorReading;
                        mTextSkinTemperature.Text = string.Format("{0:F1} (C)", skinTemperatureEvent.Temperature);
                    });
                };
            }

            if (uvSensor == null)
            {
                uvSensor = sensorMgr.CreateUVSensor();
                uvSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var uvEvent = e.SensorReading;
                        mTextUltraviolet.Text = uvEvent.UVIndexLevel.ToString();
                    });
                };
            }
            
            if (altimeterSensor == null)
            {
                altimeterSensor = sensorMgr.CreateAltimeterSensor();
                altimeterSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var altimeterEvent = e.SensorReading;
                        mTextFlightsAscended.Text = string.Format("{0:D} floors", altimeterEvent.FlightsAscended);
                        mTextFlightsDescended.Text = string.Format("{0:D} floors", altimeterEvent.FlightsDescended);
                        mTextRate.Text = string.Format("{0:F2} cm/s", altimeterEvent.Rate);
                        mTextSteppingGain.Text = string.Format("{0:D} cm", altimeterEvent.SteppingGain);
                        mTextSteppingLoss.Text = string.Format("{0:D} cm", altimeterEvent.SteppingLoss);
                        mTextStepsAscended.Text = string.Format("{0:D} steps", altimeterEvent.StepsAscended);
                        mTextStepsDescended.Text = string.Format("{0:D} steps", altimeterEvent.StepsDescended);
                        mTextTotalGain.Text = string.Format("{0:D} cm", altimeterEvent.TotalGain);
                        mTextTotalLoss.Text = string.Format("{0:D} cm", altimeterEvent.TotalLoss);
                    });
                };
            }

            if (ambientLightSensor == null)
            {
                ambientLightSensor = sensorMgr.CreateAmbientLightSensor();
                ambientLightSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var ambientLightEvent = e.SensorReading;
                        mTextBrightness.Text = string.Format("{0:D} lux", ambientLightEvent.Brightness);
                    });
                };
            }
            
            if (barometerSensor == null)
            {
                barometerSensor = sensorMgr.CreateBarometerSensor();
                barometerSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var barometerEvent = e.SensorReading;
                        mTextAirPressure.Text = string.Format("{0:D} hPa", barometerEvent.AirPressure);
                        mTextTemperature.Text = string.Format("{0:D} (C)", barometerEvent.Temperature);
                    });
                };
            }
            
            if (caloriesSensor == null)
            {
                caloriesSensor = sensorMgr.CreateCaloriesSensor();
                caloriesSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var caloriesEvent = e.SensorReading;
                        mTextCalories.Text = string.Format("{0:D} kcals", caloriesEvent.Calories);
                    });
                };
            }
            
            if (gsrSensor == null)
            {
                gsrSensor = sensorMgr.CreateGsrSensor();
                gsrSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var gsrEvent = e.SensorReading;
                        mTextResistance.Text = string.Format("{0:D} kohms", gsrEvent.Resistance);
                    });
                };
            }
            
            if (rrIntervalSensor == null)
            {
                rrIntervalSensor = sensorMgr.CreateRRIntervalSensor();
                rrIntervalSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var rrIntervalEvent = e.SensorReading;
                        mTextInterval.Text = string.Format("{0:2F} s", rrIntervalEvent.Interval);
                    });
                };
            }
        }
        //
        // Sensor event handlers
        //
        private void EnsureSensorsCreated()
        {
            IBandSensorManager sensorMgr = Model.Instance.Client.SensorManager;

            if (accelerometerSensor == null)
            {
                accelerometerSensor = sensorMgr.CreateAccelerometerSensor();
                accelerometerSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var accelerometerEvent = e.SensorReading;
                        mTextAccX.Text         = string.Format("{0:F3} m/s/s", accelerometerEvent.AccelerationX);
                        mTextAccY.Text         = string.Format("{0:F3} m/s/s", accelerometerEvent.AccelerationY);
                        mTextAccZ.Text         = string.Format("{0:F3} m/s/s", accelerometerEvent.AccelerationZ);
                    });
                };
            }

            if (contactSensor == null)
            {
                contactSensor = sensorMgr.CreateContactSensor();
                contactSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var contactEvent  = e.SensorReading;
                        mTextContact.Text = contactEvent.ContactState.ToString();
                    });
                };
            }

            if (distanceSensor == null)
            {
                distanceSensor = sensorMgr.CreateDistanceSensor();
                distanceSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var distanceEvent       = e.SensorReading;
                        mTextTotalDistance.Text = string.Format("{0:D} cm", distanceEvent.TotalDistance);
                        mTextSpeed.Text         = string.Format("{0:F2} cm/s", distanceEvent.Speed);
                        mTextPace.Text          = string.Format("{0:F2} ms/m", distanceEvent.Pace);
                        mTextPedometerMode.Text = distanceEvent.MotionType.ToString();
                    });
                };
            }

            if (gyroscopeSensor == null)
            {
                gyroscopeSensor = sensorMgr.CreateGyroscopeSensor();
                gyroscopeSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var gyroscopeEvent = e.SensorReading;
                        mTextGyroAccX.Text = string.Format("{0:F3} m/s/s", gyroscopeEvent.AccelerationX);
                        mTextGyroAccY.Text = string.Format("{0:F3} m/s/s", gyroscopeEvent.AccelerationY);
                        mTextGyroAccZ.Text = string.Format("{0:F3} m/s/s", gyroscopeEvent.AccelerationZ);
                        mTextGyroAngX.Text = string.Format("{0:F2} deg/s", gyroscopeEvent.AngularVelocityX);
                        mTextGyroAngY.Text = string.Format("{0:F2} deg/s", gyroscopeEvent.AngularVelocityY);
                        mTextGyroAngZ.Text = string.Format("{0:F2} deg/s", gyroscopeEvent.AngularVelocityZ);
                    });
                };
            }

            if (heartRateSensor == null)
            {
                heartRateSensor = sensorMgr.CreateHeartRateSensor();
                heartRateSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var heartRateEvent         = e.SensorReading;
                        mTextHeartRate.Text        = string.Format("{0:D} beats/min", heartRateEvent.HeartRate);
                        mTextHeartRateQuality.Text = heartRateEvent.Quality.ToString();
                    });
                };
            }

            if (pedometerSensor == null)
            {
                pedometerSensor = sensorMgr.CreatePedometerSensor();
                pedometerSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var pedometerEvent   = e.SensorReading;
                        mTextTotalSteps.Text = string.Format("{0:D} steps", pedometerEvent.TotalSteps);
                    });
                };
            }

            if (skinTemperatureSensor == null)
            {
                skinTemperatureSensor = sensorMgr.CreateSkinTemperatureSensor();
                skinTemperatureSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var skinTemperatureEvent  = e.SensorReading;
                        mTextSkinTemperature.Text = string.Format("{0:F1} (C)", skinTemperatureEvent.Temperature);
                    });
                };
            }

            if (uvSensor == null)
            {
                uvSensor = sensorMgr.CreateUVSensor();
                uvSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var uvEvent           = e.SensorReading;
                        mTextUltraviolet.Text = uvEvent.UVIndexLevel.ToString();
                    });
                };
            }

            if (altimeterSensor == null)
            {
                altimeterSensor = sensorMgr.CreateAltimeterSensor();
                altimeterSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var altimeterEvent         = e.SensorReading;
                        mTextFlightsAscended.Text  = string.Format("{0:D} floors", altimeterEvent.FlightsAscended);
                        mTextFlightsDescended.Text = string.Format("{0:D} floors", altimeterEvent.FlightsDescended);
                        mTextRate.Text             = string.Format("{0:F2} cm/s", altimeterEvent.Rate);
                        mTextSteppingGain.Text     = string.Format("{0:D} cm", altimeterEvent.SteppingGain);
                        mTextSteppingLoss.Text     = string.Format("{0:D} cm", altimeterEvent.SteppingLoss);
                        mTextStepsAscended.Text    = string.Format("{0:D} steps", altimeterEvent.StepsAscended);
                        mTextStepsDescended.Text   = string.Format("{0:D} steps", altimeterEvent.StepsDescended);
                        mTextTotalGain.Text        = string.Format("{0:D} cm", altimeterEvent.TotalGain);
                        mTextTotalLoss.Text        = string.Format("{0:D} cm", altimeterEvent.TotalLoss);
                    });
                };
            }

            if (ambientLightSensor == null)
            {
                ambientLightSensor = sensorMgr.CreateAmbientLightSensor();
                ambientLightSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var ambientLightEvent = e.SensorReading;
                        mTextBrightness.Text  = string.Format("{0:D} lux", ambientLightEvent.Brightness);
                    });
                };
            }

            if (barometerSensor == null)
            {
                barometerSensor = sensorMgr.CreateBarometerSensor();
                barometerSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var barometerEvent    = e.SensorReading;
                        mTextAirPressure.Text = string.Format("{0:D} hPa", barometerEvent.AirPressure);
                        mTextTemperature.Text = string.Format("{0:D} (C)", barometerEvent.Temperature);
                    });
                };
            }

            if (caloriesSensor == null)
            {
                caloriesSensor = sensorMgr.CreateCaloriesSensor();
                caloriesSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var caloriesEvent  = e.SensorReading;
                        mTextCalories.Text = string.Format("{0:D} kcals", caloriesEvent.Calories);
                    });
                };
            }

            if (gsrSensor == null)
            {
                gsrSensor = sensorMgr.CreateGsrSensor();
                gsrSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var gsrEvent         = e.SensorReading;
                        mTextResistance.Text = string.Format("{0:D} kohms", gsrEvent.Resistance);
                    });
                };
            }

            if (rrIntervalSensor == null)
            {
                rrIntervalSensor = sensorMgr.CreateRRIntervalSensor();
                rrIntervalSensor.ReadingChanged += (sender, e) =>
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var rrIntervalEvent = e.SensorReading;
                        mTextInterval.Text  = string.Format("{0:2F} s", rrIntervalEvent.Interval);
                    });
                };
            }
        }
Пример #3
0
        public void InitSensors(TestMeViewModel b)
        {
            if (!_client.IsConnected)
            {
                return;
            }
            _hrSensor        = _hrSensor ?? _client.SensorManager.CreateHeartRateSensor();
            _gsrSensor       = _gsrSensor ?? _client.SensorManager.CreateGsrSensor();
            _rrSensor        = _rrSensor ?? _client.SensorManager.CreateRRIntervalSensor();
            _contactSensor   = _contactSensor ?? _client.SensorManager.CreateContactSensor();
            _distancerSensor = _distancerSensor ?? _client.SensorManager.CreateDistanceSensor();

            if (_contactSensor == null || _hrSensor == null || _gsrSensor == null || _rrSensor == null || _distancerSensor == null)
            {
                return;
            }
            Activity activity = Droid.MainActivity.instance;

            //register contact listener
            _contactSensor.ReadingChanged += (sender, e) =>
            {
                var contactEvent = e.SensorReading;
                _bandState = contactEvent.ContactState;
            };
            //register heart rate listener
            _hrSensor.ReadingChanged += (sender, e) =>
            {
                activity.RunOnUiThread(() =>
                {
                    var heartRateEvent = e.SensorReading;
                    //_hrReadings.Add(heartRateEvent.HeartRate);
                    if (heartRateEvent.Quality == HeartRateQuality.Locked)
                    {
                        _hrSensor.StopReadings();
                        _contactSensor.StopReadings();
                        // if (b != null) { b.HR = _hrReadings[_hrReadings.Count - 1]; } //update ViewModel
                    }
                    if (_bandState != BandContactState.Worn) //user took off the band while reading
                    {
                        _hrSensor.StopReadings();
                        _contactSensor.StopReadings();
                        _bandState = null;
                        return;
                    }
                });
            };
            //register gsr listener
            _gsrSensor.ReadingChanged += (sender, e) =>
            {
                activity.RunOnUiThread(() =>
                {
                    var gsrEvent = e.SensorReading;
                    _gsrReadings.Add(gsrEvent.Resistance);
                    if (b != null)
                    {
                        b.GsrList = gsrEvent.Resistance.ToString();
                    }

                    if (_gsrDone)
                    {
                        _gsrSensor.StopReadings();
                        _contactSensor.StopReadings();
                        return;
                    }
                });
            };
            //register RR Intervals listener
            _rrSensor.ReadingChanged += (sender, e) =>
            {
                if (_bandState != BandContactState.Worn) //user took off the band while reading
                {
                    _rrSensor.StopReadings();
                    _contactSensor.StopReadings();
                    _bandState     = null;
                    b.StressResult = "Error: band is not worn.";
                    return;
                }
                var rrEvent = e.SensorReading;
                _rrIntervalsReadings.Add(rrEvent.Interval);
            };
            _distancerSensor.ReadingChanged += (sender, e) =>
            {
                currentMotionTyp = e.SensorReading.MotionType;
                _distancerSensor.StopReadings();
            };
        }