不能用的InputStream创建的ObjectInputStream的蓝牙套接字在Android平台上 [英] Cannot create ObjectInputStream with InputStream for a Bluetooth Socket on the Android Platform

查看:161
本文介绍了不能用的InputStream创建的ObjectInputStream的蓝牙套接字在Android平台上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个多人游戏Android手机。通信是通过蓝牙。我已成功地使用输入/输出流从一个手机发送字节到其他。因为我需要能够传递我想objectstreams对象。然而,当我试图创建一个ObjectStream时我流,我的程序挂起的指令。

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

// 指令新的ObjectInputStream(mmInStream)永远不会完成执行。它似乎没有引发错误,因为我会抓住它。它只是挂在该指令。是以往任何时候都没有执行下面这条线的code。

// 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?

推荐答案

刚刚构建 ObjectOutputStream的,的flush()是,在两端的的构建 ObjectInputStream的。您不必编写自己的任何数据。

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.

这篇关于不能用的InputStream创建的ObjectInputStream的蓝牙套接字在Android平台上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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