Привет У меня есть большая проблема, тоже получают данные от нескольких датчиков моего кода для регистра и чтение данных является:несколько датчиков данных Xamarin Android
[Service]
class BackgroundServiceSensoren : Service
{
StepDetector sd;
Azimut az;
double obererSchwellenWert = 2.0;
double untererSchwellenWert = -2.0;
Boolean timerFlag = false;
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Task.Run(() =>
{
sd = new StepDetector(obererSchwellenWert, untererSchwellenWert, timerFlag, 0);
az = new Azimut();
var mSensorManager1 = (SensorManager)Application.Context.GetSystemService(Context.SensorService);
var mSensorManager2 = (SensorManager)Application.Context.GetSystemService(Context.SensorService);
var mLinearAccelerometer = mSensorManager1.GetDefaultSensor(SensorType.LinearAcceleration);
// mSensorManager1.RegisterListener(sd, mLinearAccelerometer, SensorDelay.Fastest);
var mAzimutMagneticField = mSensorManager2.GetDefaultSensor(SensorType.MagneticField);
var mAzimutAccelerometer = mSensorManager2.GetDefaultSensor(SensorType.Accelerometer);
mSensorManager2.RegisterListener(az, mAzimutMagneticField, SensorDelay.Game);
mSensorManager2.RegisterListener(az, mAzimutAccelerometer, SensorDelay.Fastest);
});
return StartCommandResult.Sticky;
}
class Azimut : Java.Lang.Object, ISensorEventListener
{
float [] mGravity;
float [] mGeomagnetic;
float azimut;
public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
{
}
public void OnSensorChanged(SensorEvent e)
{
Sensor mySensor = e.Sensor;
if (mySensor.Type == SensorType.Accelerometer)
{
if (mGravity == null)
{
mGravity = new float[3];
}
e.Values.CopyTo(mGravity, 0);
}
if (mySensor.Type == SensorType.MagneticField)
{
if(mGeomagnetic == null){
mGeomagnetic = new float[3];
}
e.Values.CopyTo(mGeomagnetic,0);
}
if (mGeomagnetic != null && mGravity != null)
{
float[] R = new float[9];
float[] I = new float[9];
bool success = SensorManager.GetRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success)
{
float[] orientation = new float[3];
SensorManager.GetOrientation(R, orientation);
azimut = orientation[0];
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
Toast.MakeText(Application.Context, azimut.ToString(), ToastLength.Short).Show();
});
}
}
}
Но я получаю только значение от акселерометра, но как я могу получить значение от акселерометра и магнитного поля
Какой уровень API вы пытаетесь протестировать? –
моей мин апи 15, но я проверить его на 23 –