如何调整Android Dev代码以便可以通过蓝牙发送对象?

时间:2013-04-03 19:35:15

标签: android bluetooth

嗨我需要在蓝牙上为我的战舰游戏发送一个船级。我试着编辑发送字符串的代码,但我真的不确定我在做什么。我的代码如下。我知道我需要在发送之前对该对象进行串行,但我只是得到了错误,我将会显示出来。先感谢您 代码:

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

}

logcat的:

  

04-03 20:25:32.799:E / deserializeObject(30255):错误   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):at 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):at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)   04-03 20:25:31.868:E / serializeObject(30255):at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)   04-03 20:25:31.868:E / serializeObject(30255):at 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 java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)   04-03 20:25:31.868:E / serializeObject(30255):at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)   04-03 20:25:31.868:E / serializeObject(30255):at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)   04-03 20:25:31.868:E / serializeObject(30255):at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)   04-03 20:25:31.868:E / serializeObject(30255):at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)   04-03 20:25:31.868:E / serializeObject(30255):at 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):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)   04-03 20:25:31.868:E / serializeObject(30255):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您的对象必须是Serializable及其字段。 如果您不关心(某些)字段,则可以使用transient关键字。

例如:

class House implements Serializable{

    private int numberOfWindows; //No problem here

    private Roof myRoof; //The Roof class should implement Serializable as well!

    private transient Door myFrontDoor; //The Door class doesn't need to implement Serializable, it will be ignored.

}

请参阅here