Java 两台安卓设备之间的蓝牙数据传输
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21220993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bluetooth data transfer between two Android devices
提问by Simpsons
I have been following this Android guidefor 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.
为了准确解释我想要做什么,当两个设备配对时,每个设备(服务器和客户端)上会打开两个不同的活动,其中在服务器活动上我有不同的按钮,而在客户端活动上只有一个文本视图。我希望能够按下服务器设备上的按钮并将其显示在客户端上。
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 方法中为每个按钮发送具有不同字节码的所有按钮?在这段代码中,我们如何区分谁在发送和谁在接收?
采纳答案by Joreyaesh
Check out the BluetoothChatexample that Google provides in the SDK. It'll show you how to implement basic sending of text over bluetooth.
查看Google 在 SDK 中提供的BluetoothChat示例。它将向您展示如何通过蓝牙实现基本的文本发送。
回答by Ankit
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 用于将消息从您的 BluetoothHandle.java 传递到您的活动。这将帮助您更新由 BluetoothHandler 返回的屏幕上的消息。
you have to create mHandler from your activity and call your handler like this -
你必须从你的活动中创建 mHandler 并像这样调用你的处理程序 -
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示例工程。您也可以使用此链接:http: //myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html
回答by SunGa
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 所告知的,BlueToothChat 是您可以参考的最佳代码。通过在 2 个 android 设备上加载它来进行实验 - 使用一个作为服务器,另一个作为客户端在它们之间交换消息。这样的实验将帮助你理解它的代码并决定你的编码逻辑。
回答by user3420105
// 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;
}
}
};