마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

블루투스 통신에서 문자 송수신구조가 어떻게되는지 궁금합니다.

0 추천

블루투스 chat 예제소스를 보면서 공부하는대 

private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(main.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }

        /**
         * Write to the connected OutStream.
         * @param buffer  The bytes to write
         */
        public void write(byte[] buffer) {
            try {
                mmOutStream.write(buffer);

                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(main.MESSAGE_WRITE, -1, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }

해당 bluetoothchatservice 내부에있는 connected thread 안에 write가 송신이고 run이 수신부인대

다른 엑티비티에서 접근하려니 이상하게 write 는접근이되는대 run은 접근이안됩니다.

bluetoothchatservice.write(문자열변수 x);

후 

if(bluetoothchatservice.run() == 조건x) 식으로 명령어를 집어넣을려고하는데 혹시 제가잘못알고있는게 있나 싶어

이렇게질문드립니다.

naiad (430 포인트) 님이 2014년 2월 26일 질문

1개의 답변

0 추천
 
채택된 답변

thread부분을 먼저 아셔야  할듯 합니다.

현재 ConnectedThread는 쓰레드를 상속받고 있지요.

run은 쓰레드에서 기본적으로 쓰레드로 처리할 부분들을 작업해주는 오버라이드 된 부분입니다.

당연히 if(bluetoothchatservice.run() == 조건x) 이런식으로 안되죠.

gusdn9 (1,560 포인트) 님이 2014년 2월 26일 답변
naiad님이 2014년 2월 26일 채택됨
아 감사합니다. 그리고 제가 잘못알았네요. connectedthread class 가 privated 형식이여서 외부에 따로 write 함수가 connected 클래스에있는 write함수를 참조하네요.
...