3

私は戦艦ゲームを作成しており、Ships という名前のクラスの配列を送信したいと考えています (これには、船の名前、サイズ、回転されているかどうか、および座標の配列リストが含まれています)。私はこれをグーグルで検索し、スタックオーバーフローを調べました。基本的には配列をシリアル化する必要がありますが、ここで行き詰まっています。ObjectOutputStream を使用する必要がありますが、それを以下のコードに組み込むにはどうすればよいですか (Android 開発サイトから取得)。注: ship クラスは既にシリアライズ可能に実装されていることに注意してください。前もって感謝します

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

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

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

    }

そして私のハンドラー:

        final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
            case MESSAGE_READ2:

                byte[] readBuf = (byte[]) msg.obj;
                String readMessage = new String(readBuf, 0, msg.arg1);
                break;

            case MESSAGE_WRITE:
                byte[] writeBuf = (byte[]) msg.obj;
                String writeMessage = new String(writeBuf);
                //Toast.makeText(getApplicationContext(),"Me:" + writeMessage, Toast.LENGTH_SHORT).show();
                break;
4

1 に答える 1

6

上記のコードでは、接続されたソケットから入出力ストリームを取得します。

これらのストリームを使用して、ソケットとの間でデータをストリーミングできるようになりました。

これをどの程度正確に行うかは、ストリーミングするデータの種類によって異なります。この場合、送信するシリアル化可能なオブジェクトがあるため、オブジェクトで使用するためにストリームを適応させるフィルターでストリームをラップします: ObjectOutputStream/ObjectInputStream...

ObjectOutputStream oos = new ObjectOutputStream( mmOutStream );
for (Ship ship: ships)
  oos.writeObject( ship );

このコードは、Ship の配列を反復処理し、各 Ship をストリーム (したがって、Bluetooth ソケット) に書き込みます。

受信側も同じですが、さらに複雑な点が 1 つあります。いつ停止するか、何を読むかが必ずしもわからないということです。これを処理するためのさまざまなスキームがあり、これを具体的に扱う SO の質問があります。Android デベロッパー ガイドの Bluetooth ページには、このためのサンプル コードがあります: http://developer.android.com/guide/topics/connectivity/bluetooth.html

于 2013-03-20T01:38:24.447 に答える