Pra*_*tik 52 android bluetooth
任何人都可以给我Android Bluetooth
通信教程链接或提示吗?请不要告诉我参考蓝牙的例子,我只能理解如何发现和连接设备,但不知道如何通过蓝牙发送和接收数据.
我实际上正在开发一个Android和嵌入式Bluetooth
设备项目.请帮帮我..
vip*_*tal 10
我也使用了以下链接,因为其他人建议你进行蓝牙通信.
http://developer.android.com/guide/topics/connectivity/bluetooth.html
事情是你需要的只是一个班级 BluetoothChatService.java
这个类有以下线程:
现在当你调用BluetoothChatService的启动功能时:
mChatService.start();
Run Code Online (Sandbox Code Playgroud)
它开始接受线程,这意味着它将开始寻找连接.
现在你打电话的时候
mChatService.connect(<deviceObject>,false/true);
Run Code Online (Sandbox Code Playgroud)
这里的第一个参数是您可以从配对设备列表中获取的设备对象,或者当您扫描设备时,您将获得范围内的所有设备,您可以将该对象传递给此函数,第二个参数是一个布尔值,以进行安全或不安全的连接.
connect
函数将启动连接线程,该线程将查找正在运行接受线程的任何设备.
当发现这样的设备时,接受线程和连接线程将调用BluetoothChatService中的连接功能:
connected(mmSocket, mmDevice, mSocketType);
Run Code Online (Sandbox Code Playgroud)
此方法在两个设备中启动连接线程:使用此套接字对象连接线程获取输入和输出流到另一个设备.并且调用read
在while循环中的输入流上运行,因此它总是尝试从其他设备读取,以便每当其他设备发送消息时,此读取函数返回该消息.
BluetoothChatService还有一个write
方法,它byte[]
在连接的线程上作为输入并调用write方法.
mChatService.write("your message".getByte());
Run Code Online (Sandbox Code Playgroud)
连接线程中的write方法只是将该字节数据写入另一个设备的outputream.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在要在两个设备之间进行通信,只需在mChatService上调用write函数,并处理将在另一个设备上接收的消息.
归档时间: |
|
查看次数: |
146129 次 |
最近记录: |