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

블루투스 채팅 리시브 할때 bytequeue를 사용해서 데이터를 받으려고하는데 오류가 나서 정확한 원인을 모르겠습니다. 도와주세요

0 추천
<ByteQueue.java>

package com.example.water_bottle;

public class ByteQueue {
    /**
     * Constructor for ByteQueue.
     * @param size int
     */
    public ByteQueue(int size) {
        mBuffer = new byte[size];
    }

    /**
     * Method getBytesAvailable.
   
     * @return int */
    public int getBytesAvailable() {
        synchronized(this) {
            return mStoredBytes;
        }
    }

    /**
     * Method read.
     * @param buffer byte[]
     * @param offset int
     * @param length int
   
   
     * @return int * @throws InterruptedException */
    public int read(byte[] buffer, int offset, int length)
        throws InterruptedException {
        if (length + offset > buffer.length) {
            throw
                new IllegalArgumentException("length + offset > buffer.length");
        }
        if (length < 0) {
            throw
            new IllegalArgumentException("length < 0");

        }
        if (length == 0) {
            return 0;
        }
        synchronized(this) {
            while (mStoredBytes == 0) {
                wait();
            }
            int totalRead = 0;
            int bufferLength = mBuffer.length;
            boolean wasFull = bufferLength == mStoredBytes;
            while (length > 0 && mStoredBytes > 0) {
                int oneRun = Math.min(bufferLength - mHead, mStoredBytes);
                int bytesToCopy = Math.min(length, oneRun);
                System.arraycopy(mBuffer, mHead, buffer, offset, bytesToCopy);
                mHead += bytesToCopy;
                if (mHead >= bufferLength) {
                    mHead = 0;
                }
                mStoredBytes -= bytesToCopy;
                length -= bytesToCopy;
                offset += bytesToCopy;
                totalRead += bytesToCopy;
            }
            if (wasFull) {
                notify();
            }
            return totalRead;
        }
    }

    /**
     * Method write.
     * @param buffer byte[]
     * @param offset int
     * @param length int
   
     * @throws InterruptedException */
    public void write(byte[] buffer, int offset, int length)
    throws InterruptedException {
        if (length + offset > buffer.length) {
            throw
                new IllegalArgumentException("length + offset > buffer.length");
        }
        if (length < 0) {
            throw
            new IllegalArgumentException("length < 0");

        }
        if (length == 0) {
            return;
        }
        synchronized(this) {
            int bufferLength = mBuffer.length;
            boolean wasEmpty = mStoredBytes == 0;
            while (length > 0) {
                while(bufferLength == mStoredBytes) {
                    wait();
                }
                int tail = mHead + mStoredBytes;
                int oneRun;
                if (tail >= bufferLength) {
                    tail = tail - bufferLength;
                    oneRun = mHead - tail;
                } else {
                    oneRun = bufferLength - tail;
                }
                int bytesToCopy = Math.min(oneRun, length);
                System.arraycopy(buffer, offset, mBuffer, tail, bytesToCopy);
                offset += bytesToCopy;
                mStoredBytes += bytesToCopy;
                length -= bytesToCopy;
            }
            if (wasEmpty) {
                notify();
            }
           
        }
    }

    private byte[] mBuffer;
    private int mHead;
    private int mStoredBytes;
}

 

<BluetoothService> 의 ConnectedThread

       public void run() {
           Log.i(TAG, "BEGIN mConnectedThread");
          
           byte[] buffer = new byte[1024];
           int bytes;
          
           while (true) {
               try {
                  bytes = mmInStream.read(buffer);
                  
                  mByteQueue.write(buffer, 0, bytes);
                  
                  mHandler.sendMessage(mHandler.obtainMessage(ServiceClass.MESSAGE_READ));
               }catch (InterruptedException e) {
                   //@TODO log the exception;
                  break;
               }catch (IOException e) {
                   Log.e(TAG, "disconnected", e);
                   connectionLost();
                   break;
               }
           }
       }

 

로그창 오류

12-22 18:14:48.661: E/AndroidRuntime(17940): FATAL EXCEPTION: Thread-10273
12-22 18:14:48.661: E/AndroidRuntime(17940): Process: com.example.water_bottle, PID: 17940
12-22 18:14:48.661: E/AndroidRuntime(17940): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.water_bottle.ByteQueue.write(byte[], int, int)' on a null object reference
12-22 18:14:48.661: E/AndroidRuntime(17940):  at com.example.water_bottle.BluetoothService$ConnectedThread.run(BluetoothService.java:397)
쿠쿠부다스 (6,470 포인트) 님이 2015년 12월 22일 질문

답변 달기

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