Hi
I want to know how can i save the output from my accelerometer in a csv file
Im new to xamarin and didnt found anything regarding that
Hi
I want to know how can i save the output from my accelerometer in a csv file
Im new to xamarin and didnt found anything regarding that
Hello,
Welcome to our Microsoft Q&A platform!
You can use dependenceService to achieve it.
If you get the info from accelerometer, then save it in the List like following code. Due to I do not have device to test it, So I write a record in the list firstly with return new List<AccelerometerInfo>() { new AccelerometerInfo() { AccelerationX="33x", AccelerationY="2444Y", AccelerationZ="1256z" } };.
public class AccelerometerInfo
{
public string AccelerationX { get; set; }
public string AccelerationY { get; set; }
public string AccelerationZ { get; set; }
}
public class AccelerometerTest
{
// Set speed delay for monitoring changes.
SensorSpeed speed = SensorSpeed.UI;
List<AccelerometerInfo> accelerometerInfos;
public AccelerometerTest()
{
// Register for reading changes, be sure to unsubscribe when finished
accelerometerInfos = new List<AccelerometerInfo>();
Accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
}
public List<AccelerometerInfo> GetAccelerometerInfos()
{
if (accelerometerInfos.Count>0)
{
return accelerometerInfos;
}
else
{
return new List<AccelerometerInfo>() { new AccelerometerInfo() { AccelerationX="33x", AccelerationY="2444Y", AccelerationZ="1256z" } };
}
}
void Accelerometer_ReadingChanged(object sender, AccelerometerChangedEventArgs e)
{
var data = e.Reading;
accelerometerInfos.Add(new AccelerometerInfo() { AccelerationX=data.Acceleration.X.ToString(), AccelerationY=data.Acceleration.Y.ToString(), AccelerationZ=data.Acceleration.Z.ToString() });
Console.WriteLine($"Reading: X: {data.Acceleration.X}, Y: {data.Acceleration.Y}, Z: {data.Acceleration.Z}");
// Process Acceleration X, Y, and Z
}
public void ToggleAccelerometer()
{
try
{
if (Accelerometer.IsMonitoring)
Accelerometer.Stop();
else
Accelerometer.Start(speed);
}
catch (FeatureNotSupportedException fnsEx)
{
// Feature not supported on device
}
catch (Exception ex)
{
// Other error has occurred.
}
}
}
}
Then create an interface.
public interface IWirteFile
{
void MyWirteCSVFile(List<AccelerometerInfo> infos);
}
I execute the store execute operation by Button Click event like following code.
public partial class MainPage : ContentPage
{
AccelerometerTest accelerometerTest;
public MainPage()
{
InitializeComponent();
accelerometerTest= new AccelerometerTest();
}
private void Button_Clicked(object sender, EventArgs e)
{
List<AccelerometerInfo> accelerometerInfos= accelerometerTest.GetAccelerometerInfos();
DependencyService.Get<IWirteFile>().MyWirteCSVFile(accelerometerInfos);
}
}
Achieve it in the android platform.
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SaveAccelerometer.Droid;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xamarin.Forms;
[assembly: Dependency(typeof(MyFileFileServices))]
namespace SaveAccelerometer.Droid
{
class MyFileFileServices : IWirteFile
{
public void MyWirteCSVFile(List<AccelerometerInfo> infos)
{
//throw new NotImplementedException();
var filename = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "test.csv");
StringBuilder s = new StringBuilder();
// build the data in memory
foreach (var listing in infos)
{
s = s.AppendLine(listing.AccelerationX+" "+listing.AccelerationY+" "+listing.AccelerationZ);
}
// write the data all at once
File.WriteAllText(filename, s.ToString());
}
}
}
Please do not forget to add following permission.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And add android:requestLegacyExternalStorage="true" in the <application> tag of AndroidManifest.xml.
For iOS achievement, you can store csv file to File app , please refer to this thread:
https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
7 people are following this question.