|
Build 1.0_r1(from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectandroid.bluetooth.RfcommSocket
public class RfcommSocket
The Android Bluetooth API is not finalized, and *will* change. Use at your
own risk.
This class implements an API to the Bluetooth RFCOMM layer. An RFCOMM socket
is similar to a normal socket in that it takes an address and a port number.
The difference is of course that the address is a Bluetooth-device address,
and the port number is an RFCOMM channel. The API allows for the
establishment of listening sockets via methods
bind, listen, and
accept, as well as for the making of
outgoing connections with connect,
connectAsync, and
waitForAsyncConnect.
After constructing a socket, you need to create it and then
destroy it when you are done using it. Both
create and accept return
a FileDescriptor for the actual data.
Alternatively, you may call getInputStream and
getOutputStream to retrieve the respective streams
without going through the FileDescriptor.
| Field Summary | |
|---|---|
protected FileInputStream |
mInputStream
Maintained by getInputStream. |
protected FileOutputStream |
mOutputStream
Maintained by getOutputStream. |
| Constructor Summary | |
|---|---|
RfcommSocket()
Constructor. |
|
| Method Summary | |
|---|---|
FileDescriptor |
accept(RfcommSocket newSock,
int timeoutMs)
Accepts incoming-connection requests for a listening socket bound to an RFCOMM channel. |
boolean |
bind(String device)
Binds a listening socket to the local device, or a non-listening socket to a remote device. |
boolean |
bind(String device,
int port)
Binds a listening socket to the local device, or a non-listening socket to a remote device. |
boolean |
connect(String address,
int port)
Starts a blocking connect to a remote RFCOMM socket. |
boolean |
connectAsync(String address,
int port)
Starts an asynchronous (nonblocking) connect to a remote RFCOMM socket. |
FileDescriptor |
create()
Creates a socket. |
void |
destroy()
Destroys a socket created by create. |
protected void |
finalize()
Called by the GC to clean up the native data that we set up when we construct the object. |
FileDescriptor |
getFileDescriptor()
Returns the FileDescriptor of the socket. |
InputStream |
getInputStream()
Retrieves the input stream from the socket. |
OutputStream |
getOutputStream()
Retrieves the output stream from the socket. |
int |
getPort()
Get the port (rfcomm channel) associated with this socket. |
int |
getRemainingAcceptWaitingTimeMs()
Returns the number of milliseconds left to wait after the last call to accept. |
int |
getRemainingAsyncConnectWaitingTimeMs()
Returns the number of milliseconds left to wait after the last call to waitForAsyncConnect. |
void |
interruptAsyncConnect()
Interrupts an asynchronous connect in progress. |
boolean |
isConnected()
Tells you whether a socket is connected to another socket. |
boolean |
isConnecting()
Tells you whether there is an asynchronous connect in progress. |
boolean |
isInputConnected()
Determines whether input is connected (i.e., whether you can receive data on this socket.) |
boolean |
isListening()
Return true if this socket is listening ( listen(int)
has been called successfully). |
boolean |
isOutputConnected()
Determines whether output is connected (i.e., whether you can send data on this socket.) |
boolean |
listen(int backlog)
Starts listening for incoming connections on this socket, after it has been bound to an address and RFCOMM channel with bind. |
boolean |
shutdown()
Shuts down both directions on a socket. |
boolean |
shutdownInput()
Shuts down the input stream of the socket, but leaves the output stream in its current state. |
boolean |
shutdownOutput()
Shut down the output stream of the socket, but leaves the input stream in its current state. |
int |
waitForAsyncConnect(int timeoutMs)
Blocks for a specified amount of milliseconds while waiting for an asynchronous connect to complete. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected FileInputStream mInputStream
getInputStream.
protected FileOutputStream mOutputStream
getOutputStream.
| Constructor Detail |
|---|
public RfcommSocket()
| Method Detail |
|---|
protected void finalize()
throws Throwable
finalize in class ObjectThrowable - The virtual machine ignores any exceptions which are
thrown during finalization.
public FileDescriptor create()
throws IOException
IOExceptiondestroy()public void destroy()
create. Call this
function when you no longer use the socket in order to release the
underlying OS resources.
create()
public FileDescriptor getFileDescriptor()
throws IOException
FileDescriptor of the socket.
IOException - when the socket has not been created.
public InputStream getInputStream()
throws IOException
create or
accept.
IOException - if you have not called create on the
socket.
public OutputStream getOutputStream()
throws IOException
create or
accept.
IOException - if you have not called create on the
socket.
public boolean connect(String address,
int port)
throws IOException
address - is the Bluetooth address of the remote device.port - is the RFCOMM channel
IOException - if create has not been called.connectAsync(String, int)
public boolean connectAsync(String address,
int port)
throws IOException
waitForAsyncConnect to
block for up to a specified number of milliseconds while waiting for the
asyncronous connect to complete.
address - of remote deviceport - the RFCOMM channel
IOException - is you have not called createwaitForAsyncConnect(int),
getRemainingAsyncConnectWaitingTimeMs(),
connect(String, int)
public void interruptAsyncConnect()
throws IOException
IOException - if you have not called create.connectAsync(String, int)public boolean isConnecting()
connectAsync(String, int)
public int waitForAsyncConnect(int timeoutMs)
throws IOException
getRemainingAsyncConnectWaitingTimeMs
to retrieve the remaining time.
timeoutMs - the time to block while waiting for the async connect to
complete.
IOExceptiongetRemainingAsyncConnectWaitingTimeMs(),
connectAsync(String, int)public int getRemainingAsyncConnectWaitingTimeMs()
waitForAsyncConnect.
It is possible that waitForAsyncConnect() waits for less than the time
specified by the user, and still returns zero (i.e., async connect is
still in progress.) For this reason, if the return value is zero, you
need to call this method to retrieve the remaining time before you call
waitForAsyncConnect again.
waitForAsyncConnect(int),
connectAsync(String, int)
public boolean shutdown()
throws IOException
destroy and then
create the socket again.
IOException - is you have not caled create.shutdownInput(),
shutdownOutput()
public boolean shutdownInput()
throws IOException
IOException - is you have not called createshutdown(),
shutdownOutput()
public boolean shutdownOutput()
throws IOException
IOException - is you have not called createshutdown(),
shutdownInput()public boolean isConnected()
isInputConnected(),
isOutputConnected()public boolean isInputConnected()
isConnected(),
isOutputConnected()public boolean isOutputConnected()
isConnected(),
isInputConnected()
public boolean bind(String device)
throws IOException
device - Bluetooth address of device to bind to (currently ignored).
IOException - if you have not called createlisten(int),
accept(RfcommSocket,int)
public boolean bind(String device,
int port)
throws IOException
device - Bluetooth address of device to bind to (currently ignored).port - RFCOMM channel to bind socket to.
IOException - if you have not called createlisten(int),
accept(RfcommSocket,int)
public boolean listen(int backlog)
throws IOException
bind.
backlog - the number of pending incoming connections to queue for
accept.
IOException - if you have not called create or if the
socket has not been bound to a device and RFCOMM channel.
public FileDescriptor accept(RfcommSocket newSock,
int timeoutMs)
throws IOException
getRemainingAcceptWaitingTimeMs
in order to see how much time is left to wait, before you call this
method again.
newSock - is set to the new socket that is created as a result of a
successful accept.timeoutMs - time (in milliseconds) to block while waiting to an
incoming-connection request. A negative value is an infinite
wait.
IOException - if the socket has not been createed, is
not bound, or is not a listening socket.bind(String, int),
listen(int),
getRemainingAcceptWaitingTimeMs()public int getRemainingAcceptWaitingTimeMs()
accept.
Since accept() may return null (i.e., no incoming connection) before the
user-specified timeout expires, you need to call this method in order to
see how much time is left to wait, and wait for that amount of time
before you call accept again.
public int getPort()
throws IOException
bind(String, int), connect(String, int)
or connectAsync(String, int). This can be checked
with isListening() and isConnected().
IOExceptionpublic boolean isListening()
listen(int)
has been called successfully).
|
Build 1.0_r1(from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||