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

mzsu5hc0  于 2024-01-04  发布在  Android
关注(0)|答案(3)|浏览(203)

我有一个应用程序,可以使用Rongta RRP-200移动的打印机打印一些文本,通过Bluetoth与我的手机连接。
为此,我使用这个插件:https://github.com/srehanuddin/Cordova-Plugin-Bluetooth-Printer
我可以将我的设备连接到打印机,甚至可以从我的应用程序运行打印功能,这会给我一条消息,通知我数据已发送。然而,打印机什么也不做(除了它的灯亮起)。
这是试图打印我的文本的函数(来自插件):

  1. boolean printText(CallbackContext callbackContext, String msg) throws IOException {
  2. try {
  3. mmOutputStream.write(msg.getBytes());
  4. // tell the user data were sent
  5. //Log.d(LOG_TAG, "Data Sent");
  6. callbackContext.success("Data Sent");
  7. return true;
  8. } catch (Exception e) {
  9. String errMsg = e.getMessage();
  10. Log.e(LOG_TAG, errMsg);
  11. e.printStackTrace();
  12. callbackContext.error(errMsg);
  13. }
  14. return false;
  15. }

字符串
这里能出什么问题

daupos2t

daupos2t1#

发现该插件工作正常,但你必须给予一个完整的行到打印机,使其打印的东西。因此,添加\n在你的字符串结束。这是打印的功能,如果有人需要它(它在一个离子应用程序控制器):

  1. $scope.print = function(text) {
  2. BTPrinter.connect(function(data){
  3. BTPrinter.printText(function(data){
  4. BTPrinter.disconnect(function(){},function(err){
  5. console.log("Error");
  6. console.log(err)
  7. }, "Your Printer device")
  8. }, function(err){
  9. console.log("Error");
  10. console.log(err)
  11. }, text + "\n")
  12. }, function(err){
  13. console.log("Error");
  14. console.log(err)
  15. }, "Your Printer device");
  16. }

字符串

展开查看全部
wxclj1h5

wxclj1h52#

好吧,我有相同的打印机,写了一个小插件,它的工作awsome对我来说。我在RPP200和RPP300测试。
https://github.com/CXRom/cordova-plugin-rpp

  1. Rpp.Connect("00:0E:0E:0B:7B:93", // <-- MAC Address of the printer
  2. function(print) {
  3. //At this point we send the action but we need to wait until the connection
  4. console.log(`connect ok ${JSON.stringify(print)}`);
  5. },
  6. function (err){
  7. console.log(`connect err ${JSON.stringify(err)}`);
  8. });
  9. //Ask is device is connected
  10. Rpp.IsConnected(function(conn) {
  11. //Send to print
  12. Rpp.Print({
  13. marginTop: 10, //Margin before print
  14. marginBottom: 10, //Margin after print
  15. lineSpacing: 50, //Size of line
  16. lines: [ //Lines to print
  17. { text: "Title", align: 1, bold: true, underline: true, size: 17 }, //long name properties
  18. { text: "Subtitle", a: 1, b: true, u: true, s: 17 }, //short name properties
  19. { text: "normal line" },
  20. { text: ":)", h: true }
  21. ]
  22. }, function(res) {
  23. console.log(`print ok ${JSON.stringify(res)}`);
  24. }, function(err){
  25. console.log(`print err ${JSON.stringify(err)}`);
  26. });
  27. }, function(err) {
  28. });

字符串

展开查看全部
avkwfej4

avkwfej43#

不知道这是否仍然是必要的,但尝试Cordova-Bluetooth-Printer-Plugin by TruewindIT

  1. {
  2. "version": "1.0.0",
  3. "name": "Cordova-Bluetooth-Printer-Plugin",
  4. "cordova_name": "Cordova Bluetooth Printer Plugin",
  5. "license": "MIT License",
  6. "author": "Truewind IT",
  7. "platforms": [ "android"],
  8. "repository": {
  9. "type": "git",
  10. "url": "git+https://github.com/TruewindIT/Cordova-Bluetooth-Printer-Plugin.git"
  11. }
  12. }

字符串
$("#console")只是一个文本区域标签,用于在我的Android应用程序中打印状态
$("#deviceList")是一个ul标记,它将包含连接设备的列表
列出连接的设备:

  1. BTPrinter.list(function(success){
  2. $("#console").append("Success");
  3. $("#console").append("::"+JSON.stringify(success, null, 2)+"::");
  4. success.forEach((e) => {
  5. $("#deviceList").append(`<li value="${e}">${e}</li>`)
  6. });
  7. },function(err){
  8. $("#console").append("Error\n");
  9. $("#console").append(JSON.stringify(err, null, 2))
  10. })


连接和打印
注意:**BTPrinter.print()**是正确的方法,而不是基于BluetoothPrinter.javaBTPrinter.printText()

  1. $('#deviceList').on('click', 'li', function () {
  2. var deviceId = $(this).val();
  3. var deviceName = $(this).text();
  4. BTPrinter.connect(function(data){
  5. $("#console").append("Connect Success\n");
  6. $("#console").append(JSON.stringify(data, null, 2)+"\n")
  7. BTPrinter.print(function(data){
  8. $("#console").append("Printing Success:\n");
  9. $("#console").append(JSON.stringify(data, null, 2)+"\n");
  10. BTPrinter.disconnect(function(){
  11. $("#console").append("Disconnect Success:\n");
  12. },function(err){
  13. $("#console").append("Disconnect Error");
  14. }, deviceName)
  15. },function(err){
  16. $("#console").append("Failed in Printing:\n")
  17. $("#console").append(JSON.stringify(err, null, 2)+"\n")
  18. }, "Hello world text.\n\n\n\n")
  19. },function(err){
  20. $("#console").append("Connect Error\n");
  21. $("#console").append(JSON.stringify(err, null, 2)+"\n")
  22. }, deviceName)
  23. // Attempt to connect to the selected device
  24. });


cordova-bluetooth-printer-plugin/src/android/BluetoothPrinter.java线121

  1. else if( action.equals("print") )
  2. {
  3. try
  4. {
  5. String msg = args.getString(0);
  6. print(callbackContext, msg);
  7. }
  8. catch( IOException e )
  9. {
  10. Log.e(LOG_TAG, e.getMessage());
  11. e.printStackTrace();
  12. }
  13. return true;
  14. }

展开查看全部

相关问题