安卓蓝牙API(4)

连接设备:
如何启动两个设备之间的连接。
充当服务器的设备和客户端的设备通过不同的方式得到BluetoothSocket。【具体是:The server will receive it when an incoming connection is accepted. The client will receive it when it opens an RFCOMM channel to the server.】
Connecting as a server
当你要连接两个设备的时候,一个必须充当服务器,开启一个持续的BluetoothServerSocket。它的目的是监听请求信息,并且当一个被接受的时候,提供一个BluetoothSoket。(当BluetoothSocket已经得到的时候,BluetoothServerSocket可以被丢弃,除非你想接受更多的请求连接)
这是建立server socket 和接收连接的基本步骤:
1.使用
listenUsingRfcommWithServiceRecord(String, UUID) 方法
得到一个 BluetoothServerSocket
说明:【string is an identifiable name of your service, which
the system will automatically write to a new Service
Discovery Protocol (SDP) database entry on the device
(the name is arbitrary and can simply be your
application name).】(不会翻译,,,)
The UUID is also included in the SDP
entry and will be the basis for the connection
agreement with the client device.
就是说,当客户想和这个设备连接的时候, 它将携带一个独特的和他想连接的服务一致的UUID。
These UUIDs must match in order for the connection to be accepted (in the next step)

2.开始监听连接消息 accept()
This is a blocking call. It will return when either a connection has been accepted or an exception has occurred. A
connection is accepted only when a remote device has sent a connection request with a UUID matching the one
registered with this listening server socket. When successful, accept() will return a connected BluetoothSocket.

3使用 close()方法,除非你想接收更多连接。
这释放 server socket和它的所有资源, 但是没有关闭通过accept()返回的连接好的BluetoothSocket。不像TCP/IP, RFCOMM 一次只允许一个连接好的客户端通道。所以大多数情况下,接收一个connected socket以后立即close()是有意义的。

注意
accept() 不应该直接在main activity 里执行,因为它是阻塞的。It will prevent any other interaction with the application. It usually makes sense to do all work with a BluetoothServerSocket or BluetoothSocket in a new thread managed by your application. To abort a blocked call such as accept() , call close()
on the BluetoothServerSocket (or BluetoothSocket) from another thread and the blocked call will immediately return.
Note that all methods on a BluetoothServerSocket or BluetoothSocket are thread-safe.


Here’s a simplified thread for the server component that accepts incoming connections:

private class AcceptThread extends Thread {
 private final BluetoothServerSocket  mmServerSocket;
 public AcceptThread() {
 // Use a temporary object that is later assigned to mmServerSocket,
 // because mmServerSocket is final
 BluetoothServerSocket  tmp = null ;
 try {
 // MY_UUID is the app's UUID string, also used by the client code
 tmp = mBluetoothAdapter. listenUsingRfcommWithServiceRecord(NAME,  MY_UUID);
 } catch (IOException e) { }
 mmServerSocket = tmp;
 }
 public void run() {
 BluetoothSocket  socket = null ;
 // Keep listening until exception occurs or a socket is returned
 while (true) {
 try {
 socket = mmServerSocket. accept();
 } catch (IOException e) {
 break;
 }
 // If a connection was accepted
 if (socket != null ) {
 // Do work to manage the connection (in a separate thread)
 manageConnectedSocket(socket);
 mmServerSocket. close();
 break;
 }
 }
 }
 /** Will cancel the listening socket, and cause the thread to finish */
 public void cancel() {
 try {
 mmServerSocket. close();
 } catch (IOException e) { }
 }
}

In this example, only one incoming connection is desired, so as soon as a connection is accepted and the
BluetoothSocket is acquired, the application sends the acquired BluetoothSocket to a separate thread, closes the BluetoothServerSocket and breaks the loop.
Note that when accept() returns the BluetoothSocket, the socket is already connected, so you should not call
connect() (as you do from the client-side).
manageConnectedSocket() is a fictional method in the application that will initiate the thread for transferring data, which
is discussed in the section about Managing a Connection.
You should usually close your BluetoothServerSocket as soon as you are done listening for incoming connections. In
this example, close() is called as soon as the BluetoothSocket is acquired. You may also want to provide a public
method in your thread that can close the private BluetoothSocket in the event that you need to stop listening on the
server socket.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值