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

发布于 2024-10-31 05:17:11 字数 1574 浏览 5 评论 0原文

我正在为 Android 手机编写一款多人游戏。通过蓝牙进行通信。我已成功使用输入/输出流将字节从一部手机发送到另一部手机。因为我需要能够传输对象,所以我需要对象流。但是,当我尝试使用我的流创建对象流时,我的程序会挂起指令。

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() {.....}

我做错了什么?

I am writing a multiplayer game for Android phones. Communication is via Bluetooth. I have managed to send bytes from one phone to the other using the input / output stream. Since I need to be able to transfer objects I want objectstreams. However, when I try to create an Objectstream with my streams, my program hangs on the instruction.

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

// The instruction new ObjectInputStream(mmInStream) NEVER FINISHES EXECUTING. It doesn't seem to throw an error, because I'd catch it. It just hangs at this instruction. None of the code below this line is ever executed.

    } 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() {.....}

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗心人 2024-11-07 05:17:11

只需在构造 ObjectInputStream 之前在两端构造 ObjectOutputStreamflush() 即可。不必编写您自己的任何数据。

Just construct the ObjectOutputStream, and flush() it, at both ends before constructing the ObjectInputStream. You don't have to write any data of your own.

安人多梦 2024-11-07 05:17:11

正如 EJP 所建议的......

        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.flush();
        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());

As EJP suggested ...

        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.flush();
        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
幼儿园老大 2024-11-07 05:17:11

好吧,我想我知道我做错了什么。对象流更复杂,看来 ObjectInputStream 构造函数在创建流之前需要处理数据。我通过

  1. 创建 OOS 解决了这个问题。
  2. 在单独的线程中启动 OIS 的构造函数。
  3. 将一些数据写入 OOS 并刷新它。
  4. 等待 OIS 初始化,然后再读取。

这就是我现在使用的(请注意,我还添加了一个缓冲区):

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 (Exception e) { 
        Log.d(TAG,"Error in getting Input Streams");
        Log.w(TAG,e);

    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;


    Log.d(TAG,"The socket is: " + mmSocket);
    Log.d(TAG,"The streams are: " + mmInStream + mmOutStream);
    Log.d(TAG,"attempting to create BufStreams");

    final BufferedOutputStream bufo = new BufferedOutputStream(mmOutStream);
    final BufferedInputStream bufi = new BufferedInputStream(mmInStream);

    Log.d(TAG,"attempting to create OOS");

    try {
        oos = new ObjectOutputStream(bufo);



    } catch (StreamCorruptedException e) {
        Log.d(TAG,"Caught Corrupted Stream Exception");
        Log.w(TAG,e);

    } catch (IOException e) {
        Log.d(TAG,"Caught IOException");
        Log.w(TAG,e);
    }

    Log.d(TAG,"done OOS");

    if(oos==null)
      {
           Log.d(TAG,"oos is null!!!!");
      }


    Thread s = new Thread(){
        public void run(){
            Log.d(TAG,"attempting to create OIS");
            try {
                ois = new ObjectInputStream(bufi);
            } catch (StreamCorruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d(TAG,"completed OIS");
            if(ois == null)
            {
                Log.d(TAG,"OIS is null");
            }
        }




    };
    s.start();
    try {
        Log.d(TAG,"writing and flushing 1");
        oos.write(1);
        oos.flush();
    } catch (IOException e1) {
        Log.d(TAG,"CaugtIOexception");
        Log.w(TAG,e1);
    }
    Log.d(TAG,"sleeping to make sure stream is set up");
    while (ois == null) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }


    }
    Log.d(TAG, "done Sleeping");

    // Read out the 1 to make sure everything is okay 

    int i = 0;

    try {
        i = ois.read();
    } catch (IOException e) {
        Log.d(TAG, "error reading");
        e.printStackTrace();
    }


    Log.d(TAG,"I received an i of: " + i);
    Log.d(TAG,"OO Streams set up");




}

public void run() {...

Alright, I think I know what I did wrong. Object Streams are more complicated, it seems the ObjectInputStream constructor needs data to work on before it creates the the stream. I solved this problem by

  1. Creating the OOS.
  2. Start the constructor for the OIS in a seperate thread.
  3. Write some data into the OOS and flush it.
  4. Wait for the OIS to become initialized before reading from it.

This is what I'm using now (note that I also added a buffer):

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 (Exception e) { 
        Log.d(TAG,"Error in getting Input Streams");
        Log.w(TAG,e);

    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;


    Log.d(TAG,"The socket is: " + mmSocket);
    Log.d(TAG,"The streams are: " + mmInStream + mmOutStream);
    Log.d(TAG,"attempting to create BufStreams");

    final BufferedOutputStream bufo = new BufferedOutputStream(mmOutStream);
    final BufferedInputStream bufi = new BufferedInputStream(mmInStream);

    Log.d(TAG,"attempting to create OOS");

    try {
        oos = new ObjectOutputStream(bufo);



    } catch (StreamCorruptedException e) {
        Log.d(TAG,"Caught Corrupted Stream Exception");
        Log.w(TAG,e);

    } catch (IOException e) {
        Log.d(TAG,"Caught IOException");
        Log.w(TAG,e);
    }

    Log.d(TAG,"done OOS");

    if(oos==null)
      {
           Log.d(TAG,"oos is null!!!!");
      }


    Thread s = new Thread(){
        public void run(){
            Log.d(TAG,"attempting to create OIS");
            try {
                ois = new ObjectInputStream(bufi);
            } catch (StreamCorruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d(TAG,"completed OIS");
            if(ois == null)
            {
                Log.d(TAG,"OIS is null");
            }
        }




    };
    s.start();
    try {
        Log.d(TAG,"writing and flushing 1");
        oos.write(1);
        oos.flush();
    } catch (IOException e1) {
        Log.d(TAG,"CaugtIOexception");
        Log.w(TAG,e1);
    }
    Log.d(TAG,"sleeping to make sure stream is set up");
    while (ois == null) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }


    }
    Log.d(TAG, "done Sleeping");

    // Read out the 1 to make sure everything is okay 

    int i = 0;

    try {
        i = ois.read();
    } catch (IOException e) {
        Log.d(TAG, "error reading");
        e.printStackTrace();
    }


    Log.d(TAG,"I received an i of: " + i);
    Log.d(TAG,"OO Streams set up");




}

public void run() {...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文