/// <summary> /// SensorCore needs to be activated when app comes back to foreground /// </summary> public async Task ActivateAsync() { if (_sensorActive) { return; } if (_stepCounter != null) { await _stepCounter.ActivateAsync(); } else { await InitializeAsync(); } _sensorActive = true; }
private async Task InitializeSensorAsync() { Exception failure = null; if (!await StepCounter.IsSupportedAsync()) { MessageBox.Show( "Your device doesn't support Motion Data. Application will be closed", "Information", MessageBoxButton.OK); Application.Current.Terminate(); } try { _stepCounter = await StepCounter.GetDefaultAsync(); } catch (Exception e) { failure = e; } if (failure != null) { switch (SenseHelper.GetSenseError(failure.HResult)) { case SenseError.LocationDisabled: case SenseError.SenseDisabled: NavigationService.Navigate(new Uri("/ActivateSensorCore;component/Pages/ActivateSensorCore.xaml", UriKind.Relative)); break; default: throw (failure); } } else { await _stepCounter.ActivateAsync(); await UpdateModelAsync(); } }
public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; Window.Current.VisibilityChanged += async(oo, ee) => { if (ee.Visible) { if (await CallSensorcoreApiAsync(async() => { if (_stepCounter == null) { // Get sensor instance if needed... _stepCounter = await StepCounter.GetDefaultAsync(); } else { // ... otherwise just activate it await _stepCounter.ActivateAsync(); } })) { // Display current reading whenever application is brought to foreground await ShowCurrentReading(); } } else { // Sensor needs to be deactivated when application is put to background if (_stepCounter != null) { await CallSensorcoreApiAsync(async() => await _stepCounter.DeactivateAsync()); } } }; }