Search code examples
c#asynchronouswin-universal-appunhandled-exceptionpedometer

async Pedometer.GetSystemHistoryAsync API for Universal apps crashes


I'm trying to throw together a simple app that uses pedometer data on a Windows 10 phone. I normally live down in kernel-land, and this is my first time using most of the c# async stuff, so I'm wondering if I'm missing a core concept here.

My first attempt at getting data out was to simply report the number of recorded steps over the last hour to a textbox in my XAML app. I just created a basic XAML app, dropped a text box in, and added this event handler:

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    var history = await Pedometer.GetSystemHistoryAsync(DateTimeOffset.Now.AddHours(-1), TimeSpan.FromHours(1));
    int count = history.Sum(entry => entry.CumulativeSteps);
    textSteps.Text = count.ToString();
}

A breakpoint on the first line triggers, but before it hits the next line I get an unhandled exception. Error code -2147467259, message "Error HRESULT E_FAIL has been returned from a call to a COM component."

The top frame of the stack is in my code, but it's just the boilerplate line from App.g.i.cs that triggers a break on an unhandled exception. Below that is mscorlib and the WinRT invoker.

I looked through the app capabilities list in the manifest, and didn't find anything that looked like it applied to the pedometer. I'm testing this on a Lumia 950.

UPDATE: I just tried calling the API to get the default pedometer sensor:

Pedometer p = await Pedometer.GetDefaultAsync();

It turns out that this triggers an access denied exception with the same worthless stack. I'm currently doing more research to see if there is something that needs to be specified in the manifest.


Solution

  • After further experimenting got me an access denied error, I looked into the manifest more. The manifest Microsoft example project for the pedometer declares a device property that I can't find a way to add through the designer view. Adding it to the code worked perfectly. It's saying I've taken 300,000 steps in the last hour, but I'm sure some simple debugging will find the answer there. (The property is called CumulativeSteps, so that's a good hint...)

    <Capabilities>
        <DeviceCapability Name="activity" />
    </Capabilities>