简体   繁体   English

两个Android设备之间的蓝牙数据传输

[英]Bluetooth data transfer between two Android devices

I have been following this Android guide for Bluetooth communication 我一直在关注此Android指南进行蓝牙通信

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. 为了准确地解释我想要做什么,当两个设备配对时,在每个设备(服务器和客户端)上打开两个不同的活动,在服务器活动上我有不同的按钮,在客户端活动上只有一个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. 我已经设法在两个设备之间建立连接,但现在我想发送我无法做的数据。

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();

And is not explained in the guide. 并没有在指南中解释。 I don't know what the mHandler is or does. 我不知道mHandler是什么或做什么。

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? 如果在Server活动中,是否应该在onClick方法中为每个按钮发送不同字节代码的所有按钮? And in this code, how do we distinguish who is sending and who is receiving? 在此代码中,我们如何区分发送者和接收者?

Check out the BluetoothChat example that Google provides in the SDK. 查看Google在SDK中提供的BluetoothChat示例。 It'll show you how to implement basic sending of text over bluetooth. 它将向您展示如何通过蓝牙实现基本的文本发送。

您也可以在这里尝试教程示例

mHandler is used for passing message from your BluetoothHandle.java to your Activity. mHandler用于将消息从您的BluetoothHandle.java传递到您的Activity。 This will help you to update messages on your screen which are returned by BluetoothHandler. 这将帮助您更新屏幕上由BluetoothHandler返回的消息。

you have to create mHandler from your activity and call your handler like this - 你必须从你的活动中创建mHandler并像这样调用你的处理程序 -

mBluetoothHandler = new BluetoothHandler(this, mHandler);

and your BluetoothHandler.java has constructor like this - 你的BluetoothHandler.java有这样的构造函数 -

public class BluetoothHandler { 

    public BluetoothHandler(Context context, Handler handler) {
            mAdapter = BluetoothAdapter.getDefaultAdapter();
            mState = STATE_NONE;
            mHandler = handler;
            mcontext = context;
   }

}

For more details, please refer Android sample project of Bluetooth Chat . 有关详细信息,请参阅蓝牙聊天的 Android示例项目。 You can also use this link : http://myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html 您还可以使用以下链接: http//myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html

Can you please describe the error as seen by you? 你能否描述一下你所看到的错误?

As informed by Ankit and Addy, BlueToothChat is the best code for you to refer. 正如Ankit和Addy所知,BlueToothChat是您推荐的最佳代码。 Conduct an experiment by loading it on 2 android devices - use one as server other as client to exchange the messages between them. 通过将其加载到2个Android设备上进行实验 - 使用一个作为客户端的服务器来交换它们之间的消息。 Such experiment will help you to understand it's code and decide your coding logic. 这样的实验将帮助您理解它的代码并决定您的编码逻辑。

// Enter code here

Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        byte[] writeBuf = (byte[]) msg.obj;
        int begin = (int)msg.arg1;
        int end = (int)msg.arg2;

        switch(msg.what) {
            case 1:
                String writeMessage = new String(writeBuf);
                writeMessage = writeMessage.substring(begin, end);
                break;
        }
    }
};

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

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