Commit 6ab527dd authored by Vyacheslav Koscheev's avatar Vyacheslav Koscheev

Check if compass is started

parent 46035c5e
......@@ -65,6 +65,7 @@ JNI_LINKER_IMPL(QAndroidCompass, "ru/dublgis/androidcompass/OrientationProvider"
QAndroidCompass::QAndroidCompass(QObject * parent)
: QObject(parent)
, jniLinker_(new JniObjectLinker(this))
, started_(false)
{
}
......@@ -77,9 +78,9 @@ QAndroidCompass::~QAndroidCompass()
void QAndroidCompass::start(int32_t delayUs /*= -1*/, int32_t latencyUs /*= -1*/)
{
if (isJniReady())
if (!started_ && isJniReady())
{
jni()->callParamVoid("start", "II", static_cast<jint>(delayUs), static_cast<jint>(latencyUs));
started_ = jni()->callParamBoolean("start", "II", static_cast<jint>(delayUs), static_cast<jint>(latencyUs));
}
}
......@@ -89,10 +90,17 @@ void QAndroidCompass::stop()
if (isJniReady())
{
jni()->callVoid("stop");
started_ = false;
}
}
bool QAndroidCompass::isStarted() const
{
return started_;
}
float QAndroidCompass::getAzimuth()
{
float data = 0.f;
......
......@@ -54,6 +54,7 @@ public:
void start(int32_t delayUs = -1, int32_t latencyUs = -1);
void stop();
bool isStarted() const;
float getAzimuth();
......@@ -65,5 +66,8 @@ private:
private:
friend void JNICALL Java_onUpdate(JNIEnv * env, jobject, jlong inst);
private:
bool started_;
};
......@@ -85,17 +85,17 @@ public class OrientationProvider implements SensorEventListener {
}
public void start(int samplingPeriodUs, int maxReportLatencyUs) {
public boolean start(int samplingPeriodUs, int maxReportLatencyUs) {
Log.i(TAG, "start");
if (null == mSensorManager) {
Log.e(TAG, "Sensor manager is null");
return;
return false;
}
if (mRegistered) {
Log.w(TAG, "Listeners already registered");
return;
return false;
}
/* API 9, not 19. That is not a mistake. */
......@@ -104,22 +104,26 @@ public class OrientationProvider implements SensorEventListener {
maxReportLatencyUs = SensorManager.SENSOR_DELAY_UI;
}
mRegistered = true;
try {
if (android.os.Build.VERSION.SDK_INT >= 19 && maxReportLatencyUs > 0) {
mSensorManager.registerListener(this, mAccelerometer, samplingPeriodUs, maxReportLatencyUs);
mSensorManager.registerListener(this, mMagnetometer, samplingPeriodUs, maxReportLatencyUs);
mRegistered = mRegistered && mSensorManager.registerListener(this, mAccelerometer, samplingPeriodUs, maxReportLatencyUs);
mRegistered = mRegistered && mSensorManager.registerListener(this, mMagnetometer, samplingPeriodUs, maxReportLatencyUs);
}
else {
mSensorManager.registerListener(this, mAccelerometer, samplingPeriodUs);
mSensorManager.registerListener(this, mMagnetometer, samplingPeriodUs);
mRegistered = mRegistered && mSensorManager.registerListener(this, mAccelerometer, samplingPeriodUs);
mRegistered = mRegistered && mSensorManager.registerListener(this, mMagnetometer, samplingPeriodUs);
}
mRegistered = true;
Log.i(TAG, "Sensor listener registered successfully with samplingPeriodUs = " + samplingPeriodUs);
}
catch(Throwable e) {
Log.e(TAG, "Failed to register listeners", e);
mRegistered = false;
}
return mRegistered;
}
......@@ -131,11 +135,6 @@ public class OrientationProvider implements SensorEventListener {
return;
}
if (!mRegistered) {
Log.w(TAG, "Listener is not registered");
return;
}
try {
Log.i(TAG, "Unregistering listener");
// to stop the listener and save battery
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment