title: Picture transfer on Bluetooth sockets from Android to Windows
body: <p>I can't find a solution for this problem: I made a program on android to shot a picture (jpg format) and send it over the Bluetooth channel to an Embedded Windows XP pc. I also made the C++ application for Windows to receive the picture.<br>
  The communication is implemented using Bluetooth sockets.
  In Android the app reads the file in chunks of 1024 bytes and write every chunk on the socket until the picture is finished.
  On Windows side I read the bytes in chunck of 1024 bytes until the recv(...) function returns a value > 0.  </p>
  
  <p>The result is that on Android side it seems all the bytes are correctly read and sent, but on the Windows side I (almost) always receive only 70-80% of the bytes. I can even see the correct bytes received, as it is possible to visualize the jpg (missing bytes on the bottom part of the image are shown in grey). Only rarely I can receive the entire picture.
  Here I post the relevant part of code for the Android side:  </p>
  
  <pre><code>String picturePath = "/sdcard/VERDE/PTR-0_15-31-24.jpg";
  File picture = new File(picturePath);
  if (picture.exists())
  {
      Log.d(TAG, "File " + picture.getAbsolutePath() + " exists!");
      try
      {
          FileInputStream fis = new FileInputStream(picture);
          ostream = socket.getOutputStream();
          byte[] buf = new byte[1024];
          int read = 0;
          int totBytes = 0;
          while ((read = fis.read(buf)) != -1)
          {
              totBytes = totBytes + read;
              ostream.write(buf, 0, read);
              Log.d(TAG, "Image - Read: " + read + " - Total "
                                  + totBytes + " bytes!");
          }
          ostream.flush();
          ostream.close();
          fis.close();
      } catch (UnknownHostException ex)
      {
          System.out.println(ex.getMessage());
      } catch (IOException ex)
      {
          System.out.println(ex.getMessage());
      }
  } else
  {
      Log.d(TAG, "File " + picture.getAbsolutePath()
              + " does not exist!");
  }
  </code></pre>
  
  <p>This is the relevant part of code of the C++ application:  </p>
  
  <pre><code>if (_outFile != NULL) 
  {
      _outFile.open(result.c_str(), ofstream::binary); //"pic.jpg"
      cout &lt;&lt; "File opened!" &lt;&lt; endl;
  } else 
  {
      cout &lt;&lt; "Can't open file!" &lt;&lt; endl;
  }
  while ((received = recv(client, buf, sizeof(buf), 0)) &gt; 0) 
  {
      cout &lt;&lt; "R:" &lt;&lt; received &lt;&lt; " ";
      if (received &gt; 0) 
      {
          totalBytes += received;
          if (_outFile.is_open()) 
          {
              _outFile.write(buf, received); 
              cout &lt;&lt; " (Total: " &lt;&lt; totalBytes &lt;&lt; " B)" &lt;&lt; endl;
          } else
          cout &lt;&lt; "Error in recv() function, received bytes = "
                          &lt;&lt; received &lt;&lt; endl;
  
      } else 
          cout &lt;&lt; "R:" &lt;&lt; received &lt;&lt; " ";
  }
  </code></pre>
  
  <p>I've seen tens of blogs and posts on this topic but noone seems to have a similar problem!
  Please, help me if you have any idea!   </p>
  
  <p>Thanks!</p>