简体   繁体   中英

Android bluetooth connect two or more devices and send data

Hi I need to connect 3 different devices (Clients) to a fourth device (Server). Here is my server code:

public class MainServerActivity extends Activity {

    ArrayAdapter<String> listAdapter;
    ListView listView;
    TextView textViewTextoRecibido;
    BluetoothAdapter btADapter;
    Set<BluetoothDevice> devicesArray;
    ArrayList<String> pairedDevices;
    ArrayList<BluetoothDevice> devices;
    IntentFilter filter;
    BroadcastReceiver receiver;
    public AcceptThread acceptThread;
    protected static final int SUCCESS_CONNECT = 0;
    protected static final int MESSAGE_READ = 1;
    public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    public static final String NAME = "Tablet_Madre";
    //public static final UUID MY_UUID = UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
    ConnectedThread connectedThread;
    Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case SUCCESS_CONNECT:

                connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
                Toast.makeText(getApplicationContext(), "Dispositivo conectado!", Toast.LENGTH_LONG).show();
                //String s = "successfully connected";
                //connectedThread.write(s.getBytes());
                break;

            case MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                String string = new String(readBuf);
                textViewTextoRecibido.setText(string);
                Toast.makeText(getApplicationContext(), "Llego el mensaje!!!!", Toast.LENGTH_LONG).show();
                break;

            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_server);
        init();

        if (btADapter == null) {
            Toast.makeText(getApplicationContext(), "No bluetooth detectado", Toast.LENGTH_LONG).show();
            finish();
        } else {
            if (!btADapter.isEnabled()) {
                turnOnBT();
            }

            //getPairedDevices();
            //startDiscovery();
            aceptarConexiones();

        }
    }

    private void aceptarConexiones() {
        try {
             acceptThread = new AcceptThread();
            //acceptThread.run();

        } catch (Exception e) {
            System.out.print(e);
        }

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        try {
            acceptThread.run();
            //start();
        } catch (Exception e) {
            System.out.print(e);
        }
    }


    private void startDiscovery() {
        // TODO Auto-generated method stub
        btADapter.cancelDiscovery();
        btADapter.startDiscovery();
    }

    private void turnOnBT() {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, 1);
    }


    /*Inicializa todos los componentes*/
    private void init() {
        listView = (ListView) findViewById(R.id.listView);
        //listView.setOnItemClickListener(this);
        textViewTextoRecibido = (TextView) findViewById(R.id.textViewTextoRecibido);
        listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
        listView.setAdapter(listAdapter);
        btADapter = BluetoothAdapter.getDefaultAdapter();
        pairedDevices = new ArrayList<String>();
        filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        devices = new ArrayList<BluetoothDevice>();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Bluetooth debe estar encendido", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    public void enviarMensaje(View view){
        String s = "Campeon del Siglo";
        connectedThread.write(s.getBytes());
    }

    private class AcceptThread extends Thread{
        public final BluetoothServerSocket mmServerSocket;
        private int cantClientes;

        public AcceptThread() {
            BluetoothServerSocket tmp = null;

            // Create a new listening server socket
            try {
                tmp = btADapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
            } catch (IOException e) {
                //Log.e(TAG, "listen() failed", e);
            }
            mmServerSocket = tmp;
        }

        public void run(){
            BluetoothSocket socket = null;

            while(true){
                try{
                    socket = mmServerSocket.accept();
                    //No se llama al socket.connect() porque esto ya los conecta.
                } catch (IOException e){
                    break;
                }
                if(socket != null){
                    cantClientes = cantClientes + 1;
                }

                mHandler.obtainMessage(SUCCESS_CONNECT, socket).sendToTarget();

                if (socket != null){

                    //manageConnectedSocket(socket);
                    try {
                        mmServerSocket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
            }

        }

        public void cancel(){
            try{
                mmServerSocket.close();
            } catch(IOException e) {}
        }

    }

    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity
                    mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
                } catch (IOException e) {
                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) { }
        }

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }

}

And this is my client code:

public class MainClientActivity extends ActionBarActivity implements OnItemClickListener {


    ArrayAdapter<String> listAdapter;
    ListView listView;
    TextView textViewTextoRecibido;
    BluetoothAdapter btADapter;
    Set<BluetoothDevice> devicesArray;
    ArrayList<String> pairedDevices;
    ArrayList<BluetoothDevice> devices;
    IntentFilter filter;
    BroadcastReceiver receiver;
    protected static final int SUCCESS_CONNECT = 0;
    protected static final int MESSAGE_READ = 1;
    public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    public static final String NAME = "Tablet_Madre";
    //public static final UUID MY_UUID = UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
    ConnectedThread connectedThread;
    Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case SUCCESS_CONNECT:

                connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
                Toast.makeText(getApplicationContext(), "Dispositivo conectado!", Toast.LENGTH_LONG).show();
                //String s = "successfully connected";
                //connectedThread.write(s.getBytes());
                break;

