안드로이드 블루투스 수신 부분 질문드립니다
조회수 1749회
public void beginListenForData(){ final Handler handler = new Handler(); readBuffer= new byte[1024]; tv = (TextView)findViewById(R.id.test); mWorkerThread = new Thread(new Runnable(){ public void run(){ while(!Thread.currentThread().isInterrupted()){ try{ Toast.makeText(MainActivity.this,"테스트",Toast.LENGTH_SHORT).show(); int bytesAvailable = mInputStream.available();
if(bytesAvailable > 0){
byte[] packetBytes = new byte[bytesAvailable];
mInputStream.read(packetBytes);
for(int i = 0; i < bytesAvailable; i++){
byte b = packetBytes[i];
if(b == mDelimeter){
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes,0,encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable() {
@Override
public void run() {
tv.setText(data.toString());
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
} catch(Exception e){
}
}
}
});
mWorkerThread.start();
}
아두이노와 연결을 하였고 아두이노 소스는 이러합니다
include
int blueTx = 2; int blueRx = 3; SoftwareSerial mySerial(blueTx,blueRx); void setup() { // put your setup code here, to run once: Serial.begin(9600); mySerial.begin(9600); }
void loop{ mySerial.write("hello\n"); }
안드로이드에 수신이 된다면 tv라는 텍스트뷰에 hello라는 문자가 나타나는데 안나타나네요
여기서 뭐가 문제일까요?
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력