嗨,我需要通过蓝牙为我的战舰游戏发送一个船级。我试图编辑用于发送字符串的代码,但我真的不确定我在做什么。我的代码如下。我知道我需要在发送之前对对象进行序列化,但我只是得到了错误,我也会显示出来。提前谢谢你代码:
public class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "connectedthread started");
// mHandler.obtainMessage(TEST).sendToTarget();
mmSocket = socket;
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) {
Log.e(TAG, "temp sockets not created");
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "Begin mConnectedThread");
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
Log.i(TAG, "reaaaad msg");
mHandler.obtainMessage(SetUpGame.MESSAGE_READ2, bytes,
-1, buffer).sendToTarget();
try {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer));
Ship object = (Ship) in.readObject();
in.close();
mHandler.obtainMessage(SetUpGame.MESSAGE_READ_OBJ, -1,
-1, object).sendToTarget();
}
catch(ClassNotFoundException cnfe) {
Log.e("deserializeObject", "class not found error", cnfe);
} catch(IOException ioe) {
Log.e("deserializeObject", "io error", ioe);
}
}
catch (IOException e) {
Log.e(TAG, "disconnectd");
break;
}
}
}
/*
* Call this from the main activity to send data to the remote device
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
Log.i(TAG, "writeeee msg");
mHandler.obtainMessage(SetUpGame.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write");
}
}
public void writeObj(Ship ship) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutput outObj = new ObjectOutputStream(bos);
outObj.writeObject(ship);
outObj.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
}
catch(IOException ioe) {
Log.e("serializeObject", "error", ioe);
}
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close of connect socket failed");
}
}
}
日志猫:
04-03 20:25:32.799: E/deserializeObject(30255): io 错误 04-03 20:25:32.799: E/deserializeObject(30255): java.io.StreamCorruptedException 04-03 20:25:32.799: E/ deserializeObject(30255): atjava.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2369) 04-03 20:25:32.799: E/deserializeObject(30255): at java.io.ObjectInputStream.(ObjectInputStream.java:433) 04 -03 20:25:32.799:E/deserializeObject(30255):在 com.example.battleships.v2.ChatService$ConnectedThread.run(ChatService.java:292)
和:
04-03 20:25:31.868: E/serializeObject(30255): 错误 04-03 20:25:31.868: E/serializeObject(30255): java.io.NotSerializableException: com.example.battleships.v2.SetUpGame$FriendlyGrid 04-03 20:25:31.868: E/serializeObject(30255): 在 java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535) 04-03 20:25:31.868: E/serializeObject(30255): 在 java. io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847) 04-03 20:25:31.868: E/serializeObject(30255): 在 java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689) 04-03 20:25: 31.868: E/serializeObject(30255): 在 java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653) 04-03 20:25:31.868: E/serializeObject(30255): 在 java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream .java:1143) 04-03 20:25:31.868: E/serializeObject(30255):在 java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413) 04-03 20:25:31.868: E/serializeObject(30255): 在 java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241) 04-03 20 :25:31.868: E/serializeObject(30255): 在 java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575) 04-03 20:25:31.868: E/serializeObject(30255): 在 java.io.ObjectOutputStream。 writeObjectInternal(ObjectOutputStream.java:1847) 04-03 20:25:31.868: E/serializeObject(30255): 在 java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689) 04-03 20:25:31.868: E/ serializeObject(30255): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653) 04-03 20:25:31.868: E/serializeObject(30255): at com.example.battleships.v2.ChatService$ConnectedThread.writeObj (ChatService.java:333) 04-03 20:25:31.868: E/serializeObject(30255): at com.example.battleships.v2.ChatService.writeObj(ChatService.java:86) 04-03 20:25:31.868: E/serializeObject(30255): at com.example.battleships .v2.SetUpGame$3.onClick(SetUpGame.java:119) 04-03 20:25:31.868: E/serializeObject(30255): 在 android.view.View.performClick(View.java:2533) 04-03 20: 25:31.868: E/serializeObject(30255): 在 android.view.View$PerformClick.run(View.java:9320) 04-03 20:25:31.868: E/serializeObject(30255): 在 android.os.Handler .handleCallback(Handler.java:587) 04-03 20:25:31.868: E/serializeObject(30255): 在 android.os.Handler.dispatchMessage(Handler.java:92) 04-03 20:25:31.868: E /serializeObject(30255): 在 android.os.Looper.loop(Looper.java:150) 04-03 20:25:31.868: E/serializeObject(30255): 在 android.app.ActivityThread.main(ActivityThread.java: 4385) 04-03 20:25:31.868: E/serializeObject(30255): at java.lang.reflect.Method.invokeNative(Native Method) 04-03 20:25:31.868: E/serializeObject(30255): at java.lang.reflect.Method.invoke( Method.java:507) 04-03 20:25:31.868: E/serializeObject(30255): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 04-03 20:25: 31.868: E/serializeObject(30255): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 04-03 20:25:31.868: E/serializeObject(30255): 在 dalvik.system.NativeStart .main(本机方法)java:849) 04-03 20:25:31.868: E/serializeObject(30255): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 04-03 20:25:31.868: E/ serializeObject(30255):在 dalvik.system.NativeStart.main(Native Method)java:849) 04-03 20:25:31.868: E/serializeObject(30255): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 04-03 20:25:31.868: E/ serializeObject(30255):在 dalvik.system.NativeStart.main(Native Method)