            case MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                String string = new String(readBuf);
                textViewTextoRecibido.setText(string);
                Toast.makeText(getApplicationContext(), "Llego el mensaje!!!!", Toast.LENGTH_LONG).show();
                break;

            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_client);
        init();

        if (btADapter == null) {
            Toast.makeText(getApplicationContext(), "No bluetooth detectado", Toast.LENGTH_LONG).show();
            finish();
        } else {
            if (!btADapter.isEnabled()) {
                turnOnBT();
            }

            getPairedDevices();
            startDiscovery();
        }
    }

    /*Inicializa todos los componentes*/
    private void init() {
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        textViewTextoRecibido = (TextView) findViewById(R.id.textViewTextoRecibido);
        listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
        listView.setAdapter(listAdapter);
        btADapter = BluetoothAdapter.getDefaultAdapter();
        pairedDevices = new ArrayList<String>();
        filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        devices = new ArrayList<BluetoothDevice>();
        receiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    boolean yaEstaListado = false;
                    //Si encuentro uno, me fijo is ya esta en la lista de paireds
                    for (int j = 0; j < pairedDevices.size(); j++) {
                        if (device.getName().equals(pairedDevices.get(j))) {
                            yaEstaListado = true;
                            break;
                        }
                    }

                    if(!yaEstaListado){
                        listAdapter.add(device.getName() + "\n" + device.getAddress());
                        devices.add(device);
                    }

                } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {

                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

                } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    if (btADapter.getState() == btADapter.STATE_OFF) {
                        turnOnBT();
                    }
                }
            }
        };

        registerReceiver(receiver, filter);
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        registerReceiver(receiver, filter);
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(receiver, filter);
        filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(receiver, filter);
    }

    private void startDiscovery() {

        btADapter.cancelDiscovery();
        btADapter.startDiscovery();
    }

    public void buscarDispositivo(View view){
        startDiscovery();
    }

    private void turnOnBT() {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, 1);
    }

    private void getPairedDevices() {
        devicesArray = btADapter.getBondedDevices();
        if (devicesArray.size() > 0) {
            for (BluetoothDevice device : devicesArray) {
                pairedDevices.add(device.getName());
                listAdapter.add(device.getName() + " (Paired) " + "\n" + device.getAddress());
                devices.add(device);
            }
        }

    }


    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Bluetooth debe estar encendido", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        try {
            if(btADapter.isDiscovering()){
                btADapter.cancelDiscovery();
            }
                BluetoothDevice selectedDevice = devices.get(position);
                ConnectThread connect = new ConnectThread(selectedDevice);
                connect.start();

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void enviarMensaje(View view){
        String s = "Carbonero querido";
        connectedThread.write(s.getBytes());
    }

    private class ConnectThread extends Thread {

        private BluetoothSocket mmSocket = null;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            // BluetoothSocket tmp = null;
            mmDevice = device;

            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {
                // MY_UUID is the app's UUID string, also used by the server code
                mmSocket = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
                //Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
                //mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
            } catch (IOException e) { }
        }

        public void run() {
            // Cancel discovery because it will slow down the connection
            btADapter.cancelDiscovery();

            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                Thread.sleep(2000);
                mmSocket.connect();
                mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
            } catch (IOException connectException) {
                System.out.println(connectException);
                // Unable to connect; close the socket and get out
                try {
                    mmSocket.close();
                } catch (IOException closeException) {
                }
                return;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }  catch (Exception e) {
                System.out.println(e);
            }
        }

        /** Will cancel an in-progress connection, and close the socket */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
            }
        }
    }

    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity
                    mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
                } catch (IOException e) {
                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) { }
        }

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }



}

I saw some answers and i think i was not clear. Now with this code i cant connect two devices and send data from client to server and backwards. The second part of the problem is that i dont have any idea how can i manage 3 connections from server side. Give me some ideas, examples...

I have 4 questions:

1) I need to create a thread so I can accept more than one device because now is accepting one and the main thread is freeze when I do socket.accept();

2) Sometimes de connections works fine but sometimes no. If you see the code, I show a message in both devices when connect, sometimes appears in one and sometimes appears in the other.

3) When I have a connection, when I try to send a message, this is not working. I don't know if I have to create a ConnectionThread in the client and in the server, I create in both but when im going to write in client side, connectionThread is null.

4) How can I manage 3 threads from server, one for each client?

I hope I was clear… Some names are in Spanish.

Greets.

You can only communicate between two devices at one time.

If you try to communicate with a third device with this code, it will not work.

To communicate between more than two devices, you would need to end the communication with client 1 before commencing communication with client 2. This would require more code to manage your connections in whatever way you wish, whether you wanted to put them in some sort of queue.

It depends on if you want to automate your communication, or manually start and stop the communication between devices as to how you would manage this.

You could loop through your discoverable devices and start and stop connections. This would involve calling and managing your threads for your bluetooth server, so that the sockets were closed and a new inialisation of waiting for a new connection occurs for each new connection.

It would mean that you would need to keep track of which device has been connected and which would be next in the queue, with error handling to check that the device is available when the server is ready to accept connection from this device. It is reasonably complicate and prone to error, due to ensuring the availability of devices and you would need to carefully step through your code and ensure you make provisions for devices not being available when the server is ready, so allowing the server to then loop to the next available device.

So in the case of the server code, the basic connection and thread management is the same as for connection to one client, but this is called and managed by your code to loop through client devices, or to manually stop communicating with one device and attempt connection with another device.

The client code would not not have to change whether you are attempting to loop through more than one client device or just deal with one client. Handling this type of code would need to be managed within your server code.

Firstly you need to understand clearly how to just communicate between two devices and I am unclear if you know how to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM