Android - 在移动打印机上从应用程序打印

时间:2016-10-17 10:44:53

标签: android cordova mobile printing bluetooth

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

为此,我正在使用此插件: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;
}

这里可能出现什么问题?

2 个答案:

答案 0 :(得分:0)

发现该插件工作正常,但您必须为打印机提供完整的一行以使其打印出来。因此,请在字符串末尾添加\n。这是打印东西的功能,以防任何人需要它(它在Ionic app控制器中):

$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");
}

答案 1 :(得分:0)

我有相同的打印机,并为我写了一个小插件,它的工作很棒。我在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) {

});
相关问题