Is there a way to get data from sensors from command line?

Viewed 348

In Android, I know it's possible to use Runtime.getRuntime().exec(...) to execute native command line on android system like echo or ls. I wonder if is there is any way to get data from any sensor module (like photo or gps) not from Android API (through Java or Kotlin), but by executing a command line with Runtime.getRuntime().exec(...). Is there a way to do it?

2 Answers

Technically all that the Android framework ( + HAL ) does is communicate via system calls with the kernel.

It would certainly be possible for you to write a binary ( C/C++) that does that communication for you, bypassing the framework. And then you could call that binary with Runtime.getRuntime().exec(...) ( assuming rooted and have access ).

There aren't many tools to access the sensors like that ( expect maybe some vendors might have for testing). The only thing that comes to mind that you could use to get some information is by calling dumpsys in shell. This will give you lots of info about the current state of the system, and for example some location data as explaind in this answer

You can pack binary executables into your apk and launch them via Runtime.getRuntime().exec(...). Depending on the data you want to read it may be possible to implement an C/C++ program which reads /proc or /dev. If you completely statically link this executable (use i.e. musl libc) you can call it from your android app to read the data you need.

Related