How to detect device motion direction in Flutter?

818

Solution 1

You can try using location package to access device current location. It has attribute of heading which can be useful for you. You can use a listener to capture device location changes. Whenever device heading is changed, the heading value will be printed.

Location location = Location();

location.onLocationChanged.listen((LocationData currentLocation) {
      // Use current location
    print(currentLocation.heading);
});

Read more here: https://pub.dev/packages/location

Heading is a double value, that varies from 0 to 360 degrees. This image will help you to see what heading value means.

Solution 2

The plugin tells you about acceleration. If you want to detect motion, i.e. speed, you need to apply in integration filter to the data from those events. You may want to use UserAccellerationEvents since those already eliminate gravity.

Also, using events from the gyroscope will make you calculation more robust.

This is an application of sensor fusion by the way.

Share:
818
Ankit Chawla
Author by

Ankit Chawla

Updated on January 02, 2023

Comments

  • Ankit Chawla
    Ankit Chawla over 1 year

    I am trying to detect the direction in which a device is moved. If a device (mobile or tablet) is picked up and moved to the right or left, I want to detect that using Flutter.

    Here is what I've tried:

    I have tried using sensors_plus library for this and tried using the accelerometer for detecting left or right direction and the program is given below.

    void initState() {
    userAccelerometerEvents.listen((UserAccelerometerEvent event) {
      var me = event.x;
    
      var me2 = num.parse(me.toStringAsFixed(2));
      print(me2);
    
      if (me2.round() < 0) {
        setState(() {
          direction = "Left";
        });
      }
      if (me2 > 0) {
        if (me2 > 0.5) {
          setState(() {
            direction = "Right";
          });
        } else {
          setState(() {
            direction = "Still";
          });
        }
      }
    });
    super.initState();}
    

    the outputs I get are:

    enter image description here

    The program only shows left or right if I move the device really fast, although I would like to detect the direction on slower speeds. For this, I have tried to take smaller values of "event.x" but in that case, the output switches between "Left" and "Right" and "Still" really quickly even when the device is still.

    I could not find any example that covers this problem online or in the package docs or examples.

    I would like to know how I can achieve this? I would like to know the left and right movement of the device with slow speeds too.

    Should I use another sensor?

    I do not fully understand the magnetometer's inputs which I've also tried using. Any help in understanding that is appreciated.

    I have also tried searching for a package on pub.dev but I can't find any that fit this need.

    Any help is appreciated thanks a lot in advance.

  • Ankit Chawla
    Ankit Chawla over 2 years
    I ran this code, the value of currentlocation.heading remains 90.0 through out. How much should i move my device to see a change in value?