title: how to connect a device send a message then close the connection of BlueTooth in android application?
body: <p>I have with an blue tooth application from android api sample.I am displaying blue tooth paired device in a list when the user selects a device try for send a message to selected paired device then that message should deliver to that device.From the a this specification i would like to develop as when user checks the paired device then type a message in edit field then click on send button.when user click on send button then the user can connect to selected device then send that message to that device then if delivery is success immediately close the connection.
     I have implemented code as follows</p>
  
  <pre><code> ListView listDevicesFound;
   ArrayAdapter&lt;String&gt; btArrayAdapter;
   @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // Set up the window layout
      requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
      setContentView(R.layout.main);
      btArrayAdapter = new ArrayAdapter&lt;String&gt;(BluetoothChat.this, android.R.layout.simple_list_item_multiple_choice);
      listDevicesFound.setAdapter(btArrayAdapter);
      listDevicesFound.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
      listDevicesFound.setSelected(true);
      listDevicesFound.setClickable(true);
      Set&lt;BluetoothDevice&gt; pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() &gt; 0) {
            for (BluetoothDevice device : pairedDevices) {
             String deviceBTName = device.getName();
             String deviceBTAddress = device.getAddress();
             btArrayAdapter.add(deviceBTName + "\n"  + deviceBTAddress);
            }
        }
  
        mSendButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
  
  
            SparseBooleanArray checked = listDevicesFound.getCheckedItemPositions();
              Set&lt;BluetoothDevice&gt; devices = btAdapter.getBondedDevices();
              Log.v("prasad", "devices.size()===&gt;&gt;&gt;"+devices.size());
              for (int j=0;j&lt;=devices.size();j++) 
                 {
                      System.out.println(j);
                      if(checked.get(j))
                      {
                          String devadd= listDevicesFound.getItemAtPosition(j).toString();
                          String devaddress=devadd.substring(0,devadd.length()-17);
                          Log.v("prasad", "address===&gt;&gt;&gt;"+devaddress);
  
  
                          /*
                           *BluetoothDevice:
                           *Represents a remote Bluetooth device. 
                           *A BluetoothDevice lets you create a connection with the respective device 
                           *or query information about it, such as the name, address, class, 
                           *and bonding state 
                           */
  
                          for(BluetoothDevice itDevices:devices)
                          {
                              Log.v("prasad", "itDevices.getAddress()===&gt;&gt;&gt;"+itDevices.getAddress());
                              if(devadd.endsWith(itDevices.getAddress()))
                              {
  
  
  
                                              Log.v("1111","Here the adderess of selected device :"+itDevices.getAddress());
  
  
               }
           }
        }
     }
  
  
  
  
           //Please help on following steps
           //1.How to connect to a selected device code here                
  
           String message = messageEdit.getText().toString();
           //2.How to send the message to selected device here
  
           //3.How to close the connection with selected device here
  
  
  
              }
      });
  
  
    }  
  </code></pre>
  
  <p>please follow my comments in coding lines</p>
  
  <p>please any body help me....</p>