title: How to connect to a bluetooth device with android smartphone using only its Mac adress?
body: <p>I'm making an android app that is supposed to send values of three
   seekbars through bluetooth but I don't know how to programmatically connect it 
   to a paired bluetooth device by clicking on its name on the list that shows the paired devices
   Any help would be much appreciated.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.content.Intent;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.view.Menu;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.LinearLayout;
  import android.widget.ListView;
  import android.widget.SeekBar;
  import android.widget.TextView;
  import android.widget.Toast;
  import java.util.ArrayList;
  import java.util.Set;
  import java.util.UUID;
  
  public class MainActivity extends ActionBarActivity {
  
  private BluetoothAdapter btadapter;
  private Set&lt;BluetoothDevice&gt; pairedDevices;
  private SeekBar redcontrol = null;
  private SeekBar greencontrol = null;
  private SeekBar bluecontrol = null;
  private TextView redv;
  private TextView greenv;
  private TextView bluev;
  private ListView lv;
  public static String EXTRA_DEVICE_ADDRESS = "device_address";
  
  LinearLayout linearLayout;
  Button on, off, button;
  private static final UUID MY_UUID =
          UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  
          // Insert bluetooth devices MAC address
  private static String address = "00:14:02:13:08:21";
  private BluetoothChatService mChatService = null;
  
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_CANCELED) {
  
      finish();
          System.exit(10);
      }
  }
  
  
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
  
      setContentView(R.layout.activity_main);
      btadapter = (BluetoothAdapter.getDefaultAdapter());
  
  
  
      button = (Button)findViewById(R.id.button);
  
      lv = (ListView)findViewById(R.id.listView1);
  
      btadapter = BluetoothAdapter.getDefaultAdapter();
  
      button = (Button) findViewById(R.id.button);
      redv = (TextView) findViewById(R.id.redv);
      greenv = (TextView) findViewById(R.id.greenv);
      bluev = (TextView) findViewById(R.id.bluev);
      redcontrol = (SeekBar) findViewById(R.id.seekBar);
      greencontrol = (SeekBar) findViewById(R.id.seekBar2);
      bluecontrol = (SeekBar) findViewById(R.id.seekBar3);
      redcontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          int progressChanged = 0;
  
          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
              progressChanged = progress;
              redv.setText(String.valueOf(progress));
          }
  
          public void onStartTrackingTouch(SeekBar seekBar) {
              // TODO Auto-generated method stub
          }
  
          public void onStopTrackingTouch(SeekBar seekBar) {
              //Toast.makeText(MainActivity.this, "seek bar progress:" + progressChanged,
              // Toast.LENGTH_SHORT).show();
          }
      });
      greencontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          int progressChanged = 0;
  
          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
              progressChanged = progress;
              greenv.setText(String.valueOf(progress));
          }
  
          public void onStartTrackingTouch(SeekBar seekBar) {
              // TODO Auto-generated method stub
          }
  
          public void onStopTrackingTouch(SeekBar seekBar) {
             // Toast.makeText(MainActivity.this, "seek bar progress:" + progressChanged,
  
             //Toast.LENGTH_SHORT).show();
          }
      });
      bluecontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          int progressChanged = 0;
  
          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
              progressChanged = progress;
              bluev.setText(String.valueOf(progress));
          }
  
          public void onStartTrackingTouch(SeekBar seekBar) {
              // TODO Auto-generated method stub
          }
  
          public void onStopTrackingTouch(SeekBar seekBar) {
              //Toast.makeText(MainActivity.this, "seek bar progress:" + progressChanged,
                 //     Toast.LENGTH_SHORT).show();
          }
      });
  
      linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
      on = (Button) findViewById(R.id.on);
      off = (Button) findViewById(R.id.off);
  
      on.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              on.setBackgroundColor(Color.parseColor("#57D3E8"));
              off.setBackgroundColor(Color.parseColor("#EAEAEA"));
              if (!btadapter.isEnabled()) {
                  Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                  startActivityForResult(turnOn, 0);
                  Toast.makeText(getApplicationContext(), "Bluetooth Turning On"
                          , Toast.LENGTH_SHORT).show();
              } else {
                  Toast.makeText(getApplicationContext(), "Already On",
                          Toast.LENGTH_SHORT).show();
              }
          }
      });
  
  
      off.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              off.setBackgroundColor(Color.parseColor("#57D3E8"));
              on.setBackgroundColor(Color.parseColor("#EAEAEA"));
              if (btadapter.isEnabled()) {
                  btadapter.disable();
  
                  Toast.makeText(getApplicationContext(), "Bluetooth Off"
                          , Toast.LENGTH_SHORT).show();
              } else {
                  Toast.makeText(getApplicationContext(), "Already Off",
                          Toast.LENGTH_SHORT).show();
              }
          }
      });
  
  
  }
  public void list(View view){
      pairedDevices = btadapter.getBondedDevices();
  
      ArrayList list = new ArrayList();
      for(BluetoothDevice bt : pairedDevices)
          list.add(bt.getName()  +"\n" + bt.getAddress()  );
  
  
      Toast.makeText(getApplicationContext(),"Showing Paired Devices",
              Toast.LENGTH_SHORT).show();
      final ArrayAdapter adapter = new ArrayAdapter
              (this,android.R.layout.simple_list_item_1, list);
      lv.setAdapter(adapter);
  
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                                  long id) {
  
              String item = ((TextView) view).getText().toString();
  
              Toast.makeText(getBaseContext(), item,  Toast.LENGTH_SHORT).show();
  
  
  
              for (BluetoothDevice device : pairedDevices) {
  
                  if ("00:14:02:13:08:21".equals(device.getAddress())) {
                      Toast.makeText(getBaseContext(), "That's the right one ", Toast.LENGTH_SHORT).show();
                      break;
                  }
  
  
              }
  
              view.setEnabled(false);
  
          }
      });
  
  }
  
  
  @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
  
          return true;
      }
  
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="wrap_content"
  android:layout_height="460dp"
  android:layout_marginTop="0dp"
  android:id="@+id/id"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context="com.arduinolight.app.MainActivity"
  android:background="#9DC42C"
  &gt;
  
  
  
  
  &lt;ScrollView
      android:layout_width="fill_parent"
      android:layout_height="750dp"
      android:id="@+id/scrollView"
      android:layout_below="@+id/linearLayout"
      android:layout_marginTop="-30dp"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      &gt;
  
  
  
  &lt;RelativeLayout
  android:layout_width="310dp"
  android:layout_height="800dp"
  android:orientation="vertical"
  android:layout_marginTop="20dp"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"&gt;
  &lt;Button
  android:layout_width="200dp"
  android:layout_height="50dp"
  android:text="Paired Devices"
  android:onClick="list"
  android:layout_marginLeft="50dp"
  android:layout_marginTop="320dp"
  android:id="@+id/button" /&gt;
      &lt;SeekBar
          android:layout_width="170dp"
          android:layout_height="wrap_content"
          android:id="@+id/seekBar"
          android:indeterminate="false"
          android:max="255"
          android:progress="0"
          android:layout_above="@+id/seekBar2"
          android:layout_alignLeft="@+id/seekBar2"
          android:layout_alignStart="@+id/seekBar2"
          android:layout_marginBottom="26dp" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="RED Light"
          android:id="@+id/redlight"
          android:textColor="#59126F"
          android:textIsSelectable="false"
          android:textStyle="bold"
          android:textSize="16sp"
          android:typeface="monospace"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginTop="165dp" /&gt;
  
      &lt;SeekBar
          android:layout_width="170dp"
          android:layout_height="wrap_content"
          android:id="@+id/seekBar2"
          android:indeterminate="false"
          android:max="255"
          android:progress="0"
          android:layout_alignTop="@+id/greenlight"
          android:layout_toRightOf="@+id/bluetooth"
          android:layout_marginTop="-5dp" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="GREEN Light"
          android:id="@+id/greenlight"
          android:textColor="#59126F"
          android:textIsSelectable="false"
          android:textStyle="bold"
          android:textSize="16sp"
          android:typeface="monospace"
          android:layout_alignBottom="@+id/button"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginBottom="134dp" /&gt;
  
      &lt;SeekBar
          android:layout_width="170dp"
          android:layout_height="wrap_content"
          android:id="@+id/seekBar3"
          android:indeterminate="false"
          android:max="255"
          android:progress="0"
          android:layout_alignTop="@+id/bluelight"
          android:layout_toRightOf="@+id/greenlight"
          android:layout_marginTop="-5dp"
          android:layout_marginLeft="4dp" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="BLUE Light"
          android:id="@+id/bluelight"
          android:textColor="#59126F"
          android:textIsSelectable="false"
          android:textStyle="bold"
          android:textSize="16sp"
          android:typeface="monospace"
          android:layout_below="@+id/seekBar2"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginTop="23dp" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="Red :"
          android:id="@+id/red"
          android:singleLine="false"
          android:textColor="#F51E3E"
          android:layout_marginTop="28dp"
          android:layout_below="@+id/on"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="Green :"
          android:id="@+id/green"
          android:textColor="#59126F"
          android:layout_alignTop="@+id/red"
          android:layout_toRightOf="@+id/redlight" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="  0"
          android:id="@+id/redv"
          android:textColor="#F51E3E"
          android:layout_alignTop="@+id/red"
          android:layout_toRightOf="@+id/red"
          android:layout_marginLeft="2dp" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="  0"
          android:id="@+id/greenv"
          android:textColor="#04670F"
          android:layout_alignTop="@+id/green"
          android:layout_toRightOf="@+id/green" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="Blue :"
          android:id="@+id/blue"
          android:textColor="#292EFF"
          android:layout_alignTop="@+id/greenv"
          android:layout_marginLeft="205dp" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text="  0"
          android:id="@+id/bluev"
          android:textColor="#1850C6"
          android:layout_alignTop="@+id/blue"
          android:layout_marginLeft="250dp"
          /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:text="Bluetooth :"
          android:textColor="#59126F"
          android:textSize="24sp"
          android:id="@+id/bluetooth"
          android:layout_alignParentTop="false"
          android:layout_alignStart="@+id/red"
          android:layout_marginTop="40dp" /&gt;
  
      &lt;Button
          style="?android:attr/buttonStyleSmall"
          android:layout_width="70sp"
          android:layout_height="40sp"
          android:text="ON"
          android:textSize="20sp"
          android:id="@+id/on"
          android:longClickable="false"
          android:textColor="#000000"
          android:layout_alignTop="@+id/bluetooth"
          android:layout_toRightOf="@+id/bluetooth"
          android:layout_marginLeft="7dp"
          android:layout_marginTop="-7dp"
          android:background="#EAEAEA" /&gt;
  
      &lt;Button
          style="?android:attr/buttonStyleSmall"
          android:layout_width="70dp"
          android:layout_height="40sp"
          android:text="OFF"
          android:textSize="20sp"
          android:id="@+id/off"
          android:clickable="true"
          android:textColor="#000"
          android:layout_alignTop="@+id/on"
          android:layout_alignLeft="@+id/blue"
          android:layout_alignStart="@+id/blue"
          android:layout_marginLeft="-5dp"
          android:layout_marginTop="0dp"
          android:background="#EAEAEA" /&gt;
      &lt;ListView
          android:id="@+id/listView1"
          android:layout_width="wrap_content"
          android:layout_height="150dp"
          android:cacheColorHint="#00000000"
          android:visibility="visible"
          android:layout_below="@+id/button"
          android:layout_marginTop="30dp"&gt;
  
      &lt;/ListView&gt;
  &lt;/RelativeLayout&gt;
  &lt;/ScrollView&gt;
   &lt;/RelativeLayout&gt; 
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.arduinolightning.app"
   &gt;
  &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" /&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH"/&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="Arduino Lighting"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name="com.arduinolightning.app.MainActivity"
          android:label="Arduino Lighting" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>