Skip to content

Qt sensors just got better on Android A rewrite with huge performance gains for the Android sensors plug-in

Each month KDAB schedules time for me to maintain the Qt for Android port. Usually I use it to review the pending patches or fix bugs, but sometimes there is a quiet month and I have time to add new functionalities or improve the existing ones.

April was quite quiet so I decided to rewrite the Android sensors plugin (no, this is not a late April fool’s joke).

Why on earth did I want to do that? Why try to fix something that works? After all, the current implementation faithfully served us for years!

I decided to rewrite it after I did some performance tests that showed some surprising numbers. When I used the NDK API to get sensors data, the CPU usage dropped from over 60% to even less than 20% (it fluctuates from 15% up to 30%)! Yup, that’s a surprising performance improvement, which TBH I hadn’t expected at all!

I expected it to be faster, as the NDK API doesn’t need to move the data to JAVA and then back to C/C++ world (via JNI) but, I never dreamed it would be that fast!

Are you wondering why we didn’t use the NDK API in the first place? It’s because the sensors NDK API was not introduced until API-16, which means that when we first implemented the sensors API, we were supporting Android from API-9. However, starting with Qt 5.9, the minimum API has been raised to API-16, so, now I can safely use NDK API for this job.

Here you can find the pending patch. It targets dev branch which will be part of Qt 5.14.

7 thoughts on “Qt sensors just got better on Android”

  1. The linked patch is on the 5.12 branch … So the change will be in Qt 5.12.4 / 5.13.x if I am right?

  2. Not really a surprise… using native code is always faster than using a high level language like JAVA. It was like that already in the good old BASIC days.

    So sad that you were not able to use the NDK at API-9 in the first place, but good news that the code was finally updated. Hoping eagerly for more. Great work! 🙂

  3. Nice! good jobs, this will safe some battery! I wish they get ride of the SDK and expose everything into the NDK. Ditch that slow Java once and for all, ok I’m just dreaming but a new Android alternative with the Java mess would be so great!

  4. This is great news, but my beef with sensors was they are difficult to serialize. I wrote a lot of code to do that, in case someone wants to persist sensor readings.

    Can you add serialization? QDatastream would be fine.

Leave a Reply

Your email address will not be published. Required fields are marked *