# BluetoothServerSocket

Added in[API level 5](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Methods](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#inhmethods)  

# BluetoothServerSocket

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/bluetooth/BluetoothServerSocket "View this page in Kotlin")\|Java

`
public

final

class
BluetoothServerSocket
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


implements

`[Closeable](https://developer.android.com/reference/java/io/Closeable)`


`

|---|-----------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.bluetooth.BluetoothServerSocket |

<br />

*** ** * ** ***

A listening Bluetooth socket.

The interface for Bluetooth Sockets is similar to that of TCP sockets:[Socket](https://developer.android.com/reference/java/net/Socket)and[ServerSocket](https://developer.android.com/reference/java/net/ServerSocket). On the server side, use a[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)to create a listening server socket. When a connection is accepted by the[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket), it will return a new[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)to manage the connection. On the client side, use a single[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)to both initiate an outgoing connection and to manage the connection.

For Bluetooth BR/EDR, the most common type of socket is RFCOMM, which is the type supported by the Android APIs. RFCOMM is a connection-oriented, streaming transport over Bluetooth BR/EDR. It is also known as the Serial Port Profile (SPP). To create a listening[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)that's ready for incoming Bluetooth BR/EDR connections, use[BluetoothAdapter.listenUsingRfcommWithServiceRecord()](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#listenUsingRfcommWithServiceRecord(java.lang.String,%20java.util.UUID)).

For Bluetooth LE, the socket uses LE Connection-oriented Channel (CoC). LE CoC is a connection-oriented, streaming transport over Bluetooth LE and has a credit-based flow control. Correspondingly, use[BluetoothAdapter.listenUsingL2capChannel()](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#listenUsingL2capChannel())to create a listening[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)that's ready for incoming Bluetooth LE CoC connections. For LE CoC, you can use[getPsm()](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#getPsm())to get the protocol/service multiplexer (PSM) value that the peer needs to use to connect to your socket.

After the listening[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)is created, call[accept()](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#accept())to listen for incoming connection requests. This call will block until a connection is established, at which point, it will return a[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)to manage the connection. Once the[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)is acquired, it's a good idea to call[close()](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#close())on the[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)when it's no longer needed for accepting connections. Closing the[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)will*not* close the returned[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket).

[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)is thread safe. In particular,[close()](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#close())will always immediately abort ongoing operations and close the server socket.

<br />

### Developer Guides

For more information about using Bluetooth, read the[Bluetooth](https://developer.android.com/guide/topics/connectivity/bluetooth)developer guide.

<br />

**See also:**

- [BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)

## Summary

|                                                                                                                                                                     ### Public methods                                                                                                                                                                     ||
|-------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket) | ` `[accept](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#accept())`() ` Block until a connection is established.                                                                                                       |
| ` `[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket) | ` `[accept](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#accept(int))`(int timeout) ` Block until a connection is established, with timeout.                                                                           |
| ` void`                                                                                         | ` `[close](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#close())`() ` Immediately close this socket, and release all associated resources.                                                                             |
| ` int`                                                                                          | ` `[getPsm](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#getPsm())`() ` Returns the assigned dynamic protocol/service multiplexer (PSM) value for the listening L2CAP Connection-oriented Channel (CoC) server socket. |
| ` `[String](https://developer.android.com/reference/java/lang/String)                           | ` `[toString](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#toString())`() ` Returns a string representation of the object.                                                                                             |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[java.io.Closeable](https://developer.android.com/reference/java/io/Closeable)` ` |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/io/Closeable#close())`() ` Closes this stream and releases any system resources associated with it. | ||
| From interface` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||

## Public methods

### accept

Added in[API level 5](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public BluetoothSocket accept ()
```

Block until a connection is established.

Returns a connected[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)on successful connection.

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

[close()](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#close())can be used to abort this call from another thread.

<br />

|                                                                                                   Returns                                                                                                    ||
|----------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|
| [BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket) | a connected[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket) <br /> |

|                                                               Throws                                                                ||
|----------------------------------------------------------------------------|---------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | on error, for example this call was aborted, or timeout |

### accept

Added in[API level 5](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public BluetoothSocket accept (int timeout)
```

Block until a connection is established, with timeout.

Returns a connected[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)on successful connection.

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

[close()](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#close())can be used to abort this call from another thread.

<br />

|       Parameters        ||
|-----------|--------------|
| `timeout` | `int` <br /> |

|                                                                                                   Returns                                                                                                    ||
|----------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|
| [BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket) | a connected[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket) <br /> |

|                                                               Throws                                                                ||
|----------------------------------------------------------------------------|---------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | on error, for example this call was aborted, or timeout |

### close

Added in[API level 5](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)will*not* close any[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)received from[accept()](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket#accept()).

<br />

|                                    Throws                                     ||
|----------------------------------------------------------------------------|---|
| [IOException](https://developer.android.com/reference/java/io/IOException) |   |

### getPsm

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int getPsm ()
```

Returns the assigned dynamic protocol/service multiplexer (PSM) value for the listening L2CAP Connection-oriented Channel (CoC) server socket. This server socket must be returned by the[BluetoothAdapter.listenUsingL2capChannel()](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#listenUsingL2capChannel())or[BluetoothAdapter.listenUsingInsecureL2capChannel()](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#listenUsingInsecureL2capChannel()). The returned value is undefined if this method is called on non-L2CAP server sockets.

<br />

|                                Returns                                ||
|-------|----------------------------------------------------------------|
| `int` | the assigned PSM or LE_PSM value depending on transport <br /> |

### toString

Added in[API level 5](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString ()
```

Returns a string representation of the object.

<br />

|                                                      Returns                                                      ||
|--------------------------------------------------------------------|-----------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | a string representation of the object. <br /> |