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

문자열 담는것좀 봐주세여ㅜ.ㅜ

0 추천

안드로이드와 터미널 사이에서 블루투스 통신으로 텍스트를 주고받는데

터미널에서 텍스트를 안드로이드로 보내면 이어져서 전송되야할 글자가 한글자씩 보내집니다..

왜그런걸까요?

private final MyHandler mHandler = new MyHandler(this);

private static class MyHandler extends Handler {
    private final WeakReference<BluetoothChat> mActivity;
    public MyHandler(BluetoothChat activity) {
        mActivity = new WeakReference<BluetoothChat>(activity);
    }

    @Override
    public void handleMessage(Message msg) {
        BluetoothChat activity = mActivity.get();
        if (activity != null) {
            activity.handleMessage(msg);
        }
    }
}



public void handleMessage(Message msg) {
    switch (msg.what) {
        
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// 버퍼 내의 유효한 바이트로부터 캐릭터 라인을 구축한다
String readMessage = new String(readBuf, 0, msg.arg1);

   /* for(int i=0; i<readBuf.length;i++) {
        System.out.println(readBuf[i]);
    }*/

    String[] readBufArray = readMessage.split(",");
    List<String> readBufList = new ArrayList<>();
    for (int i = 0; i < readBufArray.length; i++) {
        readBufList.add(readBufArray[i]);
        {
            System.out.println(readBufArray[i]);
        }
    }


mAdapter.notifyDataSetChanged();
messageList.add(new androidRecyclerView.Message(counter++, readMessage, mConnectedDeviceName));
break;
private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
        // Get the BluetoothSocket input and output streams (BluetoothSocket 입력 및 출력 스트림 가져 오기)
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
        }
        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[1024];
        int bytes;

        // Keep listening to the InputStream while connected (연결된 상태에서 InputStream 수신 대기)
        while (true) {
            try {
                // Read from the InputStream (InputStream에서 읽기)
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI Activity (얻은 바이트를 UI Activity로 보낸다.)
                mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1,buffer)
                        .sendToTarget();

            } catch (IOException e) {
                connectionLost();
                break;
            }
        }
    }
코드좀 봐주세여 ㅠㅠ제발...렁러
히히ㅇㅇ 님이 2018년 1월 31일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...