Calling Java (Android) function inside a hybrid mobile application with Javascript

Asked

Viewed 218 times

1

We are building a hybrid web application, which needs to print on a portable printer by bluetooth. But the printer is imported and only comes with a Java SDK, which is not our strong!

We needed to print from Javascript code, but I don’t know if I can call the Java functions printing or something like that. And the alternative would be to create a plugin, which we are already working on but will take a long time.

It is a mobile project, will be created a .apk and is totally offline.

  • What is possible to do: Make a function in javascript, make an ajax request, execute a function in java (within java), and in javascript, capture the event, data, etc... of this execution (within java). here’s an explanation

  • Have you tried this plugin? https://github.com/eddysby2000/Cordova-Bluetooth-Printer-Plugin

1 answer

1

There is no direct way to do this because javascript will not have an API available in the browser capable of talking directly to the JDK.

However, javascript can easily request via Ajax, which uses HTTP. Then you create a Java application, which provides a REST API and publishes it on any IP:port. The javascript application communicates with the java application using REST. The java application, in turn, cares about talking to the printer.

  • In our case, the application will be used on a device with android, totally offline, to issue tickets, and compiled in a .apk. I can build this structure anyway?

  • @Rodrigo As your application is an APK on Android and not in the browser and is totally offline, the thing changes totally figure (and my answer turns out to be invalid). In this case, I don’t know what the answer would be, but I’m pretty sure it exists and it’s not that hard. Basically, your problem is reduced to implementing an APK on Android where part of the code is in java and part in javascript.

  • I imagine this @Victor, the question is how, at least the way!

  • offline, you can create a small database with a arquivo.json, can use jquery with method $.load( 'arquivo.json', function(data){ ... }); nor need java for this, can only name the oxen, for the files...

  • Or to suffer less, use the Ionic

Browser other questions tagged

You are not signed in. Login or sign up in order to post.