android-BluetoothServerSocket

来源:互联网 发布:考勤工资软件 编辑:程序博客网 时间:2024/06/07 21:13

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:

  • BluetoothSocket

Summary


Public methods

BluetoothSocketaccept()

Block until a connection is established.

阻塞直到建立连接。

BluetoothSocketaccept(int timeout)

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

voidclose()

Immediately close this socket, and release all associated resources.

StringtoString()

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.

ReturnsBluetoothSocketa connected BluetoothSocketThrowsIOExceptionon 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.

ParameterstimeoutintReturnsBluetoothSocketa connected BluetoothSocketThrowsIOExceptionon 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().

ThrowsIOException 

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()) 

ReturnsStringa string representation of the object.
0 0
原创粉丝点击