你好,游客 登录 注册 搜索
背景:
阅读新闻

Android蓝牙开发浅析

[日期:2012-04-23] 来源:Linux社区  作者:gd920129 [字体:大 中 小]

4. 蓝牙Socket通信

如果打算建议两个蓝牙设备之间的连接,则必须实现服务器端与客户端的机制。当两个设备在同一个RFCOMM channel下分别拥有一个连接的BluetoothSocket,这两个设备才可以说是建立了连接。

服务器设备与客户端设备获取BluetoothSocket的途径是不同的。服务器设备是通过accepted一个incoming connection来获取的,而客户端设备则是通过打开一个到服务器的RFCOMM channel来获取的。


服务器端的实现

通过调用BluetoothAdapter的listenUsingRfcommWithServiceRecord(String, UUID)方法来获取BluetoothServerSocket(UUID用于客户端与服务器端之间的配对)

调用BluetoothServerSocket的accept()方法监听连接请求,如果收到请求,则返回一个BluetoothSocket实例(此方法为block方法,应置于新线程中)

如果不想在accept其他的连接,则调用BluetoothServerSocket的close()方法释放资源(调用该方法后,之前获得的BluetoothSocket实例并没有close。但由于RFCOMM一个时刻只允许在一条channel中有一个连接,则一般在accept一个连接后,便close掉BluetoothServerSocket)

  1. private class AcceptThread extends Thread {  
  2.     private final BluetoothServerSocket mmServerSocket;  
  3.   
  4.     public AcceptThread() {  
  5.         // Use a temporary object that is later assigned to mmServerSocket,   
  6.         // because mmServerSocket is final   
  7.         BluetoothServerSocket tmp = null;  
  8.         try {  
  9.             // MY_UUID is the app's UUID string, also used by the client code   
  10.             tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);  
  11.         } catch (IOException e) { }  
  12.         mmServerSocket = tmp;  
  13.     }  
  14.   
  15.     public void run() {  
  16.         BluetoothSocket socket = null;  
  17.         // Keep listening until exception occurs or a socket is returned   
  18.         while (true) {  
  19.             try {  
  20.                 socket = mmServerSocket.accept();  
  21.             } catch (IOException e) {  
  22.                 break;  
  23.             }  
  24.             // If a connection was accepted   
  25.             if (socket != null) {  
  26.                 // Do work to manage the connection (in a separate thread)   
  27.                 manageConnectedSocket(socket);  
  28.                 mmServerSocket.close();  
  29.                 break;  
  30.             }  
  31.         }  
  32.     }  
  33.   
  34.     /** Will cancel the listening socket, and cause the thread to finish */  
  35.     public void cancel() {  
  36.         try {  
  37.             mmServerSocket.close();  
  38.         } catch (IOException e) { }  
  39.     }  
  40. }  

客户端的实现

通过搜索得到服务器端的BluetoothService

调用BluetoothService的listenUsingRfcommWithServiceRecord(String, UUID)方法获取BluetoothSocket(该UUID应该同于服务器端的UUID)

调用BluetoothSocket的connect()方法(该方法为block方法),如果UUID同服务器端的UUID匹配,并且连接被服务器端accept,则connect()方法返回

注意:在调用connect()方法之前,应当确定当前没有搜索设备,否则连接会变得非常慢并且容易失败

  1. private class ConnectThread extends Thread {  
  2.     private final BluetoothSocket mmSocket;  
  3.     private final BluetoothDevice mmDevice;  
  4.   
  5.     public ConnectThread(BluetoothDevice device) {  
  6.         // Use a temporary object that is later assigned to mmSocket,   
  7.         // because mmSocket is final   
  8.         BluetoothSocket tmp = null;  
  9.         mmDevice = device;  
  10.   
  11.         // Get a BluetoothSocket to connect with the given BluetoothDevice   
  12.         try {  
  13.             // MY_UUID is the app's UUID string, also used by the server code   
  14.             tmp = device.createRfcommSocketToServiceRecord(MY_UUID);  
  15.         } catch (IOException e) { }  
  16.         mmSocket = tmp;  
  17.     }  
  18.   
  19.     public void run() {  
  20.         // Cancel discovery because it will slow down the connection   
  21.         mBluetoothAdapter.cancelDiscovery();  
  22.   
  23.         try {  
  24.             // Connect the device through the socket. This will block   
  25.             // until it succeeds or throws an exception   
  26.             mmSocket.connect();  
  27.         } catch (IOException connectException) {  
  28.             // Unable to connect; close the socket and get out   
  29.             try {  
  30.                 mmSocket.close();  
  31.             } catch (IOException closeException) { }  
  32.             return;  
  33.         }  
  34.   
  35.         // Do work to manage the connection (in a separate thread)   
  36.         manageConnectedSocket(mmSocket);  
  37.     }  
  38.   
  39.     /** Will cancel an in-progress connection, and close the socket */  
  40.     public void cancel() {  
  41.         try {  
  42.             mmSocket.close();  
  43.         } catch (IOException e) { }  
  44.     }  
  45. }  
linux
相关资讯       Android蓝牙开发 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/5/22 17:57:31
<script>alert("ddddd");</script>