1

I am making a Bluetooth Android App using Kivy. How can I use pyjnius to import my own java class to my python code? But not the build in java class in Android such as 'android.bluetooth.BluetoothAdapter'.

Here is my the error I got. I used buildozer to deploy and run the App on Android phone.

07-29 08:38:40.035   601  2007 I python  : Android kivy bootstrap done. __name__ is __main__
07-29 08:38:40.035   601  2007 I python  : AND: Ran string
07-29 08:38:40.035   601  2007 I python  : Run user program, change dir and execute entrypoint
07-29 08:38:40.450   601  2007 I python  : Traceback (most recent call last):
07-29 08:38:40.450   601  2007 I python  :   File "/home/kali/buildozer/.buildozer/android/app/main.py", line 32, in <module>
07-29 08:38:40.451   601  2007 I python  :   File "/home/kali/buildozer/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/remote_app/jnius/reflect.py", line 208, in autoclass
07-29 08:38:40.451   601  2007 I python  :   File "jnius/jnius_export_func.pxi", line 28, in jnius.jnius.find_javaclass
07-29 08:38:40.451   601  2007 I python  : jnius.jnius.JavaException: Class not found b'BluetoothGattImplem'
07-29 08:38:40.451   601  2007 I python  : Python for android ended.

BluetoothGattImplem.java is the file I have and wanted to import, there is only one class in it also called BluetoothGattImplem. I put the .java file under the same dictionary with my main.py, and call autocalss function from jnius in main.py like this:

from jnius import autoclass
BluetoothGattImplem = autoclass('BluetoothGattImplem')

I tried to search for answer but still can't figure out some points.

  1. Where should I put my BluetoothGattImplem.java and what should be the path in jnius.autoclass(path) I enter? As I am running the codes on Android I think I should enter the path where this app run on Android but not path on PC. However, I don't know where this app main.py is running on Android, buildozer deploy and run this app automatically.
  2. I saw some answer suggested use jnius_config.set_classpath(absoulte_path/to/my_app) to set the path. Again, what path should I enter? I know where the .java file in my PC system but I dont know where it is in Android system.
  3. What file type should I use for the import? I have the BluetoothGattImplem.java file but I saw someone use .class and some use .jar.

I am also looking for a complete example on how to make a bluetooth android app using kivy and buildozer, because it seem that the bluetooth function on android is not well supported by kivy, and there are no simple way to do it.

Thanks for answering my question, I am really confuse about it, appreciate for any hint or suggestion.

Kelvin
  • 11
  • 1

1 Answers1

0

I can partially answer some of the points.

  1. Compile your java code and put the jar in some path.
  2. Use jnius_config.add_classpath("path/to/jars/*"). This needs to be defined before any autoclass takes place.
  3. You import the classpath, for example BluetoothGattImplem = autoclass("com.something.BluetoothGattImplem").
SamuelNLP
  • 4,038
  • 9
  • 59
  • 102