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

블루투스로 받은 문자열 저장하는 법

0 추천

안녕하세요

제가 아두이노를 이용해서 블루투스로 문자열(Aa, Ea, Ha)를 전송하면

안드로이드가 문자열에 따라 어플 버튼을 누르는 작업을 만들고 있습니다.

이 작업은 잘 되고 있는데

전송받은 문자열을 txt나 문자파일을 이용해서

저장, 로드하고 싶습니다.

어떤 식으로 저장하면 될지 모르겠어서

도움을 청합니다.

 

// 문자열 수신 쓰레드
        mWorkerThread = new Thread(new Runnable() {

            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        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 == mCharDelimiter) {
                                    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() {
                                        public void run() { // 수신된 문자열 데이터에 대한 처리 작업


                                            if(readBuffer[0]=='A') {
                                                base.performClick();
                                            }else if(readBuffer[0]=='B'){
                                                snare.performClick();
                                            }else if(readBuffer[0]=='E'){
                                                tom.performClick();
                                            }else if(readBuffer[0]=='K'){
                                                hithat.performClick();
                                            }else if(readBuffer[0]=='S'){
                                                crash.performClick();;
                                            }else if(readBuffer[0]=='T'){
                                                restart1.performClick();
                                            }
                                        }
                                    });
                                } else {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }

                    } catch (IOException ex) { // 데이터 수신 중 오류 발생
                        Toast.makeText(getApplicationContext(), "데이터 수신 중 오류가 발생", Toast.LENGTH_LONG).show();
                        finish();
                    }
                }
            }
        });
        mWorkerThread.start();
    }
익명사용자 님이 2016년 11월 13일 질문

답변 달기

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