Get an accelerometer's device id(or monitor an accelerometer in the backbround) on WinRT

290 views Asked by At

I am looking into the method of monitoring accelerometer in the background.

We can get the sample for Windows Phone which implements it at MSDN.

According to the sample code, it requires the id of an accelerometer. However, there is no property which gets the device ID for WinRT.

var rs = await BackgroundExecutionManager.RequestAccessAsync();
if (rs != BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity
|| rs != BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity) return;

var btb = new BackgroundTaskBuilder() {
    Name = TaskName,
    TaskEntryPoint = TaskEntryPoint
};
btb.SetTrigger(trigger);
taskregist = btb.Register();
taskregist.Completed += new BackgroundTaskCompletedEventHandler(OnBackgroundTaskCompleted);

try {
    var dres = await trigger.RequestAsync();    // Requires the accelerometer's device-id
    switch (dres) {
    case DeviceTriggerResult.Allowed:
        break;
    case DeviceTriggerResult.LowBattery:
        break;
    case DeviceTriggerResult.DeniedBySystem:
        break;
    default:
        break;
    }
} catch (InvalidOperationException) {
}

Can I observe an accelerometer's state in the background on WinRT devices?

1

There are 1 answers

0
SamTh3D3v On BEST ANSWER

Based on msdn, DeviceUseTrigger background task can access the sensors API only from within a Windows Runtime phone app, other winrt devices aren't supported

Sensors support in Windows Phone Store apps When running on a phone, a DeviceUseTrigger background task can access the sensors API only from within a Windows Runtime app. Using a DeviceUseTrigger background task to access the sensors API from within a Windows Phone Silverlight app is not supported.

here the list of what is supported enter image description here

And finally here a basic example of consuming the accelerometer's data by handling the ReadingChanged event

public MainPage()
    {
        this.InitializeComponent();
        this.InitializeAccelerometer();
    }
    private Accelerometer _accelerometer;
    private void InitializeAccelerometer()
    {

        this._accelerometer = Accelerometer.GetDefault();
        if (_accelerometer == null)
        {
            AccelerometerTextBlock.Text = "No accelerometer found.";
            return;
        }

        uint minReportInterval = _accelerometer.MinimumReportInterval;
        var desiredReportInterval = minReportInterval > 16 ? minReportInterval : 16;
        this._accelerometer.ReportInterval = desiredReportInterval;
        this._accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
    }
    private async void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
    {
        if (args.Reading != null)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                AccelerometerTextBlock.Text = String.Format(
                    "X: {0,5:0.00} - Y: {1,5:0.00} - Z: {2,5:0.00} - Timestamp: {3}",
                    args.Reading.AccelerationX,
                    args.Reading.AccelerationY,
                    args.Reading.AccelerationZ,
                    args.Reading.Timestamp);
            });
        }
    }