srehanuddin / Cordova-Plugin-Bluetooth-Printer

A cordova plugin for bluetooth printer for android platform
103 stars 115 forks source link

BTPrinter.connect Error #59

Open yogesh9059 opened 6 years ago

yogesh9059 commented 6 years ago

I am using this plugin in ionic1. BTPrinter.list gives me list of bluetooth devices paired with my android device. But when i try to connect any bluetooth device it gives error

read failed, socket might closed or timeout, read ret: -1

I try with adding $timeout but it still gives same error. For connecting to bluettoth device below is code

BTPrinter.connect(function(data){ 
            console.log("Printer connected..." + data); 
            BTPrinter.printText(function(data){ 
              console.log('Printer printing text...'+data); 
              setTimeout(function(){ 
                BTPrinter.disconnect(function(data){ 
                  console.log("Printer disconnected ..." + data);
                  },function(err){ 
                    console.log("Printer error disconnecting! " + err); 
                    alert("Printer error disconnecting!" + err); 
                  }, "PrinterName"); 
              }, 3000); 
            },function(err){ 
              console.log("Printer error Printing text! " + err); 
              alert("Printer error Printing text! " + err); 
            }, text + " \n \n"); 
          },function(err){ 
            console.log("Printer error Connecting! " + err); 
            alert("Printer error Connecting! " + err); 
          }, "PrinterName");

If any one solve this issue please help me to solve it.

saibrahamaiah commented 6 years ago

Any one solve this problem read failed, socket might closed or timeout, read ret: -1 Still iam getting same error.

Navaneedhanlaw commented 3 years ago

Can anyone provide sample code for angular ionic capacitor printer integration? I am stuck in BTPrinter.list itself..

lepino17gdev commented 10 months ago

Hi, Not sure if this is still necessary but try Cordova-Bluetooth-Printer-Plugin by TruewindIT.

{
    "version": "1.0.0",
    "name": "Cordova-Bluetooth-Printer-Plugin",
    "cordova_name": "Cordova Bluetooth Printer Plugin",
    "license": "MIT License",
    "author": "Truewind IT",
    "platforms": [ "android"],
    "repository": {
        "type": "git",
        "url": "git+https://github.com/TruewindIT/Cordova-Bluetooth-Printer-Plugin.git"
    }
}

Listing connected devices:

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

Connect and Print NOTE: BTPrinter.print is the correct method not BTPrinter.printText based on the BluetoothPrinter.java

$('#deviceList').on('click', 'li', function () {
      var deviceId = $(this).val();
      var deviceName = $(this).text();

      BTPrinter.connect(function(data){
        $("#console").append("Connect Success\n");
        $("#console").append(JSON.stringify(data, null, 2)+"\n")

          BTPrinter.print(function(data){
            $("#console").append("Printing Success:\n");
            $("#console").append(JSON.stringify(data, null, 2)+"\n");

              BTPrinter.disconnect(function(){

               $("#console").append("Disconnect Success:\n");
              },function(err){
                $("#console").append("Disconnect Error");
            }, deviceName)

          },function(err){
              $("#console").append("Failed in Printing:\n")
              $("#console").append(JSON.stringify(err, null, 2)+"\n")
          }, "Hello world text.\n\n\n\n")

      },function(err){
        $("#console").append("Connect Error\n");
        $("#console").append(JSON.stringify(err, null, 2)+"\n")
      }, deviceName)
      // Attempt to connect to the selected device
  });

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

else if( action.equals("print") ) 
        {
            try 
            {
                String msg = args.getString(0);
                print(callbackContext, msg);
            } 
            catch( IOException e ) 
            {
                Log.e(LOG_TAG, e.getMessage());
                e.printStackTrace();
            }

            return true;
        }