我需要我的Android应用程序连接多个设备并与他们交换信息。
为了实现这一点,我试图创建一个线程,从设备接收连接,保存其MAC号,断开连接并打开连接以接收新的dispositive,但在我的所有尝试中,我无法恢复生成失败的连接,因为线路管理给出了错误。
此外,当蓝牙生成一些错误我无法创建检索连接或启动新连接的方法时,我必须手动重启蓝牙以重新建立连接。对于我的Android应用程序,使用手动方法连接并不好。
有人可以帮我展示一种管理线程的方法,以便我可以连接和断开多个设备吗?
下面是线程的代码
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 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;
}
});
线程发送/接收
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();
}
}
}
线程客户端
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();
}
}
让我问一个问题,你有蓝牙经典或蓝牙低能耗?如果是蓝牙经典那么由于BL-Classic的协议是配对连接,所以不可能同时连接多个设备。如果使用蓝牙低功耗,那么它是可行的,我已经为单个设备制作了代码,但是您可以通过使用不同的UUID执行不同的服务来操作代码,它应该可以工作。这是链接:https://github.com/Waged/BLEArduino