Android 安卓蓝牙示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12562875/
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
Android Bluetooth Example
提问by Pratik
Can anybody give me Android Bluetooth
communication tutorial links or hints? Please don't tell me to refer to the BluetoothChat example, I can only understand how to discover and connect to devices but don't know how to send and receive the data over Bluetooth.
有人可以给我 AndroidBluetooth
通信教程链接或提示吗?请不要告诉我参考BluetoothChat示例,我只能了解如何发现和连接到设备,但不知道如何通过蓝牙发送和接收数据。
I am actually working on an Android and embedded Bluetooth
device project.
Please help me out.
我实际上正在研究一个 Android 和嵌入式Bluetooth
设备项目。请帮帮我。
回答by vipul mittal
I have also used following link as others have suggested you for bluetooth communication.
我还使用了以下链接,因为其他人建议您进行蓝牙通信。
http://developer.android.com/guide/topics/connectivity/bluetooth.html
http://developer.android.com/guide/topics/connectivity/bluetooth.html
The thing is all you need is a class BluetoothChatService.java
事情就是你需要的只是一个类 BluetoothChatService.java
this class has following threads:
这个类有以下线程:
- Accept
- Connecting
- Connected
- 接受
- 连接
- 连接的
Now when you call start function of the BluetoothChatService like:
现在,当您调用 BluetoothChatService 的启动函数时,例如:
mChatService.start();
It starts accept thread which means it will start looking for connection.
它开始接受线程,这意味着它将开始寻找连接。
Now when you call
现在当你打电话
mChatService.connect(<deviceObject>,false/true);
Here first argument is device object that you can get from paired devices list or when you scan for devices you will get all the devices in range you can pass that object to this function and 2nd argument is a boolean to make secure or insecure connection.
这里的第一个参数是您可以从配对设备列表中获取的设备对象,或者当您扫描设备时,您将获得范围内的所有设备,您可以将该对象传递给此函数,第二个参数是一个布尔值,用于建立安全或不安全的连接。
connect
function will start connecting thread which will look for any device which is running accept thread.
connect
函数将开始连接线程,该线程将查找正在运行接受线程的任何设备。
When such a device is found both accept thread and connecting thread will call connected function in BluetoothChatService:
当找到这样的设备时,accept 线程和连接线程都会调用 BluetoothChatService 中的连接函数:
connected(mmSocket, mmDevice, mSocketType);
this method starts connected thread in both the devices:
Using this socket object connected thread obtains the input and output stream to the other device.
And calls read
function on inputstream in a while loop so that it's always trying read from other device so that whenever other device send a message this read function returns that message.
此方法在两个设备中启动连接线程:使用此套接字对象连接线程获取到另一个设备的输入和输出流。并read
在 while 循环中调用inputstream 上的函数,以便它始终尝试从其他设备读取,以便每当其他设备发送消息时,此读取函数都会返回该消息。
BluetoothChatService also has a write
method which takes byte[]
as input and calls write method on connected thread.
BluetoothChatService 也有一个write
方法,它将byte[]
作为输入并在连接的线程上调用 write 方法。
mChatService.write("your message".getByte());
write method in connected thread just write this byte data to outputsream of the other device.
连接线程中的 write 方法只是将此字节数据写入其他设备的输出流。
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
// mHandler.obtainMessage(
// BluetoothGameSetupActivity.MESSAGE_WRITE, -1, -1,
// buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
Now to communicate between two devices just call write function on mChatService and handle the message that you will receive on the other device.
现在要在两个设备之间进行通信,只需在 mChatService 上调用 write 函数并处理您将在另一台设备上收到的消息。