Javascript required
Skip to content Skip to sidebar Skip to footer

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
void close()

Closes the object and release whatsoever system resource it holds.

void connect()

Attempt to connect to a remote device.

InputStream getInputStream()

Get the input stream associated with this socket.

OutputStream getOutputStream()

Get the output stream associated with this socket.

BluetoothDevice getRemoteDevice()

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

boolean isConnected()

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
Object clone()

Creates and returns a copy of this Object.

boolean equals(Object o)

Compares this instance with the specified object and indicates if they are equal.

void finalize()

Invoked when the garbage collector has detected that this instance is no longer reachable.

final Class<?> getClass()

Returns the unique instance of Class that represents this object's class.

int hashCode()

Returns an integer hash code for this object.

terminal void notify()

Causes a thread which is waiting on this object's monitor (past means of calling 1 of the wait() methods) to be woken up.

final void notifyAll()

Causes all threads which are waiting on this object's monitor (by means of calling one of the await() methods) to be woken up.

Cord toString()

Returns a cord containing a concise, man-readable description of this object.

final void look()

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.

final void wait(long millis, int nanos)

Causes the calling thread to look until some other thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

final void wait(long millis)

Causes the calling thread to await until some other thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

From interface coffee.io.Closeable
abstract void close()

Closes the object and release any arrangement resources information technology holds.

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

hanranhisfultal.blogspot.com

Source: https://stuff.mit.edu/afs/sipb/project/android/docs/reference/android/bluetooth/BluetoothSocket.html