获取java.io.IOException:读取失败,套接字可能关闭或超时,通过蓝牙打印机打印时读取ret:-1 [英] Getting java.io.IOException: read failed, socket might closed or timeout, read ret: -1 while printing via bluetooth printer

查看:1187
本文介绍了获取java.io.IOException:读取失败,套接字可能关闭或超时,通过蓝牙打印机打印时读取ret:-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码仅在第一次正常工作,如果我尝试再次连接它,则会引发此异常:

Code is working fine for first time only, if i am trying to connect it again it is throwing this exception:

读取失败,套接字可能关闭或超时,读取ret:-1

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

这是我用于连接蓝牙打印机的功能:

This is my function for connecting to the bluetooth printer:

public boolean openBT(Context context) throws IOException {
    try {
        // Standard SerialPortService ID
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
        mBluetoothAdapter.cancelDiscovery();

        mmSocket.connect();

        mmOutputStream = new DataOutputStream(mmSocket.getOutputStream());
        mmInputStream = new DataInputStream(mmSocket.getInputStream());


    } catch (NullPointerException e) {
        e.printStackTrace();
        return false;
    } catch (Exception e) { 
         e.printStackTrace();
        return false;

    }

    return true;

}

推荐答案

使用后应关闭套接字:

public boolean openBT(Context context) throws IOException {
try {
    // Standard SerialPortService ID
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mBluetoothAdapter.cancelDiscovery();

    mmSocket.connect();

    mmOutputStream = new DataOutputStream(mmSocket.getOutputStream());
    mmInputStream = new DataInputStream(mmSocket.getInputStream());

    mmSocket.close(); //Socket closed
} catch (NullPointerException e) {
    e.printStackTrace();
    return false;
} catch (Exception e) { 
     e.printStackTrace();
    return false;

}

return true;}

这篇关于获取java.io.IOException:读取失败,套接字可能关闭或超时,通过蓝牙打印机打印时读取ret:-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