嗨,我有一个很大的问题,从多个传感器获取数据 我的注册和读取数据代码是:
[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();
});
}
}
}
但是我只获得了Accelerometer的值,但是如何从Accelerometer和MagneticField中获取值
答案 0 :(得分:0)
不确定API 23上的权限,但在我的旧手机上,我收到了两个通知:
public class MainActivity : FormsApplicationActivity, ISensorEventListener
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var mSensorManager = (SensorManager)Application.Context.GetSystemService(Context.SensorService);
var mAzimutMagneticField = mSensorManager.GetDefaultSensor(SensorType.MagneticField);
var mAzimutAccelerometer = mSensorManager.GetDefaultSensor(SensorType.Accelerometer);
if (mAzimutMagneticField != null)
mSensorManager.RegisterListener(this, mAzimutMagneticField, SensorDelay.Fastest);
if (mAzimutMagneticField != null)
mSensorManager.RegisterListener(this, mAzimutAccelerometer, SensorDelay.Fastest);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
{
}
public void OnSensorChanged(SensorEvent e)
{
if (e.Sensor.Type == SensorType.MagneticField)
{
var magn = e.Values[0];
}
if (e.Sensor.Type == SensorType.Accelerometer)
{
var acc = e.Values[0];
}
}
}
答案 1 :(得分:0)
好的,我有解决方案 旧代码:
mSensorManager2.RegisterListener(az, mAzimutMagneticField, SensorDelay.Game);
mSensorManager2.RegisterListener(az, mAzimutAccelerometer, SensorDelay.Fastest);
新代码:
mSensorManager2.RegisterListener(az,(Sensor) mAzimutMagneticField, SensorDelay.Game);
mSensorManager2.RegisterListener(az,(Sensor)mAzimutAccelerometer, SensorDelay.Fastest);
我不知道为什么传感器需要投射到传感器,但它适用于我