Added in API level 5

BluetoothSocket


class BluetoothSocket : Closeable
kotlin.Any
   ↳ android.bluetooth.BluetoothSocket

A connected or connecting Bluetooth socket.

The interface for Bluetooth Sockets is similar to that of TCP sockets: java.net.Socket and java.net.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.

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 for connecting to a known device, use BluetoothDevice.createRfcommSocketToServiceRecord(). Then call 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 as a server (or "host"), see the BluetoothServerSocket documentation.

Once the socket is connected, whether initiated as a client or accepted as a server, open the IO streams by calling getInputStream and getOutputStream in order to retrieve java.io.InputStream and java.io.OutputStream objects, respectively, which are automatically connected to the socket.

BluetoothSocket is thread safe. In particular, #close will always immediately abort ongoing operations and close the socket.

Summary

Constants
static Int

L2CAP socket

static Int

L2CAP socket on LE transport

static Int

RFCOMM socket

static Int

SCO socket

Public methods
Unit

Closes this stream and releases any system resources associated with it.

Unit

Attempt to connect to a remote device.

Int

Get the type of the underlying connection.

InputStream!

Get the input stream associated with this socket.

Int

Get the maximum supported Receive packet size for the underlying transport.

Int

Get the maximum supported Transmit packet size for the underlying transport.

OutputStream!

Get the output stream associated with this socket.

BluetoothDevice!

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

Boolean

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

String

Returns a string representation of the object.

Protected methods
Unit

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Constants

TYPE_L2CAP

Added in API level 23
static val TYPE_L2CAP: Int

L2CAP socket

Value: 3

TYPE_LE

Added in API level 36
static val TYPE_LE: Int

L2CAP socket on LE transport

Value: 4

TYPE_RFCOMM

Added in API level 23
static val TYPE_RFCOMM: Int

RFCOMM socket

Value: 1

TYPE_SCO

Added in API level 23
static val TYPE_SCO: Int

SCO socket

Value: 2

Public methods

close

Added in API level 5
fun close(): Unit

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

Exceptions
java.lang.Exception if this resource cannot be closed
java.io.IOException if an I/O error occurs

connect

Added in API level 5
fun connect(): Unit

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() 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() even if it did not directly request a discovery, just to be sure.

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

Requires the android.Manifest.permission#BLUETOOTH_PRIVILEGED permission only when mDataPath is different from android.bluetooth.BluetoothSocketSettings#DATA_PATH_NO_OFFLOAD.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).

Exceptions
android.bluetooth.BluetoothSocketException in case of failure, with the corresponding error code.
java.io.IOException for other errors (eg: InputStream read failures etc.).

getConnectionType

Added in API level 23
fun getConnectionType(): Int

Get the type of the underlying connection.

Return
Int one of TYPE_RFCOMM, TYPE_SCO or TYPE_L2CAP

getInputStream

Added in API level 5
fun getInputStream(): InputStream!

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.

Return
InputStream! InputStream

getMaxReceivePacketSize

Added in API level 23
fun getMaxReceivePacketSize(): Int

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.

Return
Int the maximum supported Receive packet size for the underlying transport.

getMaxTransmitPacketSize

Added in API level 23
fun getMaxTransmitPacketSize(): Int

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.

Return
Int the maximum supported Transmit packet size for the underlying transport.

getOutputStream

Added in API level 5
fun getOutputStream(): OutputStream!

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.

Return
OutputStream! OutputStream

getRemoteDevice

Added in API level 5
fun getRemoteDevice(): BluetoothDevice!

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

Return
BluetoothDevice! remote device

isConnected

Added in API level 14
fun isConnected(): Boolean

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

Return
Boolean true if connected false if not connected

toString

Added in API level 5
fun toString(): String

Returns a string representation of the object.

Return
String a string representation of the object.

Protected methods

finalize

Added in API level 5
protected fun finalize(): Unit

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Exceptions
java.lang.Throwable the Exception raised by this method