title: How to get the data from a Bluetooth Socket in Android?
body: <p>I'm new at coding in Android. I've been following the book "Professional Android 4 App Development" I am basically trying to figure out a way to receive a bunch of strings over Bluetooth sent by any device. This is what I have so far</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // Set up the window layout
      setContentView(R.layout.data);
  
      ListView myData = (ListView)findViewById(R.id.list);
      final ArrayList&lt;String&gt; mData = new ArrayList&lt;String&gt;();
      final ArrayAdapter&lt;String&gt; aa;
  
      aa = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, mData);
  
      //Bind Array to List View
  
      myData.setAdapter(aa);
  
      startDiscovery();
  
      String add = BluetoothDevice.EXTRA_DEVICE;
  
      BluetoothDevice device = mAdapter.getRemoteDevice(add);
      connectToServerSocket(device, MY_UUID);     
  
      mData.add(0, incoming);
      aa.notifyDataSetChanged();
  
  }
  </code></pre>
  
  <p>This is my onCreate and these are the classes or functions I'm not sure how to call them...</p>
  
  <pre><code>private ArrayList&lt;BluetoothDevice&gt; deviceList =
      new ArrayList&lt;BluetoothDevice&gt;();
  
      private void startDiscovery() {
          registerReceiver(discoveryResult,
          new IntentFilter(BluetoothDevice.ACTION_FOUND));
          if (mAdapter.isEnabled() &amp;&amp; !mAdapter.isDiscovering())
          deviceList.clear();
          mAdapter.startDiscovery();
          }
          BroadcastReceiver discoveryResult = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
          String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
          BluetoothDevice remoteDevice =
                  intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                  deviceList.add(remoteDevice);
                  Log.d(TAG, "Discovered"  + remoteDeviceName);
                  }
                  };
  </code></pre>
  
  <p>This is my Socket:</p>
  
  <pre><code>private void connectToServerSocket(BluetoothDevice device, UUID uuid){
  try{
          BluetoothSocket clientSocket = device.createRfcommSocketToServiceRecord(uuid);
          clientSocket.connect();
          StringBuilder incoming = new StringBuilder();
          listenforData(clientSocket, incoming);
  
          transferSocket = clientSocket;
      }catch(IOException e){
          Log.e("BLUETOOTH","Bluetooth client I/O Exception", e);
      }
  }
  </code></pre>
  
  <p>And My listen for data looks like this:</p>
  
  <pre><code>private void listenforData(BluetoothSocket socket, StringBuilder incoming){
          int bufferSize = 1024;
          byte[] buffer = new byte[bufferSize];
          try{
              InputStream instream = socket.getInputStream();
          int bytesRead = -1;
  
          while(true){
          bytesRead = instream.read(buffer);
          if (bytesRead != -1){
              String result = "";
              while((bytesRead == bufferSize)&amp;&amp;(buffer[bufferSize-1]!=0)){
              result = result + new String(buffer,0,bytesRead-1);
              bytesRead= instream.read(buffer);
              }
              result = result + new String(buffer, 0, bytesRead -1);
              incoming.append(result);
          }
          socket.close();
          }
      }catch (IOException e){
          Log.e(TAG, "Message received failed.", e);
      }
      finally{
      }
  }}
  </code></pre>
  
  <p>What I am missing is how to retrieve the strings from the socket listener and place them on the ListView. I need some guidance. Am I even doing this correctly?</p>