I need my Android application to connect to multiple devices and exchange information with them.
To accomplish this I am trying to create a Thread to which it receives the connection of a device, save its mac number, disconnect and open the connection to receive a new device, but in all my attempts I can not establish the connection again generating failure .
Below is the bluetooth communication treads.
Thread from the server where you receive the connection
private class ServeClass extends Thread {
private BluetoothServerSocket serverSocket;
public ServeClass() {
try {
//Servidor que recebe um Socket
Log.e("BluetoothServerSocket","ServerSocket conectado");
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
while (socket == null) {
try {
Message message = Message.obtain();
message.what = STATE_CONNECTING;
// recebe um BluetoothSocket para gerenciar a mensagem
socket = serverSocket.accept();
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
Message message = Message.obtain();
message.what = STATE_CONNECTION_FAILED;
handler.sendMessage(message);
}
if (socket != null) {
Message message = Message.obtain();
message.what = STATE_CONNECTED;
handler.sendMessage(message);
sendRecive=new SendRecive(socket);
sendRecive.start();
break;
}
}
}
}
Handler to manage connection state
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case STATE_LISTENING:
content.setText("Escutando");
break;
case STATE_CONNECTING:
content.setText("Conectando");
break;
case STATE_CONNECTED:
content.setText("Conectado");
//botao.setVisibility(View.VISIBLE);
break;
case STATE_CONNECTION_FAILED:
content.setText("Conexão Falha");
break;
case STATE_MESSAGE_RECEIVED:
byte[] readBuffer= (byte[]) msg.obj;
String tempMsg= new String(readBuffer,0,msg.arg1);
botao.setText(tempMsg);
break;
}
return true;
}
});
Thread that receives the message sent via bluetooth
private class SendRecive extends Thread {
private final BluetoothSocket bluetoothSocket;
private final InputStream inputStream;
private final OutputStream outputStream;
public SendRecive(BluetoothSocket socket) {
bluetoothSocket = socket;
InputStream tempIn = null;
OutputStream tempOut = null;
try {
tempIn = bluetoothSocket.getInputStream();
tempOut = bluetoothSocket.getOutputStream();
Log.e("TESTE_CONTRUTOR", "TESTE_CONTRUTOR");
} catch (IOException e) {
e.printStackTrace();
}
inputStream = tempIn;
outputStream = tempOut;
}
@Override
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = inputStream.read(buffer);
handler.obtainMessage(STATE_MESSAGE_RECEIVED, bytes, -1, buffer).sendToTarget();
socket = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void write(byte[] bytes) {
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thread Client looking for server to connect
private class ClientClass extends Thread {
private BluetoothSocket socket;
private BluetoothDevice device;
public ClientClass(BluetoothDevice device) {
this.device = device;
try {
socket = this.device.createRfcommSocketToServiceRecord(MYUUID);
} catch (Exception e) {
Log.e("Erro", String.valueOf(e));
e.printStackTrace();
}
}
@Override
public void run() {
try {
socket.connect();
Message message = Message.obtain();
message.what = STATE_CONNECTED;
handler.sendMessage(message);
sendRecive = new SendRecive(socket);
sendRecive.start();
} catch (IOException e) {
e.printStackTrace();
Message message = Message.obtain();
message.what = STATE_CONNECTION_FAILED;
handler.sendMessage(message);
}
}
public void cancel() {
try {
socket.close();
Message message = Message.obtain();
message.what = STATE_LISTENING;
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}