Printooth icon indicating copy to clipboard operation
Printooth copied to clipboard

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

Open angeldev opened this issue 5 years ago • 4 comments

Hi, I have a list of items with a button which I can press and print the item info. Printing is fine if I press a button and wait for a while until I press again another one. But if I press two buttons too close in time I get this error and it prints only the first one:

W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@a1962b9, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@cf996fe, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@8bade5fmSocket: android.net.LocalSocket@3cbd0ac impl:android.net.LocalSocketImpl@8647175 fd:java.io.FileDescriptor@2d4250a, mSocketState: INIT D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@a1962b9, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@cf996fe, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@8bade5fmSocket: null, mSocketState: CLOSED read failed, socket might closed or timeout, read ret: -1

I just have a clickListener for the buttons:

binding.button.setOnClickListener {
    Printer.print(organisationName, item)
    viewModel.editOrderStatus(item.id, item.statusId+1) 
}


fun print(organisationName: String, order: Order) {
    if (Printooth.hasPairedPrinter()) {
        var printables = ArrayList<Printable>()
        var printable = TextPrintable.Builder()
            .setText(organisationName) //The text you want to print
            .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
            .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_BOLD) //Bold or normal
            .setFontSize(DefaultPrinter.FONT_SIZE_LARGE)
            .setUnderlined(DefaultPrinter.UNDERLINED_MODE_ON) // Underline on/off
            .setCharacterCode(DefaultPrinter.CHARCODE_PC437) // Character code to support languages
            .setLineSpacing(DefaultPrinter.LINE_SPACING_60)
            .setNewLinesAfter(1) // To provide n lines after sentence
            .build()
        printables.add(printable)
        Printooth.printer().print(printables)
    }

}

Any workaround I can have here? Thanks!

angeldev avatar Feb 23 '20 18:02 angeldev

Yes, @angeldev , i got this error too. 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

hi @angeldev , did you solve this problem?

filutfi avatar Jun 21 '21 10:06 filutfi

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

any updates on this issue?

grrigore avatar Nov 01 '21 14:11 grrigore