0

I wouldike to get value from my buffer until half values :

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

The problem, buffer/2 is not working, how can I do that for get half values from my buffer ?

Thanks for your help !

azzerty2017
  • 43
  • 1
  • 1
  • 9
  • Possible duplicate of [Java String array: is there a size of method?](https://stackoverflow.com/questions/921384/java-string-array-is-there-a-size-of-method) – Klas Lindbäck Jul 07 '17 at 12:21
  • this command is lagging completly my app. I don't understand why – azzerty2017 Jul 07 '17 at 12:28
  • Maybe you mean `bytes/2` instead of `buffer/2`. That would put half the bytes in each string. – Klas Lindbäck Jul 07 '17 at 12:39
  • yeah sorry I mean bytes/2 instead of buffer/2 but I have some problem, I don't know if it's about my buffer or not, but for each case of my tab, I get 2 values in sames times (my frequence, and my pxx value) as the tab is writing again – azzerty2017 Jul 07 '17 at 12:59
  • It works until 33 exactly, after I don't know if it's about my buffer or not – azzerty2017 Jul 07 '17 at 13:45
  • What does "not working" mean? Is it throwing an exception? Returning incorrect results? – ldam Jul 07 '17 at 14:57
  • this is not working because I get 2 values in sames times (my frequence, and my pxx value). When I fix my nb_point to until 33 points, I've got correctly values but more than 33, I get value like messy – azzerty2017 Jul 07 '17 at 17:37
  • Please fix the question; do not rely on would-be answerers to wade through all the comments to figure out what you're *really* asking. – dcsohl Jul 07 '17 at 18:21
  • Sorry I did this post before maybe it can help you to understood my problem : https://stackoverflow.com/questions/44968232/send-correctly-data-between-python-and-java-by-bluetooth – azzerty2017 Jul 07 '17 at 22:54

1 Answers1

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";
      }  
   }  
}
Ali
  • 839
  • 11
  • 21
  • I convert that to string with this : final String strReceived_freq2= new String(strReceived_freq) ? Because when I do that, I receive nothing – azzerty2017 Jul 07 '17 at 12:49
  • May be connectedInputStream.read(buffer); is not returning any bytes. To check that log the value of int bytes variable. If it is zero then indeed it is not returning any bytes. – Ali Jul 07 '17 at 13:02