Get GPS coordinates from an Android via a terminal? - android

How can I get GPS coordinates from an Android device via a terminal?
I've searched everywhere and all I've found is Java code for apps, but not a decent method by just using a plain shell. I'm not interested in passive geo-location that uses nearby cell towers and wifi endpoints, but a method that uses the built-in GPS on a device. Is this even possible? Oh and side question:
Is it possible to toggle GPS via a terminal when rooted without any user confirmation?
Edit:
I found a way to execute what I want properly. Using /system/bin/settings, I can toggle GPS itself without user confirmation. After that, I can run a binary I made with Android NDK, and using JNI I can request the users current location.

You can get latest GPS location using this adb command
adb shell dumpsys location

Related

Custom Location Services with external GPS connected to Raspberry Pi with android things

Hello I am wanting to connect an external GPS unit via I2C or serial to a raspberry pi 3 running android things to have this GPS unit provide location data.
So far using the Rasbarian OS I have a working python script with custom libraries. The script outputs raw NMEA data.
I am wondering how would I need to run this script to be able to have Android Location services use this data for location services? Would I need to create an app using the location services API? Or since this is a locally installed device would I need to write firmware or some kind? I am just wondering if someone could point me in the right direction of what kind of software I would need to write (an app or driver or firmware) in order to have this NMEA data accessible for location services.
Example of GPS driver connected via serial port you can find in Android Things user-space GPS driver and how to use it you can find in UART GPS sample for Android Things. Also take a look at Official Documentation and examples like this.

Working with GPS via libhardware on Android

In AOSP there is libhardare library which contains interface to the GPS driver - gps.h. Using this interface you can get access to GPS driver on low level (C++), turn on GPS and get current location of the device.
On the Internet there are several projects, that use this GPS interface. For example, this one created by kanru. This code works almost perfectly - I could connect to GPS driver and get correct location.
But I ran into a problem. If you turn on GPS driver using kanru project and after awhile turn on GPS via standard method (in GUI), then C++ program will suddenly stop receive information from GPS driver. At the same time in GUI you can see that GPS is On and you can start Google Maps or any other GPS-related application and get your coordinates.
I tried to restart GpsInterface, turn off and on GPS driver via hw_device_t pointer, even restart whole C++ executable - no effect.
After restart of the program GPS driver change status to GPS_STATUS_ENGINE_ON and then - silence.
Also I tried to change the order of actions. I turned on GPS in GUI, launched Google Maps and waited several minutes till I got my location. Then I started my C++ program. It successfully connected to GPS driver and started to get location data. Meanwhile GUI "lost" connection with GPS driver.
I tried to test my program on several devices with Android 4.2 and 5.1 and get same problem on the most of them. Only on Samsung S6 with Android 5.1 my program was able to work perfectly under all conditions.
It seems like on some devices GPS driver configured so that it can work with only one data consumer.
Perhaps someone faced a similar problem?

Controlling GPS operations using adb shell - turning on/seeing that GPS actually get a location

Is there a way, using adb shell commands, to:
Turn on GPS on the phone (and not to just toggle-on GPS, but to actually activate the sensor to retrieve location)
Check to see if GPS sensor actually retrieves (correct) location
I am writing a python script, passing adb shell commands through an internal app, and am having trouble checking to see if the GPS sensors check out on these phones. In a nutshell, I am testing the functionality of the GPS on the phones.
Once I have a string variable with the GPS data, I am planning on using regex to parse it and actually find latitude/longitude coordinates. Right now I can turn on the GPS sensor but, for whatever reason, these phones (factory reset) do not connect to the satellite through this sensor. They can see their location if you connect them through WiFi or the mobile network, but when both are turned off GPS seems to go quiet. In order for the GPS to actually read something, the workaround we ended up using was to download a 3rd party GPS app. Only then could the phones connect and see their own location.

Shell script to send gps coordinates to emulator?

I woule need a bunch of gps coordinates for test data. I thought about if it's possible to use adb shell for creating a quick shell script which generates a range of gps coordinates in a for loop.
But I still don't figured out how to start gps service in shell like gps fix in telnet.
Thanks for your help.
Regards
You could do the scripting outside the device. tellandroid is a recently-released Python utility that exposes Android Debug Console operations to your development computer's command line, so you can run:
tellandroid geo set [--host=<host>] [--port=<port>] [--] <lat> <long>
and such. Then, your loop would be on your development machine (e.g., as part of a test suite) rather than trying to do it on-device.
You can send gps coordinates directly from the eclipse IDE, change the perspective to DDMS.
Best regards

How to send "geo fix" command to real device of Android?

First,thank you for your attention to my question
I'm planning to design an Android GPS locating application
here are my difficulties and situations
(1)my computer hardware has been out of date for a long time and running an android emulator for debugging is really time-costing,thus i bought a real device(android cellphone) one week ago,in the aim of replacing emulator during debugging
(2)i know how to send location datas to an emulator via "DDMS/Emulator Control/Location Controls",but it is disabled for a real device
(3)i am programming indoor,thus using physical gps locating function is not acceptable(even if i turn on "a-gps")
(4)i have found another topic here: Android mock location on device?
however,i can't open the detail links,like "Andrews link"," http://dehuysser.appspot.com/blog/android-developer-shell-released"
I guess these web sites are forbidden by my country......
Here are my questions:
(A)i have found some codes of making mock loation datas ,but these codes can not activate "onLocationChanged" function
However, i wonder if there is a way to send locating datas from PC to real device via USB port(i'm in USB debugging mode), just like sending locations from DDMS to emulator?
(B)i've got the information that, it is possible to send "geo fix" command to emulator as well as to a real device, by "telnet localhost ", but how to check out the port number occupied by my real device?
i know that 5554 port is used for a emulator,but what about the real device?
i've tried to command "netstat" in terminal, but it's a shame to say that i don't know which port is for my android phone......
(C)If my (A)(B) question is not practical,then what's "settings/applications/development/allow mock location" used for?
Please help me, and thank you for your answer!
I have wrote an app exactly for this (this functionality is not available on phones by default) - it's open source and provides exactly the same interface as the emulator (telnet to :5554 and run "geo fix ..." or "geo nmea"): in app store MockGeoFix
The easiest way of using mock data is using the mock test location provider API. It's at the bottom of the documentation page. I've tried it out and location is received in the onLocationChanged callback. Here is some code that you can try out.
The port used by adb is usually 5037.

Categories

Resources