无法在Android平台上使用蓝牙套接字的InputStream创建ObjectInputStream
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需在构造
ObjectInputStream 之前在两端构造
ObjectOutputStream
和flush()
即可。不必编写您自己的任何数据。Just construct the
ObjectOutputStream,
andflush()
it, at both ends before constructing theObjectInputStream.
You don't have to write any data of your own.正如 EJP 所建议的......
As EJP suggested ...
好吧,我想我知道我做错了什么。对象流更复杂,看来 ObjectInputStream 构造函数在创建流之前需要处理数据。我通过
这就是我现在使用的(请注意,我还添加了一个缓冲区):
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
This is what I'm using now (note that I also added a buffer):