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

时间:2021-01-18 18:57:49

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. I want to be able to press a button on the server device and display it on the client.

为了确切地解释我想做什么,当两个设备成对时,在每个设备(服务器和客户端)上打开两个不同的活动,在服务器活动中我有不同的按钮,在客户端活动中只有一个textview。我希望能够在服务器设备上按下一个按钮,并在客户机上显示它。

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? And in this code, how do we distinguish who is sending and who is receiving?

除了错误之外,我甚至不知道该把代码放在哪里。是否应该在我打开或主要打开的第二个活动(服务器和客户机)中?如果在服务器活动中,是否应该在onClick方法中为每个按钮发送具有不同字节码的所有按钮?在这个代码中,我们如何区分发送者和接收者?

5 个解决方案

#1


10  

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

查看谷歌在SDK中提供的bluetooth聊天示例。它将向您展示如何通过蓝牙实现文本的基本发送。

#2


1  

You can also try the tutorial example here

您还可以尝试这里的教程示例。

#3


0  

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. Conduct an experiment by loading it on 2 android devices - use one as server other as client to exchange the messages between them. Such experiment will help you to understand it's code and decide your coding logic.

根据Ankit和Addy的信息,蓝牙聊天是最好的参考代码。通过将其加载到2个android设备上进行实验——使用一个作为客户端来交换他们之间的消息。这样的实验将帮助您理解它的代码并决定您的编码逻辑。

#4


0  

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;
}
}
};

#5


0  

mHandler is used for passing message from your BluetoothHandle.java to your Activity. This will help you to update messages on your screen which are returned by BluetoothHandler.

mHandler用于从您的蓝牙手柄中传递消息。java到你的活动。这将帮助您更新由BluetoothHandler返回的屏幕上的消息。

you have to create mHandler from your activity and call your handler like this -

你必须从你的活动中创建mHandler并且像这样调用你的handler -

mBluetoothHandler = new BluetoothHandler(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 . You can also use this link : http://myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html

更多详情,请参考蓝牙聊天的Android示例项目。您还可以使用这个链接:myandroidappdevelopment . blogspot.in/2013/05/bluetooat -example.html

#1


10  

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

查看谷歌在SDK中提供的bluetooth聊天示例。它将向您展示如何通过蓝牙实现文本的基本发送。

#2


1  

You can also try the tutorial example here

您还可以尝试这里的教程示例。

#3


0  

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. Conduct an experiment by loading it on 2 android devices - use one as server other as client to exchange the messages between them. Such experiment will help you to understand it's code and decide your coding logic.

根据Ankit和Addy的信息,蓝牙聊天是最好的参考代码。通过将其加载到2个android设备上进行实验——使用一个作为客户端来交换他们之间的消息。这样的实验将帮助您理解它的代码并决定您的编码逻辑。

#4


0  

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;
}
}
};

#5


0  

mHandler is used for passing message from your BluetoothHandle.java to your Activity. This will help you to update messages on your screen which are returned by BluetoothHandler.

mHandler用于从您的蓝牙手柄中传递消息。java到你的活动。这将帮助您更新由BluetoothHandler返回的屏幕上的消息。

you have to create mHandler from your activity and call your handler like this -

你必须从你的活动中创建mHandler并且像这样调用你的handler -

mBluetoothHandler = new BluetoothHandler(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 . You can also use this link : http://myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html

更多详情,请参考蓝牙聊天的Android示例项目。您还可以使用这个链接:myandroidappdevelopment . blogspot.in/2013/05/bluetooat -example.html