androidbluetoothandroid-print-framework

Android Bluetooth thermal printer connection issue


I am facing one strange issue with the Android Bluetooth printer. When I try to connect with the printer device for the first time I am facing the following issue

BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:738)
    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:750)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:375)

I am using the following code to connect device with printer

Thread connectThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    boolean gotuuid = btDevices.getItem(position)
                            .fetchUuidsWithSdp();
                    UUID uuid = btDevices.getItem(position).getUuids()[0]
                            .getUuid();
                    mbtSocket = btDevices.getItem(position)
                            .createRfcommSocketToServiceRecord(uuid);

                    mbtSocket.connect();
                } catch (IOException ex) {

                    ex.printStackTrace();

                    runOnUiThread(socketErrorRunnable);
                    try {
                        mbtSocket.close();
                    } catch (IOException e) {
                     e.printStackTrace();
                    }
                    mbtSocket = null;
                } finally {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            finish();

                        }
                    });
                }
            }
        });

        connectThread.start();

Note: Once I try to debug the above block of code it will connect with printer and able to print. Only the issue with the normal mode.

Anyone have any idea with the same issue.


Solution

  • FYI

    I have used the following code for the printer integration which one doesn't have this issue. https://amsheer007.wordpress.com/2019/04/12/bluetooth-printer-for-android/