블루투스 채팅을 바탕으로 데이터(bytes) 송수신을 구현중에 있습니다.
atmega-블루투스칩 <-> 안드로이드 앱과 블루투스 송수신을 하는데
받은데이터(MDS_data) 를 받을때 byte 78byte를 받을 때 계속 58~60byte를 이후
그뒤에 나머지 byte를 받습니다.
이럴경우 추가적으로 byte 끝을 알 수 있는 데이터를 더 추가해줘야하나요?
atmega에서 받는 데이터는 52/78/44/6 각각 다릅니다.
어떻게 해결하는 방안이 좋을지 조언부탁드립니다.
public void run() {
byte[] buffer = new byte[78]; //data길이만큼
while (true) {
try {
while (mmInStream.read(buffer)>-1) {
if(buffer[0] != (byte) 0x00 ){
if(buffer[0] == (byte) 0xE2){ //association
String[] split_address=phone_macaddress.split(":");
Log.i(TAG, "내 폰 블루투스 주소 : "+phone_macaddress);
byte bt_addr[] = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,(byte) 0x00, (byte) 0x00, (byte) 0x00 };
Log.i(TAG, "나눈 값 : "+split_address[0]+ "-"+ split_address[1]+ "-"+ split_address[2]+ "-"+ split_address[3]+ "-"+ split_address[4]+ "-"+ split_address[5]);
int bt_addr1 = Integer.parseInt( split_address[0], 16 );
int bt_addr2 = Integer.parseInt( split_address[1], 16 );
int bt_addr3 = Integer.parseInt( split_address[2], 16 );
int bt_addr4 = Integer.parseInt( split_address[3], 16 );
int bt_addr5 = Integer.parseInt( split_address[4], 16 );
int bt_addr6 = Integer.parseInt( split_address[5], 16 );
bt_addr[0] = (byte)bt_addr1;
bt_addr[1] = (byte)bt_addr2;
bt_addr[2] = (byte)bt_addr3;
bt_addr[3] = (byte)bt_addr4;
bt_addr[4] = (byte)bt_addr5;
bt_addr[5] = (byte)bt_addr6;
String ar_data = byte2hex_length(buffer,52);
Log.i(TAG, "받은데이터(ar_data) : "+ar_data);
//Log.i(TAG, "E2");
final byte data_AR[] = new byte[] {
(byte) 0xE3, (byte) 0x00,
(byte) 0x00, (byte) 0x2A,
(byte) 0x00, (byte) 0x00,
(byte) 0x50, (byte) 0x79,
(byte) 0x00, (byte) 0x24,
(byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x80, (byte) 0x00,
(byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x06,
(byte) bt_addr[0], (byte) bt_addr[1], (byte) bt_addr[2], (byte) bt_addr[3], (byte) bt_addr[4], (byte) bt_addr[5], //블루투스 주소
(byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
SystemClock.sleep(1000);
write(data_AR);
String ar_test = byte2hex(data_AR);
Log.e(TAG, "보낸값 "+ar_test);
final byte data_MDS[] = new byte[] { (byte) 0xE7, (byte) 0x00,
(byte) 0x00, (byte) 0x0E,
(byte) 0x00, (byte) 0x0C,
(byte) 0x00, (byte) 0x03,
(byte) 0x01, (byte) 0x03,
(byte) 0x00, (byte) 0x06,
(byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00 };
SystemClock.sleep(1000);
write(data_MDS);
String MDS_test = byte2hex(data_MDS);
Log.e(TAG, "보낸값 "+MDS_test);
for (int i = 0; i < buffer.length; i++){
buffer[i] = (byte) 0x00;
}
}//Measure_data
else if(buffer[0] == (byte) 0xE7){
if (buffer[18] == (byte) 0x0d && buffer[19] == (byte) 0x1d){
Log.i(TAG, "0xE7");
String mea_data = byte2hex_length(buffer,44);
Log.i(TAG, "받은데이터(mea_data) : "+mea_data);
//날짜 안드로이드에서 얻기
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.KOREA);
date=df.format(new Date());
//bytehex(buffer[34])+bytehex(buffer[35])+"-"+bytehex(buffer[36])+"-"+bytehex(buffer[37]);
Log.i("date", date);
time=bytehex(buffer[38])+":"+bytehex(buffer[39])+":"+bytehex(buffer[40]);
Log.i(TAG, "time"+time);
//약 번호
int drugnum=byteToUnsignedInt(buffer[43]);
String strdrugnum = String.valueOf(drugnum);
drugnumber=strdrugnum;
Log.i(TAG, "drugnumber"+drugnumber);
byte data_DR[] = new byte[] { (byte) 0xE7, (byte) 0x00,
(byte) 0x00, (byte) 0x12,
(byte) 0x00, (byte) 0x10,
(byte) 0x00, (byte) 0x04,
(byte) 0x02, (byte) 0x01,
(byte) 0x00, (byte) 0x0A,
(byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x0D, (byte) 0x1D,
(byte) 0x00, (byte) 0x00 };
SystemClock.sleep(1000);
write(data_DR);
String dr_test = byte2hex(data_DR);
Log.e(TAG, "보낸값 "+dr_test);
if(PatientIdInfo.getSharedPreference(btservice_context,"main_state").equalsIgnoreCase("fhir")) {
mHandler.obtainMessage(ServiceClass.CREATEMEDICATION, buffer).sendToTarget();
}
for (int i = 0; i < buffer.length; i++){
buffer[i] = (byte) 0x00;
}
}else{ //측정값이 아닐경우
String MDS_data = byte2hex_length(buffer,78);
Log.i(TAG, "받은데이터(MDS_data) : "+MDS_data);
SystemClock.sleep(5000);
for (int i = 0; i < buffer.length; i++){
buffer[i] = (byte) 0x00;
}
}
}else if (buffer[0] == (byte) 0xE4){ //disassociation
Log.i(TAG, "E4");
String da_data = byte2hex_length(buffer,6);
Log.i(TAG, "받은데이터(da_data) : "+da_data);
byte data_RR[] = new byte[] { (byte) 0xE5, (byte) 0x00,
(byte) 0x00, (byte) 0x02,
(byte) 0x00, (byte) 0x00 };
SystemClock.sleep(1000);
write(data_RR);
String rr_test = byte2hex(data_RR);
Log.e(TAG, "보낸값 "+rr_test);
for (int i = 0; i < buffer.length; i++){
buffer[i] = (byte) 0x00;
}
}
//다른 값 들어왔을 경우
else{
Log.i(TAG, "다른값 ");
String da_data = byte2hex(buffer);
Log.i(TAG, "받은데이터 : "+da_data);
for (int i = 0; i < buffer.length; i++){
buffer[i] = (byte) 0x00;
}
}
}else{
Log.i(TAG, "AR나머지값 ");
String da_data = byte2hex(buffer);
Log.i(TAG, "받은데이터 : "+da_data);
for (int i = 0; i < buffer.length; i++){
buffer[i] = (byte) 0x00;
}
}
if (mmInStream.available() == 0)
break;
}
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
prevaluedelete=false;
connectionLost();
break;
}
}
}