Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using able with a python-for-android service #27

Open
barrydisign opened this issue May 28, 2021 · 5 comments
Open

Using able with a python-for-android service #27

barrydisign opened this issue May 28, 2021 · 5 comments

Comments

@barrydisign
Copy link

We have created a Kivy application which spawns a service and we wish to run able within this. We tried running some existing working code but get

05-28 13:23:56.487  8004  8024 I python  :  jnius.jnius.JavaException: JVM exception occurred: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
05-28 13:23:56.487  8004  8024 I python  : Python for android ended.

Is this at all possible?
Many thanks

@b3b
Copy link
Owner

b3b commented May 30, 2021

Unfortunately, currently it is not possible to run able as a service, I have not tried it yet.

@jejmule
Copy link

jejmule commented Jul 6, 2021

Hi,
I am facing the same issue.
@barrydisign did you find a work around? I need it in a service to get streamed data via ble.

It is how the service is defined in the main app.

self.mActivity = autoclass(u'org.kivy.android.PythonActivity').mActivity
argument = ''
service.start(self.mActivity, argument)

and the complete log :

File "/home/jejmule/ISSULab/.buildozer/android/app/service.py", line 71, in
File "/home/jejmule/ISSULab/.buildozer/android/app/service.py", line 23, in init
File "/home/jejmule/ISSULab/.buildozer/android/app/arduinoBLE.py", line 31, in init
File "/home/jejmule/ISSULab/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/issulab/able/dispatcher.py", line 38, in init
File "/home/jejmule/ISSULab/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/issulab/able/android/dispatcher.py", line 60, in _set_ble_interface
File "jnius/jnius_export_class.pxi", line 256, in jnius.jnius.JavaClass.init
File "jnius/jnius_export_class.pxi", line 343, in jnius.jnius.JavaClass.call_constructor
File "jnius/jnius_utils.pxi", line 91, in jnius.jnius.check_exception

jnius.jnius.JavaException: JVM exception occurred: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference

@barrydisign
Copy link
Author

Sorry no fix or work around. We just altered the switch off time on the tablet so as to never sleep and not require a background service.

@jejmule
Copy link

jejmule commented Jul 7, 2021

Thank you for comment didi you use the wakelock option in the buildozer.spec file to keep the app alive?

# (bool) Indicate whether the screen should stay on
# Don't forget to add the WAKE_LOCK permission if you set this to True
#android.wakelock = False

I look into the code and the issue is coming from the BLE class defined in BLE.java.
on line 19 : import org.kivy.android.PythonActivity;
then in the constructor on line 40 the context is obtain from it
mContext = (Context) PythonActivity.mActivity;

able crashes on line 43 when calling mContext.getPackageManager()
Since the context is obtain from the PythonActivity and not the PythonService as a consequence the context is a null object and we obtain the following error :
jnius.jnius.JavaException: JVM exception occurred: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference

I will try to modify the BLE.java to work from service and not activity to check if my guess is correct....

b3b added a commit that referenced this issue Aug 8, 2021
b3b added a commit that referenced this issue Aug 15, 2021
b3b added a commit that referenced this issue Aug 15, 2021
@b3b
Copy link
Owner

b3b commented Aug 21, 2021

The ability to be used from a service context is added in 1.0.8.
Also, require_bluetooth_enabled and require_runtime_permissions decorators
are added to prepare adapter and permissions before service is started.

Usage example: https://herethere.me/able/examples.html#android-services

Future work should be done to add scan filters #25.
Without filters, scan stops when a device screen goes off.

b3b added a commit that referenced this issue Aug 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants