Android蓝牙API之BluetoothSocket类(1)
来源:互联网 发布:php异步发送邮件 编辑:程序博客网 时间:2024/05/29 08:53
前面两篇文章中我们提到了有关Android平台蓝牙的配对、发现、启用等操作,本文开始通过BluetoothSocket类建立有关蓝牙通讯的套接字。从Android 2.0开始支持这一特性,蓝牙和LAN一样通过MAC地址来识别远程设备,建立完通讯连接RFCOMM通道后以输入、输出流方式通讯。
一、连接设备
蓝牙通讯分为server服务器端和client客户端,它们之间使用BluetoothSocket 类的不同方法来获取数据,
1. 作为服务器
如果一个设备需要和两个或多个设备连接时,就需要作为一个server来传输,在android中提供了BluetoothServerSocket类来处理用户发来的信息,服务器端套接字在接受(accepted) 一个客户发来的BluetoothSocket时作出相应的响应。示例代码如下:
private class AcceptThread extends Thread {
private final BluetoothServerSocket cwjServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null; //使用一个临时对象代替,因为cwjServerSocket定义为final
try {
tmp = myAdapter.listenUsingRfcommWithServiceRecord(NAME, CWJ_UUID); //服务仅监听
} catch (IOException e) { }
cwjServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
while (true) { //保持连接直到异常发生或套接字返回
try {
socket = cwjServerSocket.accept(); //如果一个连接同意
} catch (IOException e) {
break;
}
if (socket != null) {
manageConnectedSocket(socket); //管理一个已经连接的RFCOMM通道在单独的线程。
cwjServerSocket.close();
break;
}
}
}
public void cancel() { //取消套接字连接,然后线程返回
try {
cwjServerSocket.close();
} catch (IOException e) { }
}
}
在这里android开发网提醒大家需要注意的是服务器一般处理多个任务不嫩阻塞,必须使用异步的方法这里我们开了一个线程,目前Android的虚拟机上层没有提供I/O模型,这里我们以后会讲解高负载情况下性能优化解决方案。
2. 作为客户端
以便初始化一个连接到远程设备,首先必须获取本地的BluetoothDevice对象,相关的方法在我们 Android蓝牙API之BluetoothAdapter类 的两篇文章中有讲到,这里不再赘述,相关的示例代码如下:
private class ConnectThread extends Thread {
private final BluetoothSocket cwjSocket;
private final BluetoothDevice cwjDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp= null;
cwjDevice= device;
try {
tmp= device.createRfcommSocketToServiceRecord(CWJ_UUID); //客户端创建
} catch (IOException e) { }
cwjSocket= tmp;
}
public void run() {
myAdapter.cancelDiscovery(); //取消发现远程设备,这样会降低系统性能
try {
cwjSocket.connect();
} catch (IOException connectException) {
try {
cwjSocket.close();
} catch (IOException closeException) { }
return;
}
manageConnectedSocket(cwjSocket); //管理一个已经连接的RFCOMM通道在单独的线程。
}
public void cancel() {
try {
cwjSocket.close();
} catch (IOException e) { }
}
}
经过上面的介绍我们可以看到在Android平台上使用蓝牙通讯相对比较方便和简单,有关数据的具体通讯我们将在下次Android蓝牙API之BluetoothSocket类(2) 讲到manageConnectedSocket的具体实现。
- Android蓝牙API之BluetoothSocket类(1)
- Android蓝牙API之BluetoothSocket类(2)
- Android蓝牙API之 BluetoothSocket类
- Android蓝牙API之BluetoothSocket类
- Android蓝牙API之BluetoothAdapter类(1)
- Android蓝牙API之BluetoothAdapter类(1)
- Android蓝牙API之BluetoothAdapter类(1)
- android 蓝牙开发 蓝牙 bluetoothsocket connect 经常出错 解决方法
- android-BluetoothSocket
- Android-BluetoothSocket建立有关蓝牙通讯的套接字
- Android蓝牙API之BluetoothAdapter类(2)
- Android蓝牙API之BluetoothAdapter类
- Android蓝牙API之BluetoothAdapter类
- Android蓝牙API之BluetoothAdapter类
- Android 蓝牙API之BluetoothAdapter类使用
- Android蓝牙API之BluetoothAdapter类
- Android蓝牙API之BluetoothAdapter类
- Android踩过的坑之bluetoothSocket
- 加密解密教程之必备基础知识
- http 协议详解
- Android蓝牙API之BluetoothAdapter类(2)
- java排序算法
- 从计算机复试看中国教育——一个面试官的经历
- Android蓝牙API之BluetoothSocket类(1)
- Apache
- 第六章 PL/SQL与Oracle间交互
- 使用postfix reload,stop等命令报错 the postfix system is not running
- Android蓝牙API之BluetoothSocket类(2)
- GTK+ layout management 转载http://zetcode.com/tutorials/gtktutorial/gtklayoutmanagement/
- C/C++语法知识:typedef struct 用法详解
- pku 3604 Professor Ben 积性函数加筛法
- MzTreeView1.4版本,异步动态加载子节点