【问题标题】:Android - print from app on mobile printerAndroid - 从移动打印机上的应用程序打印
【发布时间】:2016-10-17 10:44:52
【问题描述】:

我有一个应用程序,它使用Rongta RRP-200 移动打印机打印一些文本,通过蓝牙与我的手机连接。

为此,我正在使用这个插件:https://github.com/srehanuddin/Cordova-Plugin-Bluetooth-Printer

我可以将我的设备连接到打印机,甚至可以从我的应用程序中运行打印功能,这会返回一条消息,通知我数据已发送。然而,打印机什么也不做(除了它的灯亮着)。

这是尝试打印我的文本的函数(来自插件):

boolean printText(CallbackContext callbackContext, String msg) throws IOException {
    try {
        mmOutputStream.write(msg.getBytes());

        // tell the user data were sent
        //Log.d(LOG_TAG, "Data Sent");
        callbackContext.success("Data Sent");
        return true;

    } catch (Exception e) {
        String errMsg = e.getMessage();
        Log.e(LOG_TAG, errMsg);
        e.printStackTrace();
        callbackContext.error(errMsg);
    }
    return false;
}

这里可能出了什么问题?

【问题讨论】:

    标签: android cordova mobile printing bluetooth


    【解决方案1】:

    发现插件工作正常,但你必须给打印机一个完整的行才能让它打印一些东西。因此,在字符串末尾添加\n。这是打印某些东西的功能,以防有人需要它(它在 Ionic 应用控制器中):

    $scope.print = function(text) {
        BTPrinter.connect(function(data){
            BTPrinter.printText(function(data){
                BTPrinter.disconnect(function(){},function(err){
                    console.log("Error");
                    console.log(err)
                }, "Your Printer device")
            }, function(err){
                console.log("Error");
                console.log(err)
            }, text + "\n")
        }, function(err){
                console.log("Error");
                console.log(err)
        }, "Your Printer device");
    }
    

    【讨论】:

      【解决方案2】:

      好吧,我有相同的打印机并写了一个小插件,它的工作对我来说太棒了。我在 RPP200 和 RPP300 中测试过。

      https://github.com/CXRom/cordova-plugin-rpp

      Rpp.Connect("00:0E:0E:0B:7B:93", // <-- MAC Address of the printer
        function(print) {
          //At this point we send the action but we need to wait until the connection
          console.log(`connect ok ${JSON.stringify(print)}`);
        },
        function (err){
          console.log(`connect err ${JSON.stringify(err)}`);
        });
      
      //Ask is device is connected
      Rpp.IsConnected(function(conn) {
        //Send to print
        Rpp.Print({
          marginTop: 10, //Margin before print
          marginBottom: 10, //Margin after print
          lineSpacing: 50, //Size of line
          lines: [ //Lines to print
            { text: "Title", align: 1, bold: true, underline: true, size: 17 }, //long name properties
            { text: "Subtitle", a: 1, b: true, u: true, s: 17 }, //short name properties
            { text: "normal line" },
            { text: ":)", h: true }
          ]
        }, function(res) {
          console.log(`print ok ${JSON.stringify(res)}`);
        }, function(err){
          console.log(`print err ${JSON.stringify(err)}`);
        });
      }, function(err) {
      
      });
      

      【讨论】:

      • 提供打印机名称是否有效?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      相关资源
      最近更新 更多