SensorManager.getRotationMatrix always return false on Android Emulator -
i try orientation on android emulator sensormanager.getorientation
. before use sensormanager.getrotationmatrix
rotation matrix, return false
. here code of activity:
public class mainactivity extends activity implements surfaceholder.callback, view.onclicklistener{ public final static string debug_tag = "mainactivity"; private sensormanager sensormanager; private surfaceview preview; private float[] mgravs; private float[] mgeomags; private float[] mrotationm; private float[] morientation; private sensor maccelerometer; private sensor mmagneticfield; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().addflags(windowmanager.layoutparams.flag_fullscreen); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); preview = (surfaceview)findviewbyid(r.id.surfaceview1); sensormanager = (sensormanager)getsystemservice(sensor_service); maccelerometer = sensormanager.getdefaultsensor(sensor.type_accelerometer); mmagneticfield = sensormanager.getdefaultsensor(sensor.type_magnetic_field); mgravs = new float[3]; mgeomags = new float[3]; mrotationm = new float[16]; morientation = new float[3]; list<sensor> sensorlist = sensormanager.getsensorlist(sensor.type_all); (int ni = 0; ni < sensorlist.size(); ni++) { log.d(debug_tag, sensorlist.get(ni).getname()); } } private sensoreventlistener sensoreventlistener = new sensoreventlistener() { @override public void onsensorchanged(sensorevent event) { synchronized (this) { switch (event.sensor.gettype()) { case sensor.type_accelerometer: system.arraycopy(event.values, 0, mgravs, 0, 3); break; case sensor.type_magnetic_field: system.arraycopy(event.values, 0, mgeomags, 0, 3); break; default: return; } if (mgravs != null && mgeomags != null) { boolean bsuccess = sensormanager.getrotationmatrix(mrotationm, null, mgravs, mgeomags); if (bsuccess) { sensormanager.getorientation(mrotationm, morientation); } } } } @override public void onaccuracychanged(sensor sensor, int accuracy) { // todo auto-generated method stub } }; private boolean openhardware() { sensormanager.registerlistener(sensoreventlistener, maccelerometer, sensormanager.sensor_delay_normal); sensormanager.registerlistener(sensoreventlistener, mmagneticfield, sensormanager.sensor_delay_normal); return true; } private void closehardware() { sensormanager.unregisterlistener(sensoreventlistener); } @override protected void onresume() { super.onresume(); openhardware(); } @override protected void onpause() { super.onpause(); closehardware(); } @override protected void onstop() { closehardware(); super.onstop(); } }
ok, problem occur because magnetic field sensor return [0.0, 0.0, 0.0]. how can make magnetic field sensor return else?
i had same problem. got false
this answer has helped me: https://stackoverflow.com/a/14224122/1549127
Comments
Post a Comment