private async void OnToggleSensorSection(object sender, CompoundButton.CheckedChangeEventArgs e) { if (!Model.Instance.Connected) { return; } EnsureSensorsCreated(); Switch sw = (Switch)sender; TableLayout table = mSensorMap[sw]; if (e.IsChecked) { table.Visibility = ViewStates.Visible; if (table == mTableAccelerometer) { mRadioGroupAccelerometer.Enabled = false; SetChildrenEnabled(mRadioGroupAccelerometer, false); } else if (table == mTableGyro) { mRadioGroupGyro.Enabled = false; SetChildrenEnabled(mRadioGroupGyro, false); } // Turn on the appropriate sensor try { if (sw == mSwitchAccelerometer) { SampleRate rate; if (mRadioAcc16.Checked) { rate = SampleRate.Ms16; } else if (mRadioAcc32.Checked) { rate = SampleRate.Ms32; } else { rate = SampleRate.Ms128; } mTextAccX.Text = ""; mTextAccY.Text = ""; mTextAccZ.Text = ""; accelerometerSensor.StartReadings(rate); } else if (sw == mSwitchGyro) { SampleRate rate; if (mRadioGyro16.Checked) { rate = SampleRate.Ms16; } else if (mRadioGyro32.Checked) { rate = SampleRate.Ms32; } else { rate = SampleRate.Ms128; } mTextGyroAccX.Text = ""; mTextGyroAccY.Text = ""; mTextGyroAccZ.Text = ""; mTextGyroAngX.Text = ""; mTextGyroAngY.Text = ""; mTextGyroAngZ.Text = ""; gyroscopeSensor.StartReadings(rate); } else if (sw == mSwitchDistance) { mTextTotalDistance.Text = ""; mTextSpeed.Text = ""; mTextPace.Text = ""; mTextPedometerMode.Text = ""; distanceSensor.StartReadings(); } else if (sw == mSwitchHeartRate) { var sensorMngr = Model.Instance.Client.SensorManager; if (await sensorMngr.RequestHeartRateConsentTaskAsync(Activity)) { mTextHeartRate.Text = ""; mTextHeartRateQuality.Text = ""; heartRateSensor.StartReadings(); } else { Util.ShowExceptionAlert(Activity, "Start heart rate sensor", new Exception("User declined permission.")); mSwitchHeartRate.Checked = false; } } else if (sw == mSwitchContact) { mTextContact.Text = ""; contactSensor.StartReadings(); } else if (sw == mSwitchSkinTemperature) { mTextSkinTemperature.Text = ""; skinTemperatureSensor.StartReadings(); } else if (sw == mSwitchUltraviolet) { mTextUltraviolet.Text = ""; uvSensor.StartReadings(); } else if (sw == mSwitchPedometer) { mTextTotalSteps.Text = ""; pedometerSensor.StartReadings(); } else if (sw == mSwitchAltimeter) { mTextFlightsAscended.Text = ""; mTextFlightsDescended.Text = ""; mTextRate.Text = ""; mTextSteppingGain.Text = ""; mTextSteppingLoss.Text = ""; mTextStepsAscended.Text = ""; mTextStepsDescended.Text = ""; mTextTotalGain.Text = ""; mTextTotalLoss.Text = ""; altimeterSensor.StartReadings(); } else if (sw == mSwitchAmbientLight) { mTextBrightness.Text = ""; ambientLightSensor.StartReadings(); } else if (sw == mSwitchBarometer) { mTextAirPressure.Text = ""; mTextTemperature.Text = ""; barometerSensor.StartReadings(); } else if (sw == mSwitchCalories) { mTextCalories.Text = ""; caloriesSensor.StartReadings(); } else if (sw == mSwitchGsr) { mTextResistance.Text = ""; gsrSensor.StartReadings(); } else if (sw == mSwitchRRInterval) { var sensorMngr = Model.Instance.Client.SensorManager; if (await sensorMngr.RequestHeartRateConsentTaskAsync(Activity)) { mTextInterval.Text = ""; rrIntervalSensor.StartReadings(); } else { Util.ShowExceptionAlert(Activity, "Start rr interval sensor", new Exception("User declined permission.")); mSwitchRRInterval.Checked = false; } } } catch (BandException ex) { Util.ShowExceptionAlert(Activity, "Register sensor listener", ex); } } else { table.Visibility = ViewStates.Gone; if (table == mTableAccelerometer) { mRadioGroupAccelerometer.Enabled = true; SetChildrenEnabled(mRadioGroupAccelerometer, true); } else if (table == mTableGyro) { mRadioGroupGyro.Enabled = true; SetChildrenEnabled(mRadioGroupGyro, true); } // Turn off the appropriate sensor try { if (sw == mSwitchAccelerometer) { accelerometerSensor.StopReadings(); } else if (sw == mSwitchGyro) { gyroscopeSensor.StopReadings(); } else if (sw == mSwitchDistance) { distanceSensor.StopReadings(); } else if (sw == mSwitchHeartRate) { heartRateSensor.StopReadings(); } else if (sw == mSwitchContact) { contactSensor.StopReadings(); } else if (sw == mSwitchSkinTemperature) { skinTemperatureSensor.StopReadings(); } else if (sw == mSwitchUltraviolet) { uvSensor.StopReadings(); } else if (sw == mSwitchPedometer) { pedometerSensor.StopReadings(); } else if (sw == mSwitchAltimeter) { altimeterSensor.StopReadings(); } else if (sw == mSwitchAmbientLight) { ambientLightSensor.StopReadings(); } else if (sw == mSwitchBarometer) { barometerSensor.StopReadings(); } else if (sw == mSwitchCalories) { caloriesSensor.StopReadings(); } else if (sw == mSwitchGsr) { gsrSensor.StopReadings(); } else if (sw == mSwitchRRInterval) { rrIntervalSensor.StopReadings(); } } catch (BandException ex) { Util.ShowExceptionAlert(Activity, "Unregister sensor listener", ex); } } }
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(); }; }