is it possible to access the sensors without a Native Activity?

3,992 views
Skip to first unread message

Claudio Veas

unread,
Aug 8, 2011, 10:42:17 AM8/8/11
to android-ndk
Hello My question is simple, Im trying to access the sensors throught
C/C++ using the NDK. First let me say that Im totally new to the NDK
development but reading many entries of this group and some
documentation I have not found a way to access the sensors without
being part of a Native Activity. Even the example that comes with the
NDK is accessing the sensors with the Native Activity. The problem
that I have is that I ve been asked to develop a C++ library that
interfaces with another library developed in C++ that will ask about
the sensors state and I cant use the Native Activity
Thanks in Advance for your responses
Claudio Veas

David Turner

unread,
Aug 12, 2011, 10:19:50 AM8/12/11
to andro...@googlegroups.com
You will have to use JNI to ask the framework for the information you're looking for.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Claudio Veas

unread,
Aug 12, 2011, 10:23:48 AM8/12/11
to andro...@googlegroups.com
Thanks David for your response. Im sorry I dont know what you mean by "framework" could you pls rephrase that for me. Sorry for the inconvenience and thanks again

David Turner

unread,
Aug 12, 2011, 12:32:55 PM8/12/11
to andro...@googlegroups.com
On Fri, Aug 12, 2011 at 4:23 PM, Claudio Veas <claudi...@tallertechnologies.com> wrote:
Thanks David for your response. Im sorry I dont know what you mean by "framework" could you pls rephrase that for me. Sorry for the inconvenience and thanks again

The framework is part of the system that runs in the VM. You will have to use JNI to call its functions from native code and get the information you need.
This presumes that you're actually running an Android application, not a standalone executable.

Dianne Hackborn

unread,
Aug 12, 2011, 2:33:31 PM8/12/11
to andro...@googlegroups.com
You don't need to use a NativeActivity.  The first function in android/sensor.h:

/*
 * Get a reference to the sensor manager. ASensorManager is a singleton.
 *
 * Example:
 *
 *     ASensorManager* sensorManager = ASensorManager_getInstance();
 *
 */
ASensorManager* ASensorManager_getInstance();

No NativeActivity anywhere.
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Claudio Veas

unread,
Aug 15, 2011, 10:49:11 AM8/15/11
to andro...@googlegroups.com
Thanks for the Answer Dianne. Anyway I have a little doubt looking at the Native Activity code. It seems for what you say in this thread http://groups.google.com/group/android-ndk/browse_thread/thread/fa9aa6005b3d5dc9/c1978b4a089d1378?hl=es&lnk=gst&q=native+activity+input#c1978b4a089d1378 that I can have access to the sensor Manager, but in order to get sensors readings I suppose I need to create a SensorEventQueue. In order to get that, I need the ALooper structure that comes as a parameter in the method void android_main of the Native Activity, so basically the question here is can I get the sensor reading without the SensorEventQueue? and if you cant....Can I create a looper object of my own  (for what I understand of the above thread is that its not possible) ? 
Thanks in Advance and pls excuse my poor english

Dianne Hackborn

unread,
Aug 15, 2011, 4:14:38 PM8/15/11
to andro...@googlegroups.com
Are you looking at the header files at all?  At the top of looper.h:

/**

 * Returns the looper associated with the calling thread, or NULL if

 * there is not one.

 */

ALooper* ALooper_forThread();


enum {

    /**

     * Option for ALooper_prepare: this looper will accept calls to

     * ALooper_addFd() that do not have a callback (that is provide NULL

     * for the callback).  In this case the caller of ALooper_pollOnce()

     * or ALooper_pollAll() MUST check the return from these functions to

     * discover when data is available on such fds and process it.

     */

    ALOOPER_PREPARE_ALLOW_NON_CALLBACKS = 1<<0

};


/**

 * Prepares a looper associated with the calling thread, and returns it.

 * If the thread already has a looper, it is returned.  Otherwise, a new

 * one is created, associated with the thread, and returned.

 *

 * The opts may be ALOOPER_PREPARE_ALLOW_NON_CALLBACKS or 0.

 */

