rexroni
rexroni

Reputation: 443

Access GPS on Android from C++

I have an Android app that serves as a GUI to a little daemon (written in C++) that needs to run on an variety of mobile/embedded linux devices. The daemon needs to collect GPS data and pass it to several clients (android phones, each with a GUI). The daemon needs to be capable of running on one of the same Android devices that is being used as the GUI, but I'm having a lot of trouble accessing GPS data from the C++ daemon.

I've looked into two options so far:

1) The "stay native" method: I've heard that gpsd exists for Android (such as here http://esr.ibiblio.org/?p=4886), but it seems elusive and/or not existent on my Samsung Galaxy SII, running Cyanogenmod 10.1.3-i9100 (Android 4.2.2). My standalone toolchain built from the NDK doesn't seem to have anything gps-related at all, even though sites like this one (http://www.jayway.com/2010/01/25/boosting-android-performance-using-jni/) indicate that java uses JNI wrappers to use C code to talk to the GPS anyway.

2) The jni method: GPS seems really easy in Android Java applications, so I started looking into the JNI (I'm pretty new to Android and Java, by the way). It is supposed to proved a way for C code and Java code to interact, right? I read up on it at this site (http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html) and this site (http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html) and a bunch of others. But it occurs to me I haven't seen any C code using the JNI that has a main() function in it. Also, the JNI_CreateJavaVM() function is commented out in the jni.h header file of my NDK toolchain. In fact, I can't figure out how to have a valid JNIEnv* in the first place. The conclusion I've come to is that JNI code is meant to be used by Java applications that need C support, not C applications that need Java support. Is that correct?

And then I have a third thought, which I don't like very much:

3) the "backup" method: In the instances where the C++ daemon is running on an Android phone, perhaps it could ask the android GUI for GPS data, then broadcast that to the other clients? I'm thinking they might communicate through a socket or something simple. This seems like a really ugly solution though, because on top of seeming inefficient, the daemon should be able to run independent of any GUI, but now it would depend on the GUI for GPS data.

So my real question is, has anybody else ran into this problem and found a suitable answer? Or perhaps there is something I am not understanding something aobut gpsd in Android, or about JNI in Android?

Thanks for reading.

Upvotes: 11

Views: 9061

Answers (1)

iamantony
iamantony

Reputation: 1395

I would recommend you to try to use this code: Android GPS using libhardware.

This small project use libhardware library from AOSP. See gps.h for more information about how to use GPS abstraction interface. Also worth a look GpsLocationProvider.java and com_android_server_location_GpsLocationProvider.cpp.

Upvotes: 4

Related Questions