一、连接设备
蓝牙通讯分为 server服务器端和 client客户端,它们之间使用 BluetoothSocket 类的不同方法来获取数据。
1. 作为服务器
如果一个设备需要和两个或多个设备连接时,就需要作为一个 server来传输,在 android中提供了 BluetoothServerSocket类来处理用户发来的信息,服务器端套接字在接受( accepted) 一个客户发来的 BluetoothSocket时作出相应的响应。示例代码如下:
Java代码:
- 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) { }
- }
- }
2. 作为客户端
以便初始化一个连接到远程设备,首先必须获取本地的 BluetoothDevice对象,相关的方法在我们 Android蓝牙 API之 BluetoothAdapter类 的两篇文章中有讲到,这里不再赘述,相关的示例代码如下:
Java代码:
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蓝牙相关开发可以很好的掌握了,通过 BluetoothServerSocket可以方便的创建一个蓝牙服务器,使用 BluetoothSocket类可以很好的处理连接,今天我们继续上次的内容说下 Android下如何管理蓝牙套接字的连接,今天仍然使用 BluetoothSocket类,处理具体的数据流。
在 Java上处理数据流很简单,提供了 InputSream、 OutputSream和字节数组的之间的转化。今天 eoeandroid将和大家一起说下处理上次遗留的 manageConnectedSocket方法的细节,由于蓝牙传输中可能存在中断,所以为了防止阻塞需要开一个工作者线程,相关的示例代码
Java代码:
- private class ConnectedThread extends Thread {
- private final BluetoothSocket cwjSocket;
- private final InputStream cwjInStream;
- private final OutputStream cwjOutStream;
- public ConnectedThread(BluetoothSocket socket) {
- cwjSocket = socket;
- InputStream tmpIn = null; //上面定义的为final这是使用temp临时对象
- OutputStream tmpOut = null;
- try {
- tmpIn = socket.getInputStream(); //使用getInputStream作为一个流处理
- tmpOut = socket.getOutputStream();
- } catch (IOException e) { }
- cwjInStream = tmpIn;
- cwjOutStream = tmpOut;
- }
- public void run() {
- byte[] buffer = new byte[1024];
- int bytes;
- while (true) {
- try {
- bytes = cwjInStream.read(buffer);
- mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget(); //传递给UI线程
- } catch (IOException e) {
- break;
- }
- }
- }
- public void write(byte[] bytes) {
- try {
- cwjOutStream.write(bytes);
- } catch (IOException e) { }
- }
- public void cancel() {
- try {
- cwjSocket.close();
- } catch (IOException e) { }
- }
- }
对于具体的连接,我们看到在 Android平台上使用了 Java标准的输入、输出流操作, BluetoothSocket 提供的 getInputStream()和 getOutputStream()方法可以很好的处理我们具体的数据。