안드로이드와 터미널 사이에서 블루투스 통신으로 텍스트를 주고받는데
터미널에서 텍스트를 안드로이드로 보내면 이어져서 전송되야할 글자가 한글자씩 보내집니다..
왜그런걸까요?
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;
}
}
}
코드좀 봐주세여 ㅠㅠ제발...렁러