title: Using a read method in my android app
body: <p>So I created an application where I have a GUI class. This application will listen for 2 incoming strings from another application i created when a togglebutton is activated. What i simply cant wrap my head around is how to read the incoming bytes and pass them through my code back to where they are used in my GUI class. Hope anyone is able to help.</p>
  
  <p>When I send my two strings i get the bytes of both then send the first, get the bytes of a "%" string, send that as a seperator and then send the second.</p>
  
  <pre><code>public void ListenForAddress(View view)
  {
      on = ((ToggleButton) view).isChecked();
  
      if(on)
      {
          Address address = reciever.RecieveObject();
          Intent intent = new Intent(this, Screen3.class);
          String adressStr = address.Address;
          intent.putExtra("ADRESS_MESSAGE", adressStr);
          String postalcodeStr = address.Postalcode;
          intent.putExtra("POSTALCODE_MESSAGE", postalcodeStr);
          intent.putExtra("ONE", 1);
          startActivity(intent);
      }
      else
      {
          reciever.closeReception();
      }   
  }
  </code></pre>
  
  <p>As you can see I create a receiver and with that I call a method called RecieveObject() that method looks like the following.</p>
  
  <pre><code>public Address RecieveObject() 
  {
      accThread = new AcceptThread();
      accThread.start();
      return null;
  }
  </code></pre>
  
  <p>Next step is the thread that creates the connection and starts the managing thread</p>
  
  <pre><code>public class AcceptThread extends Thread {
  
  
  public AcceptThread() {
      BluetoothServerSocket tmp = null;
      try {
      tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("Server", MY_UUID);
      } catch (IOException e) { }
      mmServerSocket = tmp;
  }
  
  public void run() {
      BluetoothSocket socket = null;
      while (true) {
          try {
              socket = mmServerSocket.accept();
          } catch (IOException e) {
              break;
          }
          // If a connection was accepted
          if (socket != null) {
              mConnSock = new manageConnectedSocket(socket);
              mConnSock.read();
              try {
                  mmServerSocket.close();
              } catch (IOException e) {
              }
              break;
          }
      }
  }
  }
  </code></pre>
  
  <p>The final step is the thread handling the data</p>
  
  <pre><code>public class manageConnectedSocket extends Thread {
  
  public manageConnectedSocket(BluetoothSocket socket) {
      mmSocket = socket;
      InputStream tmpIn = null;
  
      try {
          tmpIn = socket.getInputStream();
      } catch (IOException e) { }
  
      mmInStream = tmpIn;
  }
  
  public Address read() {
      byte[] buffer = new byte[1024];
      int bytes; 
      Address address = new Address("", "");
      int count = 0;
  
      while(count&lt;2)
      {
          try {
              bytes = mmInStream.read(buffer);
  
              if(count==0)
              {
                  address.Address = new String(buffer);
              }
              else
              {
                  address.Postalcode = new String(buffer);
              }
              buffer = new byte[1024];
  
          } catch (IOException e) {
              break;
          }
      }
      return address;
  }
  
  }
  </code></pre>