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

블루트스 데이터를 실시간으로 db에 저장하고있는데요

0 추천

근데 이게 문제가 블루투스에서 보내는 모든 데이터를 받고있어서

GC_FOR_ALLOC freed이 로그캣에 출력이 됩니다.

그래서 데이터를 무식하게 받는게 아니라 0.5초마다라 저장하라고 지정하고 싶은데

어떻게 하면 될까요?

메인 액티비티

final DBHelper dbHelper = new DBHelper(getApplicationContext(), "angle.ab",null,1);
final TextView result = (TextView) findViewById(R.id.text1);

    mHandler = new Handler(){    //블루투스 데이터 수신 핸들러
    public void handleMessage(Message msg){

        if(msg.what == MESSAGE_READ){

            try {
                readMessage = new String((byte[]) msg.obj, "UTF-8");
                if((readMessage.indexOf("s") == 0) && (readMessage.indexOf("e") == 21))  {
                    if(readMessage != null) { // readmessage와 null(빈값)은 같지 않다
                        test = readMessage.split("");
                        str00 = test[0]+test[2]+test[3]+test[4]+test[5]+test[6]+test[7]+test[8]+test[9];
                        str0 = readMessage.substring(0,22);
                        str1 = readMessage.substring(1,7);                 // indexof - 데이터값이 01234 56789 00000 출력될때substring(0,5)일때
                        str2 = readMessage.substring(8,14);                // ex) substring(0,5)이면 01234까지
                        str3 = readMessage.substring(16,21);

                        // 모듈에서 넘어오는 데이터에 부호(+,-)값이 포함 되있음
                        // indexof에 부호값까지 포함함

                    }
                    else if(readMessage == null){
                        readMessage = str0;
                    }

                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            mReadBuffer.setText(str0);
            mRead_X.setText(str1);             // setText str1값을 mRead_X에 출력
            mRead_Y.setText(str2);
            mRead_Z.setText(str3);

            String readBuffer_X = mRead_X.getText().toString();  // db에 저장
            String readBuffer_Y = mRead_Y.getText().toString();
            String readBuffer_Z = mRead_Z.getText().toString();

        }

        if(msg.what == CONNECTING_STATUS){
            if(msg.arg1 == 1)
                mBluetoothStatus.setText("Connected to Device: " + (String)(msg.obj));
            else
                mBluetoothStatus.setText("Connection Failed");
        }

 

public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream   스트림을 위한 버퍼 저장소
            int bytes; // bytes returned from read()                            읽기()에서 반환된 바이트
            // Keep listening to the InputStream until an exception occurs      예외가 발생할 때까지 InputStream을 계속 listening
            while (true) {
                try {
                    // Read from the InputStream   InputStream에서 읽기
                    bytes = mmInStream.available();
                    if(bytes != 0) {
                        buffer = new byte[1024];
                        SystemClock.sleep(500);
                        /*Arrays.fill(buffer, (byte)0x00);
                        SystemClock.sleep(100);
                        // pause and wait for rest of data. Adjust this depending on your sending speed.
                        // 나머지 데이터를 일시 중지하고 기다립니다. 전송 속도에 따라 이 설정을 조정합니다.*/
                        bytes = mmInStream.available(); // how many bytes are ready to be read?
                        bytes = mmInStream.read(buffer,0,buffer.length); // record how many bytes we actually read
                        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                                .sendToTarget(); // Send the obtained bytes to the UI activity
                    }
                } catch (IOException e) {
                    e.printStackTrace();

                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(String input) {
            byte[] bytes = input.getBytes();           //converts entered String into bytes
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) { }
        }

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }

    }
}

 

 

익명사용자 님이 2018년 5월 16일 질문
2018년 5월 16일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...