简体   繁体   中英

Send data between 2 devices using Bluetooth in Android

I'm developing an app where you can send an arrayList of ojects with the bluetooth to another device. As I'm new to this, I've been able to pair the 2 devices but I'm having trouble to have the content sent. I followed google guide but I'm having hard time to understand it. here is the code:

public class ServerThread implements Runnable {

    private final BluetoothServerSocket serverSocket;
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    private final String APPNAME = "Quick";
    private final java.util.UUID UUID = java.util.UUID.fromString("aeeb5480-1c74-45e2-bfd0-f592958cba2a");
    private Handler handler;

    public ServerThread(Handler handler) {

        this.handler = handler;

        BluetoothServerSocket tmp = null;
        try {
            tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(APPNAME, UUID);
        } catch (IOException e) {
        }
        serverSocket = tmp;
    }

    @Override
    public void run() {
        BluetoothSocket socket;
        while (true) {
            try {
                BluetoothSocket tmp;
                tmp = serverSocket.accept();
                socket = tmp;
            } catch (IOException e) {
                break;
            }
            if (socket != null) {

            }
        }

    }
}

.

public class ClientThread implements Runnable {

    private static final UUID UUID = java.util.UUID.fromString("aeeb5480-1c74-45e2-bfd0-f592958cba2a");
    private static BluetoothSocket socket;
    BluetoothDevice device;
    private Handler handler;

    public ClientThread(BluetoothDevice device, Handler handler) {

        this.handler = handler;

        BluetoothSocket tmp = null;
        this.device = device;
        try {
            tmp = device.createRfcommSocketToServiceRecord(UUID);
        } catch (IOException e) {
            e.printStackTrace();
        }
        socket = tmp;
    }


    @Override
    public void run() {
        if (BluetoothAdapter.getDefaultAdapter().isDiscovering()) {
            BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
        }
        try {
            socket.connect();
        } catch (IOException e) {
            try {
                socket.close();
            } catch (IOException e1) {
            }
            return;
        }

    }
    public static BluetoothSocket getSocket() {
        return socket;
    }
}

The problem is I don't know how to transfer data between the devices and where I need to call the methods allowing me to do that. If someone could help me would be great. thanks

Once you get your server side BluetoothSocket, after the accept() routine, and your client side one via connect() method, you can begin communication between devices. In order to do that, you will need an InputStream and an Outputstream objects linked to sockets in both client and server side: you can obtain them by simply use BluetoothSocket methods getInputStream() and getOutputStream. Use the OutputStream to write data to the other side of the communication (via write(byte[]) method) and the InputStream read() methods (there are 3 kinds, if I remember correctly) to collect received data.

As you may have already imagined, Bluetooth communication consists of byte flow; you cannot send complex objects directly, but you have to decompose them before sending and re-compose them after receiving, so it's up to you to realize a parse routine for your arraylist objects.

[ EDIT : with java.io you CAN actually send complex "objects"; Readers and Writers are used to send/receive Strings, while FileInputStream and FileOutputStream are used to exchange files. You can even serialize Objects and send them through a Stream; my answer lacked experience, so be aware that solutions exists for you to simplify data exchange routines; just take a look at java.io library.]

I also suggest you to see all the documentation you can find both on Android Bluetooth APIs and Bluetooth architecture in general, in order to develop a strong application! Bluetooth can be tricky.

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