I am trying to virtualize GPS device in android running in virtual-box. I want to facilitate an app running in android to use the gps device or get a fake gps location some how. I have started looking at the android emulator source code but meanwhile, any one has any good place to start with.
[UPDATE]
To make it a bit more clear, the whole application stack would be some thing like:
Android app (with gps functionality) to be tested ----> (developed by end user)
Android virtual machine ----> (Guest OS)
virtual box ---->
Microsoft Windows ---> Host OS
Now, Ideally I would like to have my application (like adb
or ddms
) running in Windows from where the user can set some coordinate values for gps device (in absence of actual GPS, else virtualize the existing GPS) . These coordinates would be available to the android app. Basically, this is same as providing the functionality of existing android emulator (using ddms) through virtual-box
running an android image.
I am NOT running android emulator and I am not using eclipse. :( [UPDATE ENDS]
This is how I did it (and a brief overview of how android stack (running on vbox) works).
An Android app having GPS functionality, registers some callbacks with the underlying implementation of user mode shared lib (typically libgps.so or libhardware_legacy.so) using JNI interfaces (as exported in frameworks/base/core/jni/android_location_GpsLocationProvider.cpp
)
These registered callbacks are responsible for notifying GPS fix (or change in gps location) to the app.
Here is a simple image demonstrating call flow from Android -> VBox -> Windows
Now to complete this flow, following steps were required:
I created my own libgps.so
exporting the function gps_get_interface
(declared in gps.h
), which is used by JNI interface. An app with gps support gets the latest coordinates returned from this libgps.so, which is actually loaded dynamically by the GpsLocationProvider service.
Create one more shared object using Vbox headers and containing routines to connect, disconnect, and send function request to the host service
, running in host OS. These are simple ioctl calls with pre-defined ioctl-numbers (eg: VBOX_GUEST_IOCTL_HGCM_CONNECT etc).
Create a host service (which actually is a DLL, loaded by VBox Host Service
) with can handle the parameters passed from HGCM calls and return the required data in a format that the shared object created in step 2 understands.
Now to send any fake/dummy location back to an app running inside Android VM
, you just need to notify the host service
, and the rest is communicated back to VBox Additions
-> libgps.so
-> GpsLocationProvider service
-> and finally to the App with GPS Support
.
Hope this will help some one trying some thing similar or help some one understanding how android stack works.
NOTE: Compiling the hgcm code and linking it with android lib is also a bit complicated because of the limited number of functions implemented in android libc (and some other libs as well). To avoid this all, you can directly open the device (/dev/vboxguest) and issue ioctl calls on it in the format that is known to vbox and your host service.
How about trying to use a mock location using setTestProviderLocation
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With