title: bluetooth app instant crashes in activity
body: <p>im new on this page and i need help with my app, i want to send data via bluetooth to an ESP32 wifi/bluetooth module, but when i enter on this activity (Habitacion 1) my app then tries to start a connection and crashes and i dont know why exactly: the problem goes on ConnectBT(). If you can help me, I would be grateful, thank you.</p>
  
  <pre><code>import android.app.ProgressDialog;
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.bluetooth.BluetoothSocket;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.CompoundButton;
  import android.widget.Toast;
  import android.widget.ToggleButton;
  import java.io.IOException;
  import java.util.UUID;
  
  public class Habitacion1 extends AppCompatActivity {
      ToggleButton luzh2, puertah1, persianah1, tomabuttonh1;
      String address = null;
      private ProgressDialog progress;
      BluetoothAdapter mBluetoothAdapter = null;
      BluetoothSocket btSocket = null;
      private boolean isBtConnected = false;
      static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_habitacion1);
          Intent newint = getIntent();
          address = newint.getStringExtra(bluetoothList.EXTRA_ADDRESS);//Recibo las direcciones MAC de la otra activity
          final ToggleButton luzh1 = (ToggleButton) findViewById(R.id.luzh1);
          luzh2 = (ToggleButton) findViewById(R.id.luzh2);
          puertah1 = (ToggleButton) findViewById(R.id.puertah1);
          persianah1 = (ToggleButton) findViewById(R.id.persianah1);
          tomabuttonh1 = (ToggleButton) findViewById(R.id.tomabuttonh1);
          new ConnectedBT().execute();
          luzh1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if (isChecked) {
                      luz1OnH1();
                  } else {
                      if (btSocket != null) {
                          try {
                              btSocket.getOutputStream().write("Offl1H1".toString().getBytes());
                          } catch (IOException e) {
                              msg("Error");
                          }
                      }
                  }
              }
          });
          luzh2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if (isChecked) {
                      luz2OnH1();
                  } else {
                      if (btSocket != null) {
                          try {
                              btSocket.getOutputStream().write("Offl2H1".toString().getBytes());
                          } catch (IOException e) {
                              msg("Error");
                          }
                      }
                  }
              }
          });
          puertah1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if(isChecked){
                      puerta1OnH1();
                  }else{
                      if(btSocket!=null){
                          try{
                              btSocket.getOutputStream().write("Offp1H2".toString().getBytes());
                          }catch (IOException e){
                              msg("Error");
                          }
                      }
                  }
              }
          });
          persianah1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if(isChecked){
                      persiana1OnH1();
                  }else{
                      if(btSocket!=null){
                          try{
                              btSocket.getOutputStream().write("Offpa1H1".toString().getBytes());
                          }catch (IOException e){
                              msg("Error");
                          }
                      }
                  }
              }
          });
      }
      private class ConnectedBT extends AsyncTask&lt;Void, Void, Void&gt; {
          private boolean ConnectSuccess = true;//si llego aca esta casi listo
          @Override
          protected void onPreExecute() {
              progress = ProgressDialog.show(Habitacion1.this, "conectando...", "Por favor, espere");//muestra el dialogo del progreso
          }
  
          @Override
          protected Void doInBackground(Void... params)//mientras se muestra el dialogo de conexion, esta se hace en segundo plano
          {
              try {
               //  if (btSocket == null || !isBtConnected) {
                      mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();                 //obtiene el dispositivo bluetooth
                      BluetoothDevice dispositivo = mBluetoothAdapter.getRemoteDevice(address);//Conecta con la direccion del dispositivo y comprueba si esta disponible
                      btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//Crea la conexion rfcomm (spp)
                      BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                      btSocket.connect();//comienza la conexion
                 // }
              } catch (IOException e) {
                  ConnectSuccess = false; //si la conexion falla el valor de la variable cambia a false
                  msg("error");
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result)//despues de la conexion a segundo plano checkea si fue bien
          {
              super.onPostExecute(result);
              if (!ConnectSuccess) {
                  msg("Conexion Fallida, si es un dispositivo SSP intentelo de nuevo");
                  finish();
              } else {
                  msg("Conexion exitosa");
                  isBtConnected = true;
              }
              progress.dismiss();
          }
      }private void msg(String s) {
          Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
      }
      private void luz1OnH1() {
          if (btSocket != null) {
              try {
                  btSocket.getOutputStream().write("Ol1H1".toString().getBytes());
                  Toast.makeText(getApplicationContext(),"AASSAS",Toast.LENGTH_LONG).show();
              } catch (IOException e) {
                  msg("Error");
              }
          }
      }
      private void luz2OnH1() {
          if (btSocket != null) {
              try {
                  btSocket.getOutputStream().write("Ol2H1".toString().getBytes());
              } catch (IOException e) {
                  msg("Error");
              }
          }
      }
      private void puerta1OnH1(){
          if(btSocket!=null){
              try{
                  btSocket.getOutputStream().write("Offp1H1".toString().getBytes());
              }catch(IOException e){
                  msg("Error");
              }
          }
      }
      private void persiana1OnH1(){
          if(btSocket!=null){
              try{
                  btSocket.getOutputStream().write("Offpa1H1".toString().getBytes());
              }catch(IOException e){
                  msg("Error");
              }
          }
      }
  }
  </code></pre>