Android Open Source - BluetoothSPP Connect Activity

From Project

Back to project page BluetoothSPP.


The source code is released under:

MIT License

If you think the Android project BluetoothSPP listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*w  w  w .ja  v a 2s .com*/
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Set;

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;

public class ConnectActivity extends Activity {

  private ListView deviceListView;
  private BluetoothAdapter bluetoothAdapter;

  protected void onCreate(Bundle savedInstanceState) {

    deviceListView = (ListView) findViewById(;
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (bluetoothAdapter == null) {
      makeText(getApplicationContext(), "Bluetooth Not Found", LENGTH_SHORT).show();
    } else if (!bluetoothAdapter.isEnabled()) {
      Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBluetooth, "BT_ENABLE_REQ");
    } else {
      new DeviceSearch().execute();

    deviceListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
      public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

        BluetoothDevice device = (BluetoothDevice) adapterView.getItemAtPosition(i);

        Intent intent = new Intent(getApplicationContext(), ConsoleActivity.class);
        intent.putExtra("DEVICE", device);

        return true;

  private void startActivityForResult(Intent enableBluetooth, String bt_enable_req) {

  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(, menu);
    return true;

  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == {
      return true;
    return super.onOptionsItemSelected(item);

  private class DeviceSearch extends AsyncTask<Void, Void, ArrayList<BluetoothDevice>> {

    protected ArrayList<BluetoothDevice> doInBackground(Void... params) {
      Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
      ArrayList<BluetoothDevice> listDevices = new ArrayList<BluetoothDevice>();
      for (BluetoothDevice device : pairedDevices) {
        Log.d("DEBUG", device.getName());
      return listDevices;

    protected void onPostExecute(ArrayList<BluetoothDevice> listDevices) {
      if (listDevices.size() > 0) {
        DeviceAdapter adapter = new DeviceAdapter(getBaseContext(), listDevices);
      } else {
        makeText(getApplicationContext(), "Device Not Found", LENGTH_SHORT).show();

Java Source Code List