android-BluetoothServerSocket

BluetoothServerSocket

public final class BluetoothServerSocket 
extends Object implements Closeable

java.lang.Object
   ↳ android.bluetooth.BluetoothServerSocket


A listening Bluetooth socket.

监听蓝牙socket

The interface for Bluetooth Sockets is similar to that of TCP sockets: Socket and ServerSocket. On the server side, use a BluetoothServerSocket to create a listening server socket. When a connection is accepted by the BluetoothServerSocket, it will return a new BluetoothSocket to manage the connection. On the client side, use a single BluetoothSocket to both initiate an outgoing connection and to manage the connection.


蓝牙Sockets 的接口是类似于TCP套接字:Sockets 和考察。在服务器端,使用BluetoothServerSocket创建一个监听服务器Sockets 被BluetoothServerSocket接受连接时,它将返回一个新的BluetoothSocket管理连接。在客户端,使用单个BluetoothSocket发起一个即将离任的连接和管理连接。

The most common type of Bluetooth socket is RFCOMM, which is the type supported by the Android APIs. RFCOMM is a connection-oriented, streaming transport over Bluetooth. It is also known as the Serial Port Profile (SPP).

To create a listening BluetoothServerSocket that's ready for incoming connections, use BluetoothAdapter.listenUsingRfcommWithServiceRecord(). Then call accept() to listen for incoming connection requests. This call will block until a connection is established, at which point, it will return aBluetoothSocket to manage the connection. Once the BluetoothSocket is acquired, it's a good idea to call close() on the BluetoothServerSocket when it's no longer needed for accepting connections. Closing the BluetoothServerSocket will not close the returned BluetoothSocket.

创建一个倾听BluetoothServerSocket准备到达的连接,使用BluetoothAdapter.listenUsingRfcommWithServiceRecord()。然后调用accept()来侦听传入的连接请求。这个调用会阻塞,直到建立连接,此时,它将返回aBluetoothSocket管理连接。BluetoothSocket一旦获得,这是一个好主意叫关闭()BluetoothServerSocket时不再需要接受连接。关闭BluetoothSocket返回的BluetoothServerSocket不会关闭。

BluetoothServerSocket is thread safe. In particular, close() will always immediately abort ongoing operations and close the server socket.

BluetoothServerSocket是线程安全的。特别是,close()总是会立即中止正在进行的操作并关闭服务器套接字。

Note: Requires the BLUETOOTH permission.

Developer Guides

For more information about using Bluetooth, read the Bluetooth developer guide.

See also:

Summary


Public methods

BluetoothSocket accept()

Block until a connection is established.

阻塞直到建立连接。

BluetoothSocket accept(int timeout)

Block until a connection is established, with timeout.阻塞直到建立连接,超时。

void close()

Immediately close this socket, and release all associated resources.

String toString()

Returns a string representation of the object.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public methods


accept

Added in  API level 5
BluetoothSocket accept ()

Block until a connection is established.

Returns a connected BluetoothSocket on successful connection.

Once this call returns, it can be called again to accept subsequent incoming connections.

close() can be used to abort this call from another thread.

Returns
BluetoothSocket a connected BluetoothSocket
Throws
IOException on error, for example this call was aborted, or timeout

accept

Added in  API level 5
BluetoothSocket accept (int timeout)

Block until a connection is established, with timeout.

Returns a connected BluetoothSocket on successful connection.

Once this call returns, it can be called again to accept subsequent incoming connections.

close() can be used to abort this call from another thread.

Parameters
timeout int
Returns
BluetoothSocket a connected BluetoothSocket
Throws
IOException on error, for example this call was aborted, or timeout

close

Added in  API level 5
void close ()

Immediately close this socket, and release all associated resources.

Causes blocked calls on this socket in other threads to immediately throw an IOException.

Closing the BluetoothServerSocket will not close any BluetoothSocket received from accept().

Throws
IOException  

toString

Added in  API level 5
String toString ()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值