Java.io.ioexception: Bt Socket Closed, Read Return: -1
Course Overview
A continued or connecting Bluetooth socket.
The interface for Bluetooth Sockets is like to that of TCP sockets: Socket
and ServerSocket
. On the server side, utilise a BluetoothServerSocket
to create a listening server socket. When a connection is accepted past the BluetoothServerSocket
, it will render 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 almost common type of Bluetooth socket is RFCOMM, which is the blazon supported by the Android APIs. RFCOMM is a connexion-oriented, streaming ship over Bluetooth. It is also known equally the Serial Port Profile (SPP).
To create a BluetoothSocket
for connecting to a known device, employ BluetoothDevice.createRfcommSocketToServiceRecord()
. Then call connect()
to effort a connection to the remote device. This call volition block until a connexion is established or the connectedness fails.
To create a BluetoothSocket
as a server (or "host"), encounter the BluetoothServerSocket
documentation.
Once the socket is connected, whether initiated every bit a client or accepted every bit a server, open the IO streams by calling getInputStream()
and getOutputStream()
in gild to retrieve InputStream
and OutputStream
objects, respectively, which are automatically continued to the socket.
BluetoothSocket
is thread safe. In particular, close()
will always immediately abort ongoing operations and close the socket.
Note: Requires the BLUETOOTH
permission.
Developer Guides
For more than information about using Bluetooth, read the Bluetooth developer guide.
Summary
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
| Closes the object and release whatsoever system resource it holds. | ||||||||||
| Attempt to connect to a remote device. | ||||||||||
| Get the input stream associated with this socket. | ||||||||||
| Get the output stream associated with this socket. | ||||||||||
| Get the remote device this socket is connecting, or connected, to. | ||||||||||
| Get the connectedness condition of this socket, ie, whether there is an agile connection with remote device. |
[Expand] Inherited Methods | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class coffee.lang.Object
| |||||||||||||||||||||||||||||||||
From interface coffee.io.Closeable
|
Public Methods
public void close ()
Closes the object and release whatever organization resource it holds.
Although but the first phone call has any effect, it is prophylactic to phone call close multiple times on the same object. This is more than lenient than the overridden AutoCloseable.close()
, which may exist called at most once.
public void connect ()
Attempt to connect to a remote device.
This method volition block until a connection is fabricated or the connexion fails. If this method returns without an exception and so this socket is at present connected.
Creating new connections to remote Bluetooth devices should not be attempted while device discovery is in progress. Device discovery is a heavyweight process on the Bluetooth adapter and will significantly ho-hum a device connection. Use cancelDiscovery()
to cancel an ongoing discovery. Discovery is not managed past the Activity, merely is run as a arrangement service, so an application should always telephone call cancelDiscovery()
even if information technology did not directly request a discovery, simply to be certain.
close()
tin be used to abort this telephone call from another thread.
Throws
IOException | on mistake, for instance connexion failure |
---|
public InputStream getInputStream ()
Become the input stream associated with this socket.
The input stream will be returned even if the socket is non yet continued, but operations on that stream will throw IOException until the associated socket is connected.
public OutputStream getOutputStream ()
Go the output stream associated with this socket.
The output stream will exist returned even if the socket is non withal connected, but operations on that stream will throw IOException until the associated socket is connected.
public BluetoothDevice getRemoteDevice ()
Go the remote device this socket is connecting, or connected, to.
public boolean isConnected ()
Become the connexion status of this socket, ie, whether there is an active connexion with remote device.
Returns
- truthful if connected false if non connected
Source: https://stuff.mit.edu/afs/sipb/project/android/docs/reference/android/bluetooth/BluetoothSocket.html