현재 Arduino에서 Bluesmirf 모듈을 사용하여 Android로 데이터를 가져 오려고합니다.Android 블루투스 Arduino
여기 내 Arduino 코드입니다. 당신이 볼 수 있듯이
void setup() {
Serial.begin(115200);
}
void loop() {
if(Serial.available()){
char val = Serial.read();
if(val == '.'){
Serial.println("t1|x1|x2|x3|x4");
}
}
}
, 난 그냥 긴 문자열을 쓰는 데 문제가 있습니다. 결국 문자열에는 값이 포함됩니다. arduino에 마침표를 쓰면 값을 반환합니다. 여기 내 블루투스 코드는 블루투스 채팅 샘플에있는 것과 매우 비슷합니다.
private class ConnectedThread extends Thread{
private BluetoothSocket mmSocket;
private InputStream mmInStream;
private OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
// TODO Auto-generated constructor stub
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (Exception e) {
// TODO: handle exception
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run(){
byte[] buffer = new byte[1024];
int bytes;
while(true){
try {
// Garbage collector necessary to prevent data loss
System.gc();
bytes = mmInStream.read(buffer);
Log.d("Value of Output", new String(buffer, 0, bytes))
} catch (IOException e) {
e.printStackTrace();
connectionLost();
}
}
}
public void write(byte[] buffer){
try{
mmOutStream.write(buffer);
} catch(IOException e){
Log.d("Write", "failed");
}
}
public void cancel(){
if (mmInStream != null) {
try{mmInStream.close();}catch(Exception e){}
mmInStream = null;
}
if (mmOutStream != null) {
try{mmOutStream.close();} catch(Exception e){}
mmOutStream = null;
}
if (mmSocket != null) {
try{mmSocket.close();}catch(Exception e){}
mmSocket = null;
}
}
}
몇 가지 언급하고 싶습니다. System.gc()는 거기에 넣지 않으면 때때로 잘못된 데이터를 가져 오기 때문에 거기에 있습니다. 때로는 데이터가 손실되고 때로는 반복됩니다.
내가 겪고있는 문제는 출력이 두 줄 이상으로 돌아옵니다. 그래서 내 로그에 내가
같은 출력 (T1)의 값을 뭔가를 얻을 수 있습니다 | X1 | X
출력 2의 값 | 한 줄에
대신 모든 X4 | X3. 블루투스 (블루투스 동글)를 통해 컴퓨터에 arduino를 연결하면 데이터가 한 줄에 다시 나타납니다. 데이터가 한 줄에 다시 표시되도록하려면 어떻게해야합니까?
하는 데 도움이 전체
희망 인 경우에만 다음,
readFully
당신에게 버퍼를 반환합니다. 나는 이것이 안드로이드 측에서 문제라고 생각한다. – user2461391