title: Android Bluetooth Discover and Show Devices
body: <p>I want to create simple application in which i want to show bluetooth devices..but when i press the button, Application crashes..
  in ActivityMain.java  this method will go to next activity (Activity.java)..in which i am getting problem..Rest of buttons are working</p>
  
  <p>ActivityMain.java</p>
  
  <pre><code>package com.example.bluetoothcheck5;
  
  
  import java.util.Set;
  
  import com.example.bluetoothcheck5.Activity2;
  import android.os.Bundle;
  import android.app.Activity;
  
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.content.Context;
  import android.content.Intent;
  
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends Activity {
    private static final int REQUEST_ENABLE_BT = 0;
      private static final int REQUEST_DISCOVERABLE_BT = 0;
    @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
     final TextView out=(TextView)findViewById(R.id.out);
     final Button button = (Button) findViewById(R.id.button1);
    final Button button1 = (Button) findViewById(R.id.button2);
    final Button button2 = (Button) findViewById(R.id.button3);
    final Button buttonSearch = (Button) findViewById(R.id.buttonSearch);
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
     if (mBluetoothAdapter == null) {
      out.append("device not supported");
    }
    button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          if (!mBluetoothAdapter.isEnabled()) {
              Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
              startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
          }
      }
    });
    button1.setOnClickListener(new View.OnClickListener() {
   @Override
      public void onClick(View arg0) {
          if (!mBluetoothAdapter.isDiscovering()) {               
                 Context context = getApplicationContext();
                 CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";
                 int duration = Toast.LENGTH_SHORT;
  
                 Toast toast = Toast.makeText(context, text, duration);
                 toast.show();
              Intent enableBtIntent = new         Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
              startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);
  
          }
      }
  });
  button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {   
          mBluetoothAdapter.disable();
  
          Context context = getApplicationContext();
             CharSequence text = "TURNING_OFF BLUETOOTH";
  
             Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
             toast.show();
  
  
             Set&lt;BluetoothDevice&gt; mPairedDevices = mBluetoothAdapter.getBondedDevices();
             if (mPairedDevices.size() &gt; 0) 
             {
                 for (BluetoothDevice mDevice : mPairedDevices) 
                 {
                     Log.v("Title", "PairedDevices: " + mDevice.getName() + " " +         mDevice.getAddress());
                 }
             }
          }
  });
  buttonSearch.setOnClickListener(new View.OnClickListener() {
  
      @Override
      public void onClick(View arg0) {
  
  
          // TODO Auto-generated method stub
          Intent myIntent = new Intent(arg0.getContext(), Activity2.class);
      //   myIntent.setClass(MainActivity.this, Activity2.class);
            // startActivityForResult(myIntent, REQUEST_PAIRED_DEVICE); 
  
          startActivity(myIntent);
  
  
  
      }
  });
  
  
  }}
  </code></pre>
  
  <p>Activity2.java</p>
  
  <pre><code>package com.example.bluetoothcheck5;
  
  
  import java.util.Set;
  
  import android.app.Activity;
  import android.bluetooth.BluetoothAdapter;
  
  import android.bluetooth.BluetoothDevice;
  
  import android.content.Intent;
  
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.AdapterView.OnItemClickListener;
  
  
  
  import com.example.bluetoothcheck5.R;
  import com.example.bluetoothcheck5.MainActivity;
  
  
  public class Activity2 extends MainActivity {
     protected static final String TAG = "TAG";
     private BluetoothAdapter mBluetoothAdapter;
     private ArrayAdapter&lt;String&gt; mPairedDevicesArrayAdapter;
  
   protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_2);
          mPairedDevicesArrayAdapter = new ArrayAdapter&lt;String&gt;(this,   R.layout.activity_2);
  
          ListView mPairedListView = (ListView) findViewById(R.id.listView1);
          mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
          mPairedListView.setOnItemClickListener(mDeviceClickListener);
  
          mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          Set&lt;BluetoothDevice&gt; mPairedDevices = mBluetoothAdapter.getBondedDevices();
  
          if (mPairedDevices.size() &gt; 0) 
          {
              findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
              for (BluetoothDevice mDevice : mPairedDevices) 
              {
                  mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" +   mDevice.getAddress());
              }
          } 
          else 
          {
              String mNoDevices =    getResources().getText(R.string.none_paired).toString();
              mPairedDevicesArrayAdapter.add(mNoDevices);
          }
      }
  
   private OnItemClickListener mDeviceClickListener = new OnItemClickListener() 
      {
          public void onItemClick(AdapterView&lt;?&gt; mAdapterView, View mView, int   mPosition, long mLong) 
          {
              mBluetoothAdapter.cancelDiscovery();
              String mDeviceInfo = ((TextView) mView).getText().toString();
              String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() -    17);
              Log.v(TAG, "Device_Address " + mDeviceAddress);
  
              Bundle mBundle = new Bundle();
              mBundle.putString("DeviceAddress", mDeviceAddress);
              Intent mBackIntent = new Intent();
              mBackIntent.putExtras(mBundle);
              setResult(Activity.RESULT_OK, mBackIntent);
              finish();
          }
      };
  
  
   }
  </code></pre>