b3b / able

Python for Android Bluetooth Low Energy package
MIT License
39 stars 18 forks source link

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

Open barrydisign opened 3 years ago

barrydisign commented 3 years ago

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 commented 3 years ago

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

jejmule commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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.