コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using DeviceMotion.Plugin.Abstractions;
namespace AppName.Services
{
public delegate void OnMotionChangedDelegate(MotionVector vector);
//DependencyServiceから利用する
public interface IMotionService
{
// 非同期で動作情報の変更をリスニングする
void StartGyroscopeSensor(MotionSensorDelay delay, double threshold);
//動作情報変更イベント
event OnMotionChangedDelegate OnMotionChanged;
}
}
using System;
using Xamarin.Forms;
using AppName.Droid.Services;
using AppName.Services;
using DeviceMotion.Plugin;
using DeviceMotion.Plugin.Abstractions;
[assembly: Dependency(typeof(MotionService))]
namespace AppName.Droid.Services
{
public class MotionService : IMotionService
{
private double _threshold = 0;
/// <summary>
/// 非同期で動作情報の変更をリスニングする
/// </summary>
/// <param name="delay">動作を検知する間隔</param>
/// <param name="threshold">変更イベントを通知する閾値</param>
public void StartGyroscopeSensor(MotionSensorDelay delay, int threshold)
{
//閾値を変数に確保する
_threshold = threshold;
//ジャイロセンサーの検知を開始する
IDeviceMotion motion = CrossDeviceMotion.Current;
motion.Start(MotionSensorType.Gyroscope, delay);
//ジャイロセンサーの変更イベントを実装します。
motion.SensorValueChanged += (object sender, SensorValueChangedEventArgs e) =>
{
if (this.OnMotionChanged != null &&
e.SensorType == MotionSensorType.Gyroscope)
{
MotionVector vector = (MotionVector)e.Value;
//閾値を超えた場合のみイベント通知する
if (Math.Abs(vector.X) > _threshold ||
Math.Abs(vector.Y) > _threshold ||
Math.Abs(vector.Z) > _threshold)
{
this.OnMotionChanged((MotionVector)e.Value);
}
}
};
}
//動作情報変更イベント
public event OnMotionChangedDelegate OnMotionChanged;
}
}
using System;
using Xamarin.Forms;
using AppName.iOS.Services;
using AppName.Services;
using DeviceMotion.Plugin;
using DeviceMotion.Plugin.Abstractions;
[assembly: Dependency(typeof(MotionService))]
namespace AppName.iOS.Services
{
public class MotionService : IMotionService
{
private double _threshold = 0;
/// <summary>
/// 非同期で動作情報の変更をリスニングする
/// </summary>
/// <param name="delay">動作を検知する間隔</param>
/// <param name="threshold">変更イベントを通知する閾値</param>
public void StartGyroscopeSensor(MotionSensorDelay delay, int threshold)
{
//閾値を変数に確保する
_threshold = threshold;
//ジャイロセンサーの検知を開始する
IDeviceMotion motion = CrossDeviceMotion.Current;
motion.Start(MotionSensorType.Gyroscope, delay);
//ジャイロセンサーの変更イベントを実装します。
motion.SensorValueChanged += (object sender, SensorValueChangedEventArgs e) =>
{
if (this.OnMotionChanged != null &&
e.SensorType == MotionSensorType.Gyroscope)
{
MotionVector vector = (MotionVector)e.Value;
//閾値を超えた場合のみイベント通知する
if (Math.Abs(vector.X) > _threshold ||
Math.Abs(vector.Y) > _threshold ||
Math.Abs(vector.Z) > _threshold)
{
this.OnMotionChanged((MotionVector)e.Value);
}
}
};
}
//動作情報変更イベント
public event OnMotionChangedDelegate OnMotionChanged;
}
}
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
void GyroscopeSensor_Click(object sender, EventArgs e)
{
DependencyService.Get<IMotionService>().OnMotionChanged += new OnMotionChangedDelegate(this.MotionChanged);
//非同期スレッドを開始する
DependencyService.Get<IMotionService>().StartGyroscopeSensor(DeviceMotion.Plugin.Abstractions.MotionSensorDelay.Default, 1);
}
void MotionChanged(DeviceMotion.Plugin.Abstractions.MotionVector vector)
{
//変更イベントを捕捉してローカル通知で情報をお知らせしています。
DependencyService.Get<INotificationService>().SetNotifyCondition(DateTimeOffset.Now, 0, "");
DependencyService.Get<INotificationService>().On("ジャイロスコープセンサー", "ジャイロスコープセンサーテスト", "X=" + vector.X.ToString() + ";Y=" + vector.Y.ToString() + ";Z=" + vector.Z.ToString());
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。