I am building an app with kivy for a android device that have a built-in thermal printer. I already found the API that give me access to the printer and is written in java (so i am using pyjnius) but i cannot make it work. I've seen with a program called 'JarExplorer' that the constructor class in which i am interested needs an argument called 'android.content.context Context' and i don't know how to pass it correctly.
This is my python code with my attemps:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from jnius import autoclass
# Java Context class
Context = autoclass('android.content.Context')
# Java ThermalPrinter class
Printer = autoclass('com.telpo.tps550.api.printer.UsbThermalPrinter')
class Box(BoxLayout):
def construct_method1(self):
self.Printer = Printer()
def construct_method2(self):
self.Printer = Printer('android/content/Context')
def construct_method3(self):
self.Printer = Printer(Context())
def print_something(self):
# Open the printer
self.Printer.start()
# Specifying text
self.Printer.addString(self.ids.input.text)
# Print text
self.Printer.printString()
# Release the printer
self.Printer.stop()
class MainApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
MainApp().run()
With the Method 1 the logcat confirm that i need to pass android context:
06-02 20:41:29.688 28544 28925 I python : File "/home/ivo/Escritorio/Printer/.buildozer/android/app/main.py", line 14, in construct_method1
06-02 20:41:29.689 28544 28925 I python : File "jnius/jnius_export_class.pxi", line 256, in jnius.jnius.JavaClass.__init__
06-02 20:41:29.690 28544 28925 I python : File "jnius/jnius_export_class.pxi", line 294, in jnius.jnius.JavaClass.call_constructor
06-02 20:41:29.691 28544 28925 I python : jnius.jnius.JavaException: Invalid call, number of argument mismatch for constructor, available: ['(Landroid/content/Context;)V']
06-02 20:41:29.691 28544 28925 I python : Python for android ended.
Logcat with Method 2:
06-02 20:44:33.109 29084 29126 I python : File "jnius/jnius_export_class.pxi", line 256, in jnius.jnius.JavaClass.__init__
06-02 20:44:33.110 29084 29126 I python : File "jnius/jnius_export_class.pxi", line 326, in jnius.jnius.JavaClass.call_constructor
06-02 20:44:33.111 29084 29126 I python : File "jnius/jnius_conversion.pxi", line 109, in jnius.jnius.populate_args
06-02 20:44:33.112 29084 29126 I python : jnius.jnius.JavaException: Invalid python object for this argument. Want 'android/content/Context', got 'android/content/Context'
06-02 20:44:33.112 29084 29126 I python : Python for android ended.
Logcat of Method 3:
06-02 20:47:24.241 30838 30876 F rg.test.printe: java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: can't make objects of type android.content.Context: 0x6fdea688
And then the app close.
I know that the API should work because it comes with an demo app that can successfuly print but is written fully in Java.
Do you guys know whats wrong or is a deeper problem? I really appreciate your help and knowledge :) Thanks!