10

I need read about 100 samples per second off the accelerometer on a Android, and at a fixed sample rate. The current sensormanager does or offers neither.

I've read Need to read android sensors really fast

He seems to have access to the driver code which made his life easier.

I am curious if anyone has been able to do something like this through the NDK w/o that type of knowledge.

Thanks in advance!

5 Answers 5

8

I have taken a look at sensor.h, and the API looks very promising; however, a little howto on how to deal with the ASensorEventQueue and ASensorEventQueue_hasEvents and so forth would be -extremely- useful.

Ha! Update:

This looks a code example for what you want to do.

http://developer.android.com/reference/android/app/NativeActivity.html

1
  • it works, but using accelerometer keeps a wakelock and prevents suspend mode. Does it make sense ?
    – ransh
    Jan 14, 2017 at 11:26
3

Since Android SDK version 9 (for Android > 2.2) it seems to be possible to use the sensors via NDK.

See the STABLE_APIS.html file included in the docs. (Search for "sensor.h"). A mirror of that file is here.

1
  • it works, but using accelerometer keeps a wakelock and prevents suspend mode. Does it make sense ?
    – ransh
    Jan 14, 2017 at 11:36
2

Example from google https://github.com/googlesamples/android-ndk/tree/master/sensor-graph.Their code allow you controll SENSOR_REFRESH_RATE_HZ for the accelerometer.Written on c++ and connected to java code through the NDK.

2

I have recently worked on a pure C sensor application for android. Using NDK, you can access the sensors on the device without using Java. Check : https://github.com/PromiseMasango/sensor. Check : https://github.com/PromiseMasango/Android-NDK-sensor-example.

1
  • i noticed that you read only 50 events/samples, how can we change that to continuously read? i tried replacing it with infinite loop, but it blocks the main thread basically, and putting this loop in a code, i crashed my app on ALooper_pollAll: No looper for this thread! error. May 14, 2021 at 17:43
1

Sensors can be read reading directly from system "files" like:

/sys/class/sensors/light_sensor/lux or /sys/class/sensors/light_sensor/raw_data

I wonder if there is a more direct way to do it, reading directly from the chip.

Like something in /dev/...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.