마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

블루투스 수신데이터를 받을때 ����������가 출력이 됩니다.

0 추천

 

아두이노와 안드로이드 간의 블루투스 통신을 블루투스 터미널 앱 테스트 후 

제작한 앱 테스트를 진행을 하였는데요. 

로그 캣 창에 다음과 같이 출력이 되었습니다.

Content_Main received : C����������������������������    

Content_Main received : C����������������������������

Content_Main received : C����������������������������

D/Content_Main: Content_Main received : urrent spe�������������������
    Content_Main received : urrent spe����������������������
    Content_Main received : urrent spe����������������������

수신부 코드

void OnBluetoothMessage(String p_received) {

    Log.d("Content_Main","Content_Main received : "+p_received );
    Log.d("Content_Main","Content_Main received : "+p_received );
    Log.d("Content_Main","Content_Main received : "+p_received );

    Calendar calendar = Calendar.getInstance();            // 캘린더 동기화 선언
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.KOREA);  // 한국 날짜형식으로 변경
    String formattedDate = simpleDateFormat.format(calendar.getTime());
    databaseReference = FirebaseDatabase.getInstance().getReference();


    databaseReference.child("데이터").child(formattedDate).setValue(mRead);   // 데이터 베이스에 센서 데이터라는 경로에 mRead_X값 저장
    databaseReference.child("타이머").child(formattedDate).setValue(mMinutes);


}

 

메세지 수신

@Override
public void handleMessage(Message p_message) {
    try{
        switch (p_message.what){

            case Constants.MESSAGE_WRITE :
                break;

            case Constants.MESSAGE_READ :       // 메시지 수신
                //callback.onReceivedMsg(ByteReader.readByte(getApplicationContext(),(byte[])p_message.obj));
                callback.onReceivedMsg(new String((byte[]) p_message.obj, "UTF-8"));

                break;

            case Constants.MESSAGE_DEVICE_NAME:     // 연결된 장치정보 수신

                String tmp_deviceAddress = p_message.getData().getString(Constants.DEVICE_ADDRESS);
                String tmp_deviceName = p_message.getData().getString(Constants.DEVICE_NAME);

                if(tmp_deviceAddress != null && tmp_deviceName != null){
                    connectionInfo.setDeviceInfo(tmp_deviceName, tmp_deviceAddress);
                }

                break;

            case Constants.MESSAGE_STATE_CHANGE :       // 블루투스 연결 상태변경 수신

                bluetoothState = p_message.arg1;

                if(bluetoothState == BluetoothManager.STATE_CONNECTED){
                    callback.onConnected();
                }else if(bluetoothState == BluetoothManager.STATE_FAILED){
                    callback.onConnectFailed();
                }

                break;
        }

앱에서 문제 인거 같은데 어떻게 수정을 해야 할까요?

 

 

익명사용자 님이 2018년 7월 31일 질문

2개의 답변

0 추천
인코딩/디코딩 을 알아보심이..
개발자초심 (21,220 포인트) 님이 2018년 7월 31일 답변
0 추천

블루투스 통신할때 (특히 C/C++에서 바이트 배열을 받을 때)  바이너리 데이터 로그를 찍으려면 아래와 같이 변환이 필요합니다.  

public String toTextData(byte[] data, boolean isHexFormat) {
    StringBuffer sb = new StringBuffer();
    for(int i=0;i<data.length;++i) {
        if (i != 0) {
            sb.append(",");
        }
        if (isHexFormat) {
            sb.append(Integer.toHexString( 0xFF& data[i]));
        } else {
            sb.append(0xFF & data[i]);
        }
    }

    return sb.toString();
}
luxsoft (1,780 포인트) 님이 2018년 7월 31일 답변
...