mazenrashed / Printooth

A well documented, high-level Android interface that makes printing via bluetooth printers easier
Mozilla Public License 2.0
399 stars 113 forks source link

Someone has the souvenir - read failed, socket might closed or timeout, read ret: -1 #46

Open RonnyeryBarbosa opened 4 years ago

RonnyeryBarbosa commented 4 years ago

I'm trying to print to more than one printer simultaneously, seve.

sometimes it works at other times

RaRoPe commented 3 years ago

Yes, @RonnyeryBarbosa, you solved it? I'm thinking that is an error in socket, that if we want to create a two instances of printing, we should create a different socket to support that. Or we could change the Bluetooth.java, but we can't do that because that file is protected. Another solution is trying to create our library changing the

device.createRfcommSocketToServiceRecord(mMyUuid);

from:

tmp = createBluetoothSocket(mmDevice);

This one is the part of the code that we got our error:

(android/platform/frameworks/base/d6883533e4ac3f73d2fde1db9a1dddf06dac6709/./core/java/android/bluetooth/BluetoothSocket.java)

private int readAll(InputStream is, byte[] b) throws IOException {

   int left = b.length;
   while(left > 0) {
   int ret = is.read(b, b.length - left, left);
   if(ret <= 0)
   throw new IOException("read failed, socket might closed, read ret: " + ret);
   left -= ret;
   if(left != 0)
   Log.w(TAG, "readAll() looping, read partial size: " + (b.length - left) +
   ", expect size: " + b.length);
   }
   return b.length;
}
rafahsborges commented 3 years ago

I've changed the code in Bluetooth.java as follows:

ConnectThread(BluetoothDevice device, boolean insecureConnection) { Bluetooth.this.device = device; try { if (insecureConnection) { Bluetooth.this.socket = device.createInsecureRfcommSocketToServiceRecord(uuid); } else { Bluetooth.this.socket = (BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1); } } catch (IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { if (deviceCallback != null) { deviceCallback.onError(Objects.requireNonNull(e.getMessage())); } } }

It worked fine.

grrigore commented 2 years ago

this doesn't work for me. any updates?

ralphgabrielle commented 1 year ago

Any fix on this?

ovalman commented 7 months ago

Not a perfect solution but I created a dialog telling my users the app needs a restart. When the app restarts, it resets the socket and fixes the problem. This only needs to run once until a new printer is installed.

This is what worked for me.