How to Create a Mobile App to Monitor Accelerometer | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (172).NET Core  (29).NET MAUI  (192)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (209)BoldSign  (12)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (63)Flutter  (131)JavaScript  (219)Microsoft  (118)PDF  (80)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (882)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (49)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (125)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (62)Development  (613)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (37)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (488)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (41)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (368)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (30)Visual Studio Code  (17)Web  (577)What's new  (313)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

How to Create a Mobile App That Monitors Accelerometer Data

This guest blog was written by Brandon Minnick, a Developer Advocate at Microsoft, specializing in Xamarin and Azure.

Thanks to Xamarin.Essentials and Syncfusion’s circular gauge control, creating a mobile app that monitors accelerometer data has never been easier!

Let’s take a look at how to implement this control in a Xamarin.Forms app.

                 

Walkthrough

1. Install NuGet Packages

  1. Install the Syncfusion.Xamarin.SfGauge NuGet Package into each project, this includes the Xamarin.iOS, Xamarin.Android, and the .NET Standard Project (if one is being used).
  2. Install the Xamarin.Essentials NuGet Package into each project, this includes the Xamarin.iOS, Xamarin.Android, and the .NET Standard Project (if one is being used).

2. Initialize Syncfusion on iOS

To utilize the Syncfusion circular gauge, we first need to initialize it in AppDelegate.FinishedLaunching.

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    ...

    public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
        ...

        Syncfusion.SfGauge.XForms.iOS.SfGaugeRenderer.Init();
        Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Your SyncFusion License Key");

        ...
    }
}

 

3. Initialize Syncfusion on Android

To utilize the Syncfusion circular gauge, we first need to initialize it in MainActivity.OnCreate:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    ...

    protected override void OnCreate(Bundle savedInstanceState)
    {
        ...

        Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Your SyncFusion License Key");

        ...
    }
}

 

4. Reference Mono.Android.Export

    1. In the Solution Explorer in the Android project, right-click on References.
    2. In the References menu, select Edit References.

  1. In the Edit References window, select the Packages tab.
  2. In the Packages tab, locate Mono.Android.Export.
  3. In the Packages tab, ensure Mono.Android.Export is checked.
  4. In the Edit References window, click OK.

5. Implement SfCircularGauge in Xamarin.Forms

This app requires three instances of the circular gauge control in our app, so let’s start by creating an implementation of SfCircularGauge.

public class CircularGaugeView : SfCircularGauge
{
    public CircularGaugeView(string headerText, double startValue, double endValue)
    {
        Pointer = new NeedlePointer { AnimationDuration = 0.5 };

        var header = new Header
        {
            Text = headerText,
            ForegroundColor = Color.Gray
        };

        var circularGaugeScale = new Scale
        {
            Interval = (endValue - startValue) / 10,
            StartValue = startValue,
            EndValue = endValue,
            ShowTicks = true,
            ShowLabels = true,
            Pointers = { Pointer },
            MinorTicksPerInterval = 4,
        };

        Scales = new ObservableCollection<Scale> { circularGaugeScale };
        Headers = new ObservableCollection<Header> { header };
    }

    public NeedlePointer Pointer { get; }
}

 

6. Create Accelerometer Page

In the Xamarin.Forms project, create a new class, AccelerometerPage.cs:

public class AccelerometerPage : ContentPage
{
    readonly CircularGaugeView xCircularGauge, yCircularGauge, zCircularGauge;

    public AccelerometerPage()
    {
        Icon = "Accelerometer";
        Title = "Accelerometer";

        xCircularGauge = new CircularGaugeView("X-Axis", -1, 1);
        yCircularGauge = new CircularGaugeView("Y-Axis", -1, 1);
        zCircularGauge = new CircularGaugeView("Z-Axis", -10, 10);

        var grid = new Grid
        {
            Margin = new Thickness(0, 20),
            RowDefinitions = {
                new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
            },
            ColumnDefinitions = {
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
            }
        };
        grid.Children.Add(xCircularGauge, 0, 0);
        grid.Children.Add(yCircularGauge, 0, 1);
        grid.Children.Add(zCircularGauge, 0, 2);

        Content = grid;

        On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        InitializeAccelerometer();
    }

    void InitializeAccelerometer()
    {
        try
        {
            Accelerometer.Start(SensorSpeed.Normal);
            Accelerometer.ReadingChanged += HandleAccelerometerReadingChanged;
        }
        catch (FeatureNotSupportedException)
        {
            Debug.WriteLine("Accelerometer Unavailable");
        }
    }

    void HandleAccelerometerReadingChanged(AccelerometerChangedEventArgs e)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            xCircularGauge.Pointer.Value = e.Reading.Acceleration.X;
            yCircularGauge.Pointer.Value = e.Reading.Acceleration.Y;
            zCircularGauge.Pointer.Value = e.Reading.Acceleration.Z;
        });
    }
}

 

7. Set AccelerometerPage as the MainPage

In App.cs, ensure that MainPage = new AccelerometerPage();

 

public class App : Xamarin.Forms.Application
{
    public App()
    {
        MainPage = new AccelerometerPage();
    }
}

 

8. Launch the app on an iOS or Android Device

About the Author

Brandon Minnick is a Developer Advocate at Microsoft, specializing in Xamarin and Azure. As a Developer Advocate, Brandon works closely with the mobile app community, helping them create 5-star apps and provide their feedback to the Microsoft product teams to help improve our tools and empower every person and every organization on the planet to achieve more.

@TheCodeTraveler

Resources

 

If you like this blog post, we think you’ll also like the following free e-books:

Xamarin.Forms Succinctly
Xamarin.Forms for macOS Succinctly
Writing Native Mobile Apps in a Functional Language Succinctly

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top