1

56lgkhnf  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(221)

我试图发送字节从我的电路板到我的android应用程序。我的硬件是一个微芯片bm78spp芯片,我知道硬件传输字节,因为当我把我的电脑上的rfcomm监视器连接到硬件时,字节永远被接收。然而,在我的android应用程序上,接收字节的时间从30秒到2分钟不等,然后程序就停止了。应用程序没有崩溃,只是看起来停止了运行。当程序停止时,run窗口和logcat窗口显示以下内容:程序停止时run和logcat窗口

为什么最后的缓冲区充满了钻石问号?每次程序停止时,最后一个缓冲区都充满了菱形问号,长度超过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) { }
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题