# BluetoothSocket

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

# BluetoothSocket

*** ** * ** ***

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

`
public

final

class
BluetoothSocket
`  
`

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.BluetoothSocket |

<br />

*** ** * ** ***

A connected or connecting 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.

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[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)for connecting to a known device, use[BluetoothDevice.createRfcommSocketToServiceRecord()](https://developer.android.com/reference/android/bluetooth/BluetoothDevice#createRfcommSocketToServiceRecord(java.util.UUID)). Then call[connect()](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#connect())to attempt a connection to the remote device. This call will block until a connection is established or the connection fails.

To create a[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)as a server (or "host"), see the[BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)documentation.

Once the socket is connected, whether initiated as a client or accepted as a server, open the IO streams by calling[getInputStream()](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#getInputStream())and[getOutputStream()](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#getOutputStream())in order to retrieve[InputStream](https://developer.android.com/reference/java/io/InputStream)and[OutputStream](https://developer.android.com/reference/java/io/OutputStream)objects, respectively, which are automatically connected to the socket.

[BluetoothSocket](https://developer.android.com/reference/android/bluetooth/BluetoothSocket)is thread safe. In particular,[close()](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#close())will always immediately abort ongoing operations and close the 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:**

- [BluetoothServerSocket](https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket)
- [InputStream](https://developer.android.com/reference/java/io/InputStream)
- [OutputStream](https://developer.android.com/reference/java/io/OutputStream)

## Summary

|                                                          ### Constants                                                           ||
|-------|---------------------------------------------------------------------------------------------------------------------------|
| `int` | [TYPE_L2CAP](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#TYPE_L2CAP) L2CAP socket           |
| `int` | [TYPE_LE](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#TYPE_LE) L2CAP socket on LE transport |
| `int` | [TYPE_RFCOMM](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#TYPE_RFCOMM) RFCOMM socket        |
| `int` | [TYPE_SCO](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#TYPE_SCO) SCO socket                 |

|                                                                                                                                                   ### Public methods                                                                                                                                                    ||
|-------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                                         | ` `[close](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#close())`() ` Closes this stream and releases any system resources associated with it.                                            |
| ` void`                                                                                         | ` `[connect](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#connect())`() ` Attempt to connect to a remote device.                                                                          |
| ` int`                                                                                          | ` `[getConnectionType](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#getConnectionType())`() ` Get the type of the underlying connection.                                                  |
| ` `[InputStream](https://developer.android.com/reference/java/io/InputStream)                   | ` `[getInputStream](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#getInputStream())`() ` Get the input stream associated with this socket.                                                 |
| ` int`                                                                                          | ` `[getMaxReceivePacketSize](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#getMaxReceivePacketSize())`() ` Get the maximum supported Receive packet size for the underlying transport.     |
| ` int`                                                                                          | ` `[getMaxTransmitPacketSize](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#getMaxTransmitPacketSize())`() ` Get the maximum supported Transmit packet size for the underlying transport.  |
| ` `[OutputStream](https://developer.android.com/reference/java/io/OutputStream)                 | ` `[getOutputStream](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#getOutputStream())`() ` Get the output stream associated with this socket.                                              |
| ` `[BluetoothDevice](https://developer.android.com/reference/android/bluetooth/BluetoothDevice) | ` `[getRemoteDevice](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#getRemoteDevice())`() ` Get the remote device this socket is connecting, or connected, to.                              |
| ` boolean`                                                                                      | ` `[isConnected](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#isConnected())`() ` Get the connection status of this socket, ie, whether there is an active connection with remote device. |
| ` `[String](https://developer.android.com/reference/java/lang/String)                           | ` `[toString](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#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. | ||

## Constants

### TYPE_L2CAP

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

```
public static final int TYPE_L2CAP
```

L2CAP socket

<br />

Constant Value: 3 (0x00000003)  

### TYPE_LE

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

```
public static final int TYPE_LE
```

L2CAP socket on LE transport

<br />

Constant Value: 4 (0x00000004)  

### TYPE_RFCOMM

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

```
public static final int TYPE_RFCOMM
```

RFCOMM socket

<br />

Constant Value: 1 (0x00000001)  

### TYPE_SCO

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

```
public static final int TYPE_SCO
```

SCO socket

<br />

Constant Value: 2 (0x00000002)

## Public methods

### close

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

```
public void close ()
```

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in[AutoCloseable.close()](https://developer.android.com/reference/java/lang/AutoCloseable#close()), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally*mark* the`Closeable`as closed, prior to throwing the`IOException`.

<br />

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

### connect

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

```
public void connect ()
```

Attempt to connect to a remote device.

This method will block until a connection is made or the connection fails. If this method returns without an exception then this socket is now connected.

Creating new connections to remote Bluetooth devices should not be attempted while device discovery is in progress. Device discovery is a heavyweight procedure on the Bluetooth adapter and will significantly slow a device connection. Use[BluetoothAdapter.cancelDiscovery()](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#cancelDiscovery())to cancel an ongoing discovery. Discovery is not managed by the Activity, but is run as a system service, so an application should always call[BluetoothAdapter.cancelDiscovery()](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#cancelDiscovery())even if it did not directly request a discovery, just to be sure.

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

Requires the[Manifest.permission.BLUETOOTH_PRIVILEGED](https://developer.android.com/reference/android/Manifest.permission#BLUETOOTH_PRIVILEGED)permission only when`mDataPath`is different from[ERROR(BluetoothSocketSettings.DATA_PATH_NO_OFFLOAD/android.bluetooth.BluetoothSocketSettings#DATA_PATH_NO_OFFLOAD BluetoothSocketSettings.DATA_PATH_NO_OFFLOAD)](https://developer.android.com/).  
For apps targeting[Build.VERSION_CODES.S](https://developer.android.com/reference/android/os/Build.VERSION_CODES#S)or or higher, this requires the[Manifest.permission.BLUETOOTH_CONNECT](https://developer.android.com/reference/android/Manifest.permission#BLUETOOTH_CONNECT)permission which can be gained with[Activity.requestPermissions(String[], int)](https://developer.android.com/reference/android/app/Activity#requestPermissions(java.lang.String[],%20int)).

<br />

|                                                                                 Throws                                                                                 ||
|----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------|
| [BluetoothSocketException](https://developer.android.com/reference/android/bluetooth/BluetoothSocketException) | in case of failure, with the corresponding error code. |
| [IOException](https://developer.android.com/reference/java/io/IOException)                                     | for other errors (eg: InputStream read failures etc.). |

### getConnectionType

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

```
public int getConnectionType ()
```

Get the type of the underlying connection.

<br />

|                                                                                                                                                           Returns                                                                                                                                                           ||
|-------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `int` | one of[TYPE_RFCOMM](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#TYPE_RFCOMM),[TYPE_SCO](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#TYPE_SCO)or[TYPE_L2CAP](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#TYPE_L2CAP) <br /> |

### getInputStream

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

```
public InputStream getInputStream ()
```

Get the input stream associated with this socket.

The input stream will be returned even if the socket is not yet connected, but operations on that stream will throw IOException until the associated socket is connected.

<br />

|                                            Returns                                             ||
|----------------------------------------------------------------------------|--------------------|
| [InputStream](https://developer.android.com/reference/java/io/InputStream) | InputStream <br /> |

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

### getMaxReceivePacketSize

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

```
public int getMaxReceivePacketSize ()
```

Get the maximum supported Receive packet size for the underlying transport. Use this to optimize the reads done on the input stream, as any call to read will return a maximum of this amount of bytes - or for some transports a multiple of this value.

<br />

|                                        Returns                                        ||
|-------|--------------------------------------------------------------------------------|
| `int` | the maximum supported Receive packet size for the underlying transport. <br /> |

### getMaxTransmitPacketSize

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

```
public int getMaxTransmitPacketSize ()
```

Get the maximum supported Transmit packet size for the underlying transport. Use this to optimize the writes done to the output socket, to avoid sending half full packets.

<br />

|                                        Returns                                         ||
|-------|---------------------------------------------------------------------------------|
| `int` | the maximum supported Transmit packet size for the underlying transport. <br /> |

### getOutputStream

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

```
public OutputStream getOutputStream ()
```

Get the output stream associated with this socket.

The output stream will be returned even if the socket is not yet connected, but operations on that stream will throw IOException until the associated socket is connected.

<br />

|                                              Returns                                              ||
|------------------------------------------------------------------------------|---------------------|
| [OutputStream](https://developer.android.com/reference/java/io/OutputStream) | OutputStream <br /> |

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

### getRemoteDevice

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

```
public BluetoothDevice getRemoteDevice ()
```

Get the remote device this socket is connecting, or connected, to.

<br />

|                                                      Returns                                                       ||
|----------------------------------------------------------------------------------------------|----------------------|
| [BluetoothDevice](https://developer.android.com/reference/android/bluetooth/BluetoothDevice) | remote device <br /> |

### isConnected

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

```
public boolean isConnected ()
```

Get the connection status of this socket, ie, whether there is an active connection with remote device.

<br />

|                          Returns                           ||
|-----------|-------------------------------------------------|
| `boolean` | true if connected false if not connected <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 /> |