0

我在使用 Android 蓝牙打印机时遇到了一个奇怪的问题。当我第一次尝试连接打印机设备时,我遇到了以下问题

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)

我正在使用以下代码将设备与打印机连接

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();

注意:一旦我尝试调试上面的代码块,它将与打印机连接并能够打印。只有普通模式的问题。

任何人都对同样的问题有任何想法。

4

1 回答 1

0

供参考

我已将以下代码用于打印机集成,其中一个没有此问题。https://amsheer007.wordpress.com/2019/04/12/bluetooth-printer-for-android/

于 2019-04-24T12:03:22.770 回答