简体   繁体   English

在Android中使用蓝牙在2个设备之间发送数据

[英]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. 我正在开发一个应用程序,您可以在其中将带有蓝牙的对象的arrayList发送到另一台设备。 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. 一旦获得了服务器端BluetoothSocket,在accept()例程和客户端的connect()方法之后,就可以开始设备之间的通信了。 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. 为此,您需要在客户端和服务器端都链接到套接字的InputStream和Outputstream对象:只需使用BluetoothSocket方法getInputStream()和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. 使用OutputStream将数据写入通信的另一端(通过write(byte [])方法)和InputStream read()方法(如果我没记错的话,有3种)来收集接收到的数据。

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. 您不能直接发送复杂的对象,但是必须在发送之前分解它们,并在接收之后重新组合它们,因此由您来实现对arraylist对象的解析例程由您决定。

[ EDIT : with java.io you CAN actually send complex "objects"; [ 编辑 :使用java.io,您实际上可以发送复杂的“对象”; Readers and Writers are used to send/receive Strings, while FileInputStream and FileOutputStream are used to exchange files. Reader和Writer用于发送/接收字符串,而FileInputStream和FileOutputStream用于交换文件。 You can even serialize Objects and send them through a Stream; 您甚至可以序列化对象并通过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.] 只需看看java.io库即可。]

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! 我还建议您查看所有可以同时在Android Bluetooth API和Bluetooth体系结构上找到的所有文档,以开发强大的应用程序! Bluetooth can be tricky. 蓝牙可能很棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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