Printooth icon indicating copy to clipboard operation
Printooth copied to clipboard

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

Open RonnyeryBarbosa opened this issue 4 years ago • 5 comments

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

sometimes it works at other times

RonnyeryBarbosa avatar Mar 26 '20 15:03 RonnyeryBarbosa

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

RaRoPe avatar Nov 05 '20 21:11 RaRoPe

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.

rafahsborges avatar Aug 04 '21 21:08 rafahsborges

this doesn't work for me. any updates?

grrigore avatar Nov 02 '21 10:11 grrigore

Any fix on this?

ralphgabrielle avatar Mar 07 '23 00:03 ralphgabrielle

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.

ovalman avatar Mar 03 '24 14:03 ovalman