LoginSignup
9
13

More than 5 years have passed since last update.

Xamarin.Androidで加速度センサーとジャイロセンサーを同時に取得

Last updated at Posted at 2016-08-02

最近、Xamarinに興味を持ち始めて勉強始めたりしてます。

Androidの加速度センサーの取り方は非常にわかりやすい解説がたくさんあります

http://ytabuchi.hatenablog.com/entry/2015/11/28/233420

んで、加速度系とジャイロを同時に取るには、SensorManagerにそれぞれのセンサーをregisterすればいいんだと理解。
詳しいセンサーとかはAndroidの公式リファレンスを見ればなんでも乗ってる。結局はnativeのapiの知識が必要なので

以下、コード全文。
それぞれのテキストにセンサーから取得した値を載せてます。
accelgyroに、それぞれのセンサーを登録し、それをsensorManager.RegisterListenerで登録する。
(それぞれのセンサーの値の更新判定のとき、Javaとは違って、e.Sensor.Typeで型取ることに気づなくて苦闘した)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Hardware;

namespace AndroidTest
{
    [Activity(Label = "MenuActivity")]
    public class MenuActivity : Activity, ISensorEventListener
    {
        TextView accelTextX;
        TextView accelTextY;
        TextView accelTextZ;
        TextView gyroTextX;
        TextView gyroTextY;
        TextView gyroTextZ;
        SensorManager sensorManager;
        Sensor accel;
        Sensor gyro;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Menu);

            accelTextX = FindViewById<TextView>(Resource.Id.accelX);
            accelTextY = FindViewById<TextView>(Resource.Id.accelY);
            accelTextZ = FindViewById<TextView>(Resource.Id.accelZ);
            gyroTextX = FindViewById<TextView>(Resource.Id.gyroX);
            gyroTextY = FindViewById<TextView>(Resource.Id.gyroY);
            gyroTextZ = FindViewById<TextView>(Resource.Id.gyroZ);
        }

        protected override void OnResume()
        {
            base.OnResume();
            sensorManager = (SensorManager)GetSystemService(Context.SensorService);
            accel = sensorManager.GetDefaultSensor(SensorType.Accelerometer);
            gyro = sensorManager.GetDefaultSensor(SensorType.Gyroscope);
            sensorManager.RegisterListener(this, accel, SensorDelay.Normal);
            sensorManager.RegisterListener(this, gyro, SensorDelay.Normal);
        }

        protected override void OnPause()
        {
            base.OnPause();
            sensorManager.UnregisterListener(this);
        }

        public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
        {

        }

        public void OnSensorChanged(SensorEvent e)
        {
            switch (e.Sensor.Type)
            {
                case SensorType.Accelerometer:
                    accelTextX.Text = e.Values[0].ToString();
                    accelTextY.Text = e.Values[1].ToString();
                    accelTextZ.Text = e.Values[2].ToString();
                    break;
                case SensorType.Gyroscope:
                    gyroTextX.Text = e.Values[0].ToString();
                    gyroTextY.Text = e.Values[1].ToString();
                    gyroTextZ.Text = e.Values[2].ToString();
                    break;
                default:
                    break;
            }
        }
    }
}

e.Valuesに順番でx,y,zが入っているようです

9
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
13