Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTPrinter.connect Error #59

Open
yogesh9059 opened this issue Mar 7, 2018 · 3 comments
Open

BTPrinter.connect Error #59

yogesh9059 opened this issue Mar 7, 2018 · 3 comments

Comments

@yogesh9059
Copy link

yogesh9059 commented Mar 7, 2018

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
Copy link

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

@Navaneedhanlaw
Copy link

Navaneedhanlaw commented Feb 19, 2021

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

@lepino17gdev
Copy link

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;
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants