【问题标题】:get half value of buffer java获取缓冲区java的一半值
【发布时间】:2017-12-11 17:46:36
【问题描述】:

我想从我的缓冲区中获取价值,直到一半的价值:

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";

问题,buffer/2 不工作,我怎样才能从我的缓冲区中获取一半的值?

感谢您的帮助!

【问题讨论】:

  • 这个命令完全落后于我的应用程序。我不明白为什么
  • 也许您的意思是 bytes/2 而不是 buffer/2。这将在每个字符串中放入一半的字节。
  • 是的,对不起,我的意思是 bytes/2 而不是 buffer/2 但我有一些问题,我不知道这是否与我的缓冲区有关,但对于我的选项卡的每种情况,我得到 2选项卡再次写入时的相同时间值(我的频率和我的 pxx 值)
  • 在我不知道它是否与我的缓冲区有关之后,它一直有效到 33 点

标签: java android bluetooth


【解决方案1】:
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";
      }  
   }  
}

【讨论】:

  • 我将其转换为字符串: final String strReceived_freq2= new String(strReceived_freq) ?因为当我这样做时,我什么也得不到
  • 可以连接InputStream.read(buffer);不返回任何字节。检查该日志 int bytes 变量的值。如果它为零,那么它确实没有返回任何字节。
猜你喜欢
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多