Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android蓝牙程序在大约一分钟后停止接收字符。java.io.exception:BT套接字关闭读取返回:-1_Java_Android_Sockets_Bluetooth_Spp - Fatal编程技术网

Android蓝牙程序在大约一分钟后停止接收字符。java.io.exception:BT套接字关闭读取返回:-1

Android蓝牙程序在大约一分钟后停止接收字符。java.io.exception:BT套接字关闭读取返回:-1,java,android,sockets,bluetooth,spp,Java,Android,Sockets,Bluetooth,Spp,我正在尝试将字节从我的电路板发送到我的android应用程序。我的硬件是一个微芯片BM78 SPP芯片,我肯定知道硬件传输字节,因为当我将计算机上的RFCOMM监视器连接到硬件时,字节将永远被接收。然而,在我的Android应用程序上,从30秒到2分钟接收字节,然后程序就停止了。应用程序没有崩溃,只是看起来停止了运行。程序停止时,“运行”窗口和“日志猫”窗口显示以下内容: 程序停止时运行并记录窗口 为什么最后一个缓冲区满是钻石问号?每次程序停止时,最后一个缓冲区充满菱形问号,长度超过60字节。

我正在尝试将字节从我的电路板发送到我的android应用程序。我的硬件是一个微芯片BM78 SPP芯片,我肯定知道硬件传输字节,因为当我将计算机上的RFCOMM监视器连接到硬件时,字节将永远被接收。然而,在我的Android应用程序上,从30秒到2分钟接收字节,然后程序就停止了。应用程序没有崩溃,只是看起来停止了运行。程序停止时,“运行”窗口和“日志猫”窗口显示以下内容: 程序停止时运行并记录窗口

为什么最后一个缓冲区满是钻石问号?每次程序停止时,最后一个缓冲区充满菱形问号,长度超过60字节。此外,当我调试程序时,当它停止时,我没有收到任何错误消息。程序只是静静地停止。有一次我收到消息java.io.exception:BT socket closed read return:-1。为什么我的蓝牙插座关闭了?提前谢谢

以下是我的ConnectedThread.java代码:

public class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private final Handler mHandler;

public ConnectedThread(BluetoothSocket socket, Handler handler) {
    mmSocket = socket;
    mHandler = handler;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) { }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}

@Override
public void run() {
    int bytes; // bytes returned from read()
    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            // Read from the InputStream
            bytes = mmInStream.available();
            byte[] buffer = new byte[60];
            if(bytes != 0) {
                SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed. Originally 100
                bytes = mmInStream.read(buffer, 0, 50); // record how many bytes we actually read
                mHandler.obtainMessage(AndroidGame.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget(); // Send the obtained bytes to the UI activity
            }
        } catch (IOException e) {
            e.printStackTrace();

            break;
        }

    }

}

/* Call this from the main activity to send data to the remote device
public void write(String input) {
    byte[] bytes = input.getBytes();           //converts entered String into bytes
    try {
        mmOutStream.write(bytes);
    } catch (IOException e) { }
}

/* Call this from the main activity to shutdown the connection */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
}

}

这是一个硬件问题。我回答了我的问题。