Looking to interface Android SensorManager

44 views
Skip to first unread message

tor

unread,
May 21, 2024, 12:07:47 PMMay 21
to Kivy users support
Greetings,

Via plyer I can access an assortment of sensors directly but is there a way to interface the higher level from python?

For example SensorManager#getSensorList.


Pointers to docs, examples or tutorials are most welcome...

Tomek CEDRO

unread,
May 21, 2024, 12:19:49 PMMay 21
to kivy-...@googlegroups.com
Plyer [1] is already the top level platform independent Python access
to hardware features.

If you want low-level talk to Android classes directly from Python try
PyJnius [2] :-)

[1] https://github.com/kivy/plyer
[2] https://github.com/kivy/pyjnius

--
CeDeROM, SQ7MHZ, http://www.tomek.cedro.info

tor

unread,
May 21, 2024, 2:27:03 PMMay 21
to Kivy users support
Thanks,

Ok, but there is no interfacing with for example SensorManager, right ?

I did look at pyjnius autoclass but frankly I'm not sure about how to proceed. There are some blogs that mentions this but every attempt gets me errors about class this and that not found.

Regarding java I'm really out of my depth and was hoping there would be no need for that...

Tomek CEDRO

unread,
May 21, 2024, 3:23:07 PMMay 21
to kivy-...@googlegroups.com
On Tue, May 21, 2024 at 8:27 PM tor <tommy.r...@gmail.com> wrote:
> Thanks,
> Ok, but there is no interfacing with for example SensorManager, right ?
> I did look at pyjnius autoclass but frankly I'm not sure about how to proceed. There are some blogs that mentions this but every attempt gets me errors about class this and that not found.
> Regarding java I'm really out of my depth and was hoping there would be no need for that...

Why do you need to access that directly?

For most general sensor use cases Plyer will do the job for you. If
something is not supported by Plyer or you want to to anything else
you need to use Java to call Android API just as it was native Android
application. If you want to call Java (Android API) from Python
Pyjnius is the wrapper that allows that.


Pyjnius github repo shows how to operate Java from within Python:

https://github.com/kivy/pyjnius


Documentation has an example on how to operate the accelerometer using
Sensor API. First you create a Java class that does what you want,
then you "autoclass" that and operate just like standard Python code
:-)

https://pyjnius.readthedocs.io/en/latest/android.html#accelerometer-access


Here are some reference on SensorManager:

https://josejuansanchez.org/android-sensors-overview/references/README.html


I would work on Java code in Android Studio in the first place, create
a class that does what you need, then when you make sure you get what
you want, autocalls that class into Python with Pyjnius :-)


This may sound complex at first, but you cannot run Java directly from
Python. You need to create a java class for a specific task first in
one file then that code gets wrapped into Python in another file. That
way it is possible to perform Java tasks with Python. Also you can
easily modify only the java source file when API changes, or you can
have separate java source files to perform specific tasks that you
will then wrap into Python classes and use them in your Kivy
application. This is quite elegant solution :-)

tor

unread,
May 25, 2024, 5:15:46 PMMay 25
to Kivy users support
Ok, unfortunately it is not clear to me why creating a java class of my own and interfacing it in python is different than interfacing google code direct.
Guess it shows that I'm out of my depth here...

The idea was to get a list of available sensors on the device, a no-go without java code then.

Tomek CEDRO

unread,
May 25, 2024, 5:55:11 PMMay 25
to kivy-...@googlegroups.com
On Sat, May 25, 2024 at 11:15 PM tor <tommy.r...@gmail.com> wrote:
> Ok, unfortunately it is not clear to me why creating a java class of my own and interfacing it in python is different than interfacing google code direct.

Because:

1. Android provides generic Java API as the native native programming
language that runs on Dalvik machine that then uses Linux kernel API
to talk to hardware. Developer would have to talk to either Linux
kernel drivers directly (which may not be even possible in Android by
a security design but also Linux kernel API changes with every minor
release so you would have to customize your application for every
possible Linux kernel version out there), or by a generic set of
functions and parameters (API) dedicated to Java that is well defined
for a given API number (new Android API may change sensor handling
function names and parameters because of new features for example).
Because most people write native applications in Java on Android thus
the (versioned) Android API dedicated for Java programming language.

2. Kivy uses Python that is compiled into a binary form with NDK that
is then launched by a generic Adnroid application wrapper to launch
Python then main.py all that follows is the Python runtime. If you
want to use "outside" Java calls from "inside" Python you need to
create a "bridge" with PyJnius. Think of this as using "os" or "sys"
module in Python that allows working with the OS interface (i.e. env
variables, launching applications, etc).


> Guess it shows that I'm out of my depth here...
> The idea was to get a list of available sensors on the device, a no-go without java code then.

Most popular sensors can be accessed with Plyer because someone before
created a set of functions (API) that makes calling Java Sensor API
functions from Python code. If you need a sensor that is not yet
supported probably you will have to extend the Plyer and share back
the result. Maybe someone from the Plyer users/developers could help
you with that.. maybe even implement the feature if it benefits the
whole community. Maybe the sensor list is already available in Plyer
(I have no experience yet sorry). Please try creating an Issue at
Plyer GitHub repo to find out :-)
Reply all
Reply to author
Forward
0 new messages