如何使Android App与多个蓝牙设备连接?

时间:2018-08-27 00:24:28

标签: android bluetooth android-bluetooth

我需要我的Android应用程序才能与多个设备连接并与它们交换信息。

要实现此目的,我试图创建一个从设备接收连接的线程,保存其MAC编号,断开连接并打开连接以接收新的存储库,但是在所有尝试中,我都无法恢复生成失败,因为线路管理出现错误。

此外,当蓝牙产生一些错误时,我无法创建一种方法来检索连接或启动新连接,我必须手动重启蓝牙才能重新建立连接。对于我的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();
        }

    }

1 个答案:

答案 0 :(得分:0)

让我问一个问题,您是否具有蓝牙经典功能或蓝牙低功耗功能? 如果是蓝牙经典,那么BL-Classic的协议是成对连接,则不可能同时连接多个设备。 如果使用低功耗蓝牙,那是可行的,我为单个设备编写了代码,但是您可以通过使用不同的UUID进行不同的服务来操纵代码,并且该代码应该可以工作。 这是链接:https://github.com/Waged/BLEArduino