如何从android webview推送打印到蓝牙热敏打印机?

16 printing android bluetooth-printing

我们在云服务器和桌面上配置了Odoo POS应用程序,如果我们点击打印收据,它将自动弹出chrome中的打印对话框,并可以从连接的设备打印!我们在android中开发的相同应用程序只需输入webView!

现在我需要使用与Android设备配对的连接蓝牙热敏打印机进行打印!我搜索了很多,但仍然没有得到任何解决方案!任何链接或建议都将是很有帮助的

rep*_*tch 9

如果您可以访问网页的html/js代码,则可以执行以下操作:

  1. 创建将处理js回调的特殊类:

    private class PrintInterface {
      @JavascriptInterface // required annotation!
      public void printSome(String arg) { // here you can pass some args from js code
        // do some printing here from Java code
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 为webview启用js.然后将创建的界面添加到webView.作为第二个参数,您需要指定名称,这将在js中使用.我称它为"PrintInterface",与类名相同:

    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new PrintInterface(), "PrintInterface");`
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在您的js代码中调用PrintInterface中的相应方法.请注意,每个方法都必须注释@JavascriptInterface(看看1.):

    $("#someHtmlButton").on("click", function() {
        PrintInterface.printSome("some argument");
    } 
    
    Run Code Online (Sandbox Code Playgroud)

最后,在PrintInterface #printSome方法中,您可以直接从Java代码执行任何操作.

一些关于html打印的文档:https://developer.android.com/training/printing/html-docs