我是新的在这个页面,我需要帮助我的应用程序,我想通过蓝牙发送数据到ESP32 wifi /蓝牙模块,但当我进入此活动(Habitacion 1)我的应用程序然后尝试启动连接和崩溃,我不知道为什么到底:ConnectBT()问题。如果你能帮助我,我将不胜感激,谢谢你。
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<Void, Void, Void> {
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");
}
}
}
}