在 WinRT 上获取加速度计的设备 ID(或在后台监控加速度计)

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

正在研究后台监控加速度计的方法

我们可以获得 Windows Phone 的示例,它在 MSDN 实现了它。

根据示例代码,需要加速度计的id。但是,没有 属性 获取 WinRT 的设备 ID。

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) {
}

我可以在 WinRT 设备的后台观察加速度计的状态吗?

基于 msdnDeviceUseTrigger 后台任务只能从 Windows 运行时 phone 应用程序中访问传感器 API,其他 winrt 设备不能'支持

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.

这里是支持的列表

And 最后是一个通过处理 ReadingChanged 事件

使用加速度计数据的基本示例
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);
            });
        }
    }