两款Android设备之间的蓝牙数据传输 [英] Bluetooth data transfer between two Android devices

查看:141
本文介绍了两款Android设备之间的蓝牙数据传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注这款Android指南蓝牙通讯

要正是我想做的事情解释一下,当两个设备配对,两个不同的活动开各个设备(服务器和客户端),其中对服务器的活动我有不同的按钮,并在客户的活动也只是一个TextView。 我希望能够以preSS的所述服务器装置上的按钮,并在客户端上显示它。

To explain exactly what I want to do, when the two devices are paired, two different activities open up on each device (server and client) where on the server activity I have different buttons, and on the client activity there is just a textview. I want to be able to press a button on the server device and display it on the client.

我已成功地建立在两个设备之间的连接,但现在我要发送数据,我一直没能做到。

I have managed to establish a connection between the two devices, but now I want to send data which I have not been able to do.

他们发现,这code数据传输:

They give this code for data transfer:

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) { }
}

}

不过,此行产生一个错误

But this line generates an error

// Send the obtained bytes to the UI activity
            mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();

和在引导没有解释。我不知道mHandler是或做什么。

And is not explained in the guide. I don't know what the mHandler is or does.

除了误差,我甚至不真正明白的地方把这个code。它应该是在第二活动(服务器和客户端),该予打开或在主?如果服务器的活动,它应该是在onclick方法具有不同字节code发送的每个按钮所有的按钮?而在这code,我们怎么区分是谁发送和谁接收?

Apart from the error, I don't even really understand where to put this code. Should it be in the second activities (server and client) that I open or in the main? If in the Server activity, should it be in the onClick method for all the buttons with a different byte code to send for each button ? And in this code, how do we distinguish who is sending and who is receiving ?

任何帮助和建议将大大AP preciated。

Any help and advice would be greatly appreciated.

推荐答案

退房的<一个href="https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat"相对=nofollow> BluetoothChat 例如,谷歌提供了SDK。它会告诉你如何实现基本的发送文本的通过蓝牙。

Check out the BluetoothChat example that Google provides in the SDK. It'll show you how to implement basic sending of text over bluetooth.

这篇关于两款Android设备之间的蓝牙数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