得到缓冲区java的一半值

时间:2017-07-07 12:18:42

标签: java android bluetooth

我希望从缓冲区获取值,直到半值:

public void run() {
                byte[] buffer = new byte[1048576]; // 20 bits
                int bytes;
                String strRx = "";

                while (running) {
                    try {
                        bytes = connectedInputStream.read(buffer);
                        final String strReceived_freq = new String(buffer,0, buffer/2); // buffer/2 not working
                        final String strReceived_pxx = new String(buffer,buffer/2, bytes); // buffer/2 not working
                        //final int samples_sdr=new Integer(buffer,0,bytes);
                        final String strByteCnt = String.valueOf(bytes) + " bytes received.\n";

问题,缓冲区/ 2无法正常工作,如何从缓冲区中获取半值?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

public void run() {
   byte[] buffer = new byte[1048576]; // 20 bits
   int bytes = 0;
   String strRx = "";
   while (running) {
      try {
         bytes = connectedInputStream.read(buffer);
         if(bytes == 0) {
            Log.d("DEBUG","connectedInputStream returned 0 bytes");
         }   
         byte[] strReceived_freq_byte = new byte[(buffer.length)/2];
         for(int i = 0; i < (buffer.length)/2; i++) {
            strReceived_freq_byte[i] = buffer[i];
         }
         byte[] strReceived_pxx_byte = new byte[(buffer.length)/2];
         for(int i =0 ; i < (buffer.length)/2; i++) {
            strReceived_freq_byte[i] = buffer[i+((buffer.length)/2)];
         }
     String strReceived_freq = new String(strReceived_freq_byte);
     String strReceived_pxx = new String(strReceived_pxx_byte);
     final String strByteCnt = String.valueOf(bytes) + " bytes received.\n";
      }  
   }  
}