public final class BluetoothServerSocket extends Object implements Closeable
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.
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
a BluetoothSocket
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
is thread
safe. In particular, close()
will always immediately abort ongoing
operations and close the server socket.
Note:
Requires the android.Manifest.permission#BLUETOOTH
permission.
For more information about using Bluetooth, read the Bluetooth developer guide.
BluetoothSocket
Modifier and Type | Method and Description |
---|---|
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.
|
int |
getChannel()
Returns the channel on which this socket is bound.
|
public BluetoothSocket accept() throws IOException
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.
BluetoothSocket
IOException
- on error, for example this call was aborted, or
timeoutpublic BluetoothSocket accept(int timeout) throws IOException
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.
BluetoothSocket
IOException
- on error, for example this call was aborted, or
timeoutpublic void close() throws IOException
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()
.
close
in interface Closeable
close
in interface AutoCloseable
IOException
public int getChannel()