ALooper* ALooper_prepare(int opts);

Claudio Veas

unread,
Aug 15, 2011, 4:21:25 PM8/15/11
to andro...@googlegroups.com
Thanks for your reply! sorry for my inexperience with C++ code but this is my first ndk project (and c++ project) and Im not sure about where to look...

dolpal kim

unread,
Mar 8, 2012, 2:59:23 AM3/8/12
to andro...@googlegroups.com
Do you solve this problem?

I'm doing to try this code, but I can't...
build and install apk are success job
I run my app, but i can't do well
Issues is "E/Looper  ( 8763): Invalid attempt to set NULL callback but not allowed for this looper" (logcat)

anyone show me correct code, please.

my source is

TestActivity.java

@Override

    protected void onResume() {

   

    // TODO Auto-generated method stub

    super.onResume();

    mainRun(); //native method

    }



jni.c

void Java_com_example_test_jni_TestJNIActivity_ mainRun(JNIEnv* env, jclass clazz) {

    ASensorManager* sensorManager = ASensorManager_getInstance();


    ALooper* looper = ALooper_forThread();

    if(looper == NULL)

        looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);


    ASensorRef accelerometerSensor = ASensorManager_getDefaultSensor(sensorManager,ASENSOR_TYPE_ACCELEROMETER);

    LOGI("accelerometerSensor: %s, vendor: %s", ASensor_getName(accelerometerSensor), ASensor_getVendor(accelerometerSensor));


    ASensorEventQueue* queue = ASensorManager_createEventQueue(sensorManager, looper, LOOPER_ID, NULL, NULL);


    ASensorEventQueue_enableSensor(queue, accelerometerSensor);

    ASensorEventQueue_setEventRate(queue, accelerometerSensor, (1000L/SAMP_PER_SEC)*1000);


    int ident;//identifier

    int events;

    while (1) {

        while ((ident=ALooper_pollAll(-1, NULL, &events, NULL) >= 0)) {

            // If a sensor has data, process it now.

            if (ident == LOOPER_ID) {

                ASensorEvent event;

                while (ASensorEventQueue_getEvents(queue, &event, 1) > 0) {

                    LOGI("accelerometer X = %f y = %f z=%f ", event.acceleration.x, event.acceleration.y, event.acceleration.z);

                }

            }

        }

    }

}



2011년 8월 8일 월요일 오후 11시 42분 17초 UTC+9, Claudio Veas 님의 말:

Sensor Platforms

unread,
Mar 12, 2012, 2:16:44 AM3/12/12
to andro...@googlegroups.com
Your code would work with one change: you need to specify a callback to handle the sensor event.  For example:

ASensorEventQueue* queue =
      ASensorManager_createEventQueue(sensorManager,
                                      looper, LOOPER_ID_USER,
                                      get_sensor_events, sensor_data);


The callback method here is "get_sensor_events" and needs to have the following format (see looper.h):

typedef int (*ALooper_callbackFunc)(int fd, int events, void* data);


For example:

static int get_sensor_events(int fd, int events, void* data) {
  ASensorEvent event;

  while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) {
    if(event.type==ASENSOR_TYPE_ACCELEROMETER) {
            LOGI("accl(x,y,z,t): %f %f %f %lld",                                                                                                                                               
                 event.acceleration.x, event.acceleration.y,                                                                                                                                   
                 event.acceleration.z, event.timestamp);                                                                                                                                       
    }

  //should return 1 to continue receiving callbacks, or 0 to unregister                                                                                                                          
  return 1;
}


There is more background detailed here: http://www.sensorplatforms.com/native-sampling
Message has been deleted

dolpal kim

unread,
Mar 13, 2012, 7:57:20 PM3/13/12
to android-ndk
Thank You sir~ i got it.

