title: 1024 buffer in Bluetooth chat chunks my information ;-(
body: <p>I use some of the Bluetooth chat samplecode for sending a SMALL (177 byte to 3617 byte)  "settings-file" "securly" between apps.
  when it is under 1024 bit everything works fine: (so the 177 works PERFECT)
  sendingdevice press "send button" and the reciver gets it (with a dialog if they want it..) (and I save the "string" to a "settings"file on that device)</p>
  
  <p>but if the file is over 1024 it gets chunkt/cut off.. (example: 2000byte)
  so the file gets corrupted (data-loss but some info remains..)</p>
  
  <p>Probably I need to "split" my file in 1024 bits and send the bits and in the receiver-end, I need to "add them all up"..</p>
  
  <p>but I don't know the "standard best practices" for this, do you have any suggestions? </p>
  
  <p>I have tryed to "only higher" the 1024 byte to 65536byte, but that don't work..
  (or maby I do this wrong..)</p>
  
  <pre><code>public void run() {
          Log.i(TAG, "BEGIN mConnectedThread");
          byte[] buffer = new byte[1024];
          int bytes;
          // Keep listening to the InputStream while connected
          while (true) {
              try {
                  // Read from the InputStream
                  bytes = mmInStream.read(buffer);
  
                  // Send the obtained bytes to the UI Activity
                  mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                          .sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "disconnected", e);
                  connectionLost();
                  // Start the service over to restart listening mode
                  BluetoothChatService.this.start();
                  break;
              }
          }
      }
  </code></pre>
  
  <p>sedan write:</p>
  
  <pre><code>/**
       * Write to the connected OutStream.
       * @param buffer  The bytes to write
       */
      public void write(byte[] buffer) {
          try {
              mmOutStream.write(buffer);
              mmOutStream.flush();
  
              // Share the sent message back to the UI Activity
              mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
                      .sendToTarget();
          } catch (IOException e) {
              Log.e(TAG, "Exception during write", e);
          }
      }
  </code></pre>
  
  <p>and when i "click on send settings":</p>
  
  <pre><code>String message = view.getText().toString();
              String settingInAString = getSettingInALargeString();
              sendMessage(settingInAString);
  </code></pre>
  
  <p>and in "sendMessage":</p>
  
  <pre><code>if (message.length() &gt; 0) {
          // Get the message bytes and tell the BluetoothChatService to write
          byte[] send = message.getBytes();
          mChatService.write(send); //SO convert to byte and then send the byte..
  
          // Reset out string buffer to zero and clear the edit text field
          mOutStringBuffer.setLength(0);
          mOutEditText.setText(mOutStringBuffer);
      }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>    /**
   * Write to the ConnectedThread in an unsynchronized manner
   * @param out The bytes to write
   * @see ConnectedThread#write(byte[])
   */
  public void write(byte[] out) {
      // Create temporary object
      ConnectedThread r;
      // Synchronize a copy of the ConnectedThread
      synchronized (this) {
          if (mState != STATE_CONNECTED) return;
          r = mConnectedThread;
      }
      // Perform the write unsynchronized
      r.write(out);
  }
  </code></pre>
  
  <p>but I think you know what Im lookning for ...
  (or can I some how change the "BluetoothChat" so it can sent and recive a large Sring, and not "byte:s"? :-)  )</p>
  
  <p>Best REGARDS to you all :-)</p>
  
  <p>EDIT:
  on the reader side I have this:</p>
  
  <p>on the "reader end" I have:
  ....</p>
  
  <pre><code>     case MESSAGE_READ:
      byte[] readBuf = (byte[]) msg.obj;
  //only a byte redebuffer, hmm can I change this? or do i use a whileloop?
      // construct a string from the valid bytes in the buffer
      String readMessage = new String(readBuf, 0, msg.arg1);
      recivedStringCheckFirst(readMessage); 
  //simple-check if the data is a "data-setting-file"
      String [] allSettingsInALargeArray1 = doSplitOnLargeString(readMessage);
      int titleArrayLength1 = getLengthOffTheUpCommingTitleArrayFromNew(allSettingsInALargeArray1); //this do a split and looks if it is 1,2,3..20 settings..) 
      mConversationArrayAdapter.add(titleArrayLength1 + " datasettings recived from " + mConnectedDeviceName + " SAVE THIS?"); 
  //this type this text to the "chatwindow"               
                      break;
  </code></pre>
  
  <p>Here is the splitting-chunk-problem now..
  if i send under ~ 1024 I receive the correct amount of settings ant i can save this fine :-)</p>
  
  <p>If i sent larger then 1024 I get first for exampel "6 settings from.."  and then a new message that I recived "1 settings from.." message :-(</p>
  
  <p>just for your info:</p>
  
  <pre><code>protected void recivedStringCheckFirst(String readMessage) {
          String eventuellSettings = readMessage;
  
          if (isThisASettingFile(eventuellSettings)){
              //of ok..
              System.out.println("incommingISAsetting :-) ");
  
              inkommenSettings = eventuellSettings;
              showDialog(); //dialog for save settings?
          }
          if (!isThisASettingFile(eventuellSettings)){
              //not a settingsfile! 
              Toast.makeText(this, "try again..", Toast.LENGTH_LONG).show();
          }
  
  
      }
  </code></pre>
  
  <p>so i think it is:
  case MESSAGE_READ: 
  is not only called if a complete file is received,
   it is also called if a small chunks is received. 
  So I probably should place the "readFile-chunk" in a separate buffer 
  (i.e. mNewBufForFile += readFileChunk) 
  And then check the mNewBufForFile has a complete packet in it (how?). If it is done: I "save" the file message and then clear all buffer. </p>
  
  <p>but how can i "split this from "Message_read", and do I "add a stopping bit" so i can check when i recive all the data? or can i do this better?</p>