Search code examples
c#windows-mobilewin-universal-appshake

Accelerometer Shaken event in windows universal app doesn't work


I try to detect a shake on my phone with the Shaken event from Accelerometer object. The accelerometer object is not null but when I shake the phone, it never go in the _accelerometer_Shaken event.

    public int shakeCount = 0;
    Accelerometer _accelerometer = Accelerometer.GetDefault();
    public MainPage()
    {
        this.InitializeComponent();

        if (_accelerometer != null)
        {
            _accelerometer.Shaken += _accelerometer_Shaken;
        }
    }

    async private void _accelerometer_Shaken(Accelerometer sender, AccelerometerShakenEventArgs args)
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            shakeCount++;
            tbInfo.Text = shakeCount.ToString();
        });

    }

I don't understand why


Solution

  • This feature is not supported yet.

    This is an extract from the official sample code

    https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Accelerometer

    Accelerometer Shake Events

    When you choose the Enable button for the Shake Events option, the app displays the cumulative number of shake events each time an event occurs. (The app first increments the event count and then renders the most recent value.)

    Note that shake events are not supported in Windows 10 build 10240, so the Shaken event will never be raised, but the sample demonstrates how to handle the event when support for shake is added.

    I made a test under Windows 10 10586 and it still does not work.