이 소스는 블루투스 통신 소스중에 일부분인데요 이 소스를 조금 수정해볼려고 하는데..계속 실패만 하네요 ㅠㅠ
1)한쪽에서 test 라는 문자를 전송하면 다른 한쪽에서 TEST라는 문자를 출력하도록 소스 수정
2)한쪽에서 소문자를 보내면 다른 한쪽에서는 대문자로 출력 (이건 해결!아주 간단한 거였네요 ㅎㅎ)
이렇게 두개를 만드려고 하는데 잘 되지 않네요 ㅠㅠ 고수님들 도와주세요ㅠㅠ
// 메시지를 화면에 표시
public void showMessage(String strMsg) {
// 메시지 텍스트를 핸들러에 전달
Message msg = Message.obtain(mHandler, 0, strMsg);
mHandler.sendMessage(msg);
Log.d("tag1", strMsg);
}
// 메시지 화면 출력을 위한 핸들러
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0) {
String strMsg = (String)msg.obj;
mTextMsg.setText(strMsg);
}
}
};
// 원격 디바이스와 접속되었으면 데이터 송수신 스레드를 시작
public void onConnected(BluetoothSocket socket) {
showMessage("Socket connected");
// 데이터 송수신 스레드가 생성되어 있다면 삭제한다
if( mSocketThread != null )
mSocketThread = null;
// 데이터 송수신 스레드를 시작
mSocketThread = new SocketThread(socket);
mSocketThread.start();
}
// 데이터 송수신 스레드
private class SocketThread extends Thread {
private final BluetoothSocket mmSocket; // 클라이언트 소켓
private InputStream mmInStream; // 입력 스트림
private OutputStream mmOutStream; // 출력 스트림
public SocketThread(BluetoothSocket socket) {
mmSocket = socket;
// 입력 스트림과 출력 스트림을 구한다
try {
mmInStream = socket.getInputStream();
mmOutStream = socket.getOutputStream();
} catch (IOException e) {
showMessage("Get Stream error");
}
}
// 소켓에서 수신된 데이터를 화면에 표시한다
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
// 입력 스트림에서 데이터를 읽는다
bytes = mmInStream.read(buffer);
String strBuf = new String(buffer, 0, bytes);
showMessage("Receive: " + strBuf);
SystemClock.sleep(1);
} catch (IOException e) {
showMessage("Socket disconneted");
break;
}
}
}
// 데이터를 소켓으로 전송한다
public void write(String strBuf) {
try {
// 출력 스트림에 데이터를 저장한다
byte[] buffer = strBuf.getBytes();
mmOutStream.write(buffer);
showMessage("Send: " + strBuf);
} catch (IOException e) {
showMessage("Socket write error");
}
}
}
// 버튼 클릭 이벤트 함수
public void onClick(View v) {
switch( v.getId() ) {
case R.id.btnSend : {
// 데이터 송수신 스레드가 생성되지 않았다면 함수 탈출
if( mSocketThread == null ) return;
// 사용자가 입력한 텍스트를 소켓으로 전송한다
String strBuf = mEditData.getText().toString();
if( strBuf.length() < 1 ) return;
mEditData.setText("");
mSocketThread.write(strBuf);
break;
}
}
}