java – 无法在Android平台上使用InputStream为蓝牙套接字创建ObjectInputStream

前端之家收集整理的这篇文章主要介绍了java – 无法在Android平台上使用InputStream为蓝牙套接字创建ObjectInputStream前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为 Android手机编写多人游戏.通过蓝牙进行通信.我已设法使用输入/输出流将字节从一个电话发送到另一个电话.因为我需要能够传输我想要的对象流的对象.但是,当我尝试使用我的流创建一个Objectstream时,我的程序挂起了指令.
public class ConnectedThread extends Thread {
private static final String TAG = "Connected Thread";
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private Handler mHandler;
private ObjectInputStream ois;
private ObjectOutputStream oos;

public ConnectedThread(BluetoothSocket socket,Handler h) {
    mmSocket = socket;
    mHandler = h;

    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;
    Log.d(TAG,"attempting to create OIS");

    try {
    ois = new ObjectInputStream(mmInStream);

//指令new ObjectInputStream(mmInStream)永远不会完成执行.它似乎没有抛出错误,因为我抓住了它.它只是挂在这个指令.此行下面的代码都没有执行过.

} catch (Exception e) {

        Log.e(TAG,"Error");
        Log.d(TAG,e.getMessage());
        e.printStackTrace();
    } 

    Log.d(TAG,"attempting to create OOS");
    try {
        oos = new ObjectOutputStream(mmOutStream);
    } catch (IOException e) {
        Log.e(TAG,"IO exception for Output Stream,I have no idea what caused this");
        Log.d(TAG,e.getMessage());
    }

}

public void run() {.....}

我究竟做错了什么?

解决方法

在构造ObjectInputStream之前,只需构造ObjectOutputStream,并在两端刷新().您不必编写自己的任何数据.

猜你在找的Android相关文章