On Mar 12, 1:36 pm, Sensor Platforms <sensorplatfo...@gmail.com>
wrote:
> Your code would work with one change: you need to specify a callback
> to handle the sensor event.  For example:
>
> ASensorEventQueue* queue =
>       ASensorManager_createEventQueue(sensorManager,
>                                       looper, LOOPER_ID_USER,
>                                       get_sensor_events, sensor_data);
>
> The callback method here is "get_sensor_events" and needs to have the
> following format (see looper.h):
>
> typedef int (*ALooper_callbackFunc)(int fd, int events, void* data);
>
> For example:
>
> static int get_sensor_events(int fd, int events, void* data) {
>   ASensorEvent event;
>
>   while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0)
> {
>     if(event.type==ASENSOR_TYPE_ACCELEROMETER) {
>             LOGI("accl(x,y,z,t): %f %f %f
> %lld",
>                  event.acceleration.x,
> event.acceleration.y,
>                  event.acceleration.z,
> event.timestamp);
>     }
>
>   //should return 1 to continue receiving callbacks, or 0 to
> unregister
>   return 1;
>
> }
>
> More information available here:http://www.sensorplatforms.com/native-sampling
>
> On Mar 8, 12:59 am, dolpal kim <minoss...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Do you solve this problem?
>
> > I'm doing to try this code, but I can't...
> > build and install apk are success job
> > I run my app, but i can't do well
> > Issues is "E/Looper  ( 8763): Invalid attempt to set NULL callback but not
> > allowed for this looper" (logcat)
>
> > anyone show me correct code, please.
>
> > my source is
>
> > *TestActivity.java*
>
> > @Override
>
> >     protected void onResume() {
>
> >     // TODO Auto-generated method stub
>
> >     super.onResume();
>
> >     mainRun(); //native method
>
> >     }
>
> > *jni.c*

GoG

unread,
Jan 29, 2013, 8:46:44 AM1/29/13
to andro...@googlegroups.com
Hello Claudio, Dianne,

Did you finally manage to resolve sensor access without Java nor Native Activity ?
Because I have to admit I'm stuck :/

It works great using SensorPlatforms method or Google one if you are in an Android application (native or not) :
- http://developer.android.com/reference/android/app/NativeActivity.html
- http://www.sensorplatforms.com/native-sampling/

But what I want to do is (like Claudio I think) access sensor from my pure native Linux application.
It compiles, links and runs without any error or warning but never receive any event in callback (see SensorPlatform method)
nor from 'ALooper_pollAll' (see Google Android doc).

I checked twice but did not commit any coding error listed in this forum about 'ALooper' nor 'createEventQueue'
(see 'ALooper_forThread', 'ALooper_prepare' and 'ASensorManager_createEventQueue').
I also checked NDK function return values but none was 'NULL'.

In short, it's weird the same code works in my Android NDK application but not in my native Linux one...


Does anyone have a clue about how to make it works ? Is it possible at all or sensor access is limited to Android apps ?


Thanks,
GoG
Message has been deleted

Vitush

unread,
Oct 10, 2013, 2:56:48 AM10/10/13
to andro...@googlegroups.com, claudi...@tallertechnologies.com
Hi GoG, 

I'm trying to do the same thing: access the sensor code from a pure native linux application. 
Did anyone manage to do that? 

Thanks!
Message has been deleted

mojint...@gmail.com

unread,
Mar 17, 2015, 3:37:21 AM3/17/15
to andro...@googlegroups.com
Hi, did anyone solve the GoG's question???

在 2013年1月29日星期二 UTC+8下午9:46:44,GoG写道:

s.rawat

unread,
Mar 19, 2015, 10:17:40 AM3/19/15
to android-ndk
Hello All, 
This is interesting.Just wondering if some is able to write a pure c/c++ code to access the sensor data .
Nevertheless there is an app in the linux kernel  -> http://lxr.free-electrons.com/source/drivers/staging/iio/Documentation/generic_buffer.c .This will give you the data from all the sensors.But there is a dependency : 
-> sensor must be IIO sensors
I have tested this on a platform running linux kernel >3.15.
Any update on the original Qs solution?
If you need more info on the link I have posted I can assist.


To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages