BluetoothServerSocket.java revision 71c3c7806acb2b2b7b8441817c26a2101d447bbe
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.bluetooth;
18
19import java.io.Closeable;
20import java.io.IOException;
21
22/**
23 * A listening Bluetooth socket.
24 *
25 * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets:
26 * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server
27 * side, use a {@link BluetoothServerSocket} to create a listening server
28 * socket. It will return a new, connected {@link BluetoothSocket} on an
29 * accepted connection. On the client side, use the same
30 * {@link BluetoothSocket} object to both intiate the outgoing connection,
31 * and to manage the connected socket.
32 *
33 * <p>The most common type of Bluetooth Socket is RFCOMM. RFCOMM is a
34 * connection orientated, streaming transport over Bluetooth. It is also known
35 * as the Serial Port Profile (SPP).
36 *
37 * <p>Use {@link BluetoothDevice#createRfcommSocket} to create a new {@link
38 * BluetoothSocket} ready for an outgoing connection to a remote
39 * {@link BluetoothDevice}.
40 *
41 * <p>Use {@link BluetoothAdapter#listenUsingRfcommOn} to create a listening
42 * {@link BluetoothServerSocket} ready for incoming connections to the local
43 * {@link BluetoothAdapter}.
44 *
45 * <p>{@link BluetoothSocket} and {@link BluetoothServerSocket} are thread
46 * safe. In particular, {@link #close} will always immediately abort ongoing
47 * operations and close the socket.
48 */
49public final class BluetoothServerSocket implements Closeable {
50
51    /*package*/ final BluetoothSocket mSocket;
52
53    /**
54     * Construct a socket for incoming connections.
55     * @param type    type of socket
56     * @param auth    require the remote device to be authenticated
57     * @param encrypt require the connection to be encrypted
58     * @param port    remote port
59     * @throws IOException On error, for example Bluetooth not available, or
60     *                     insufficient priveleges
61     */
62    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
63            throws IOException {
64        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port);
65    }
66
67    /**
68     * Block until a connection is established.
69     * <p>Returns a connected {@link BluetoothSocket} on successful connection.
70     * <p>Once this call returns, it can be called again to accept subsequent
71     * incoming connections.
72     * <p>{@link #close} can be used to abort this call from another thread.
73     * @return a connected {@link BluetoothSocket}
74     * @throws IOException on error, for example this call was aborted, or
75     *                     timeout
76     */
77    public BluetoothSocket accept() throws IOException {
78        return accept(-1);
79    }
80
81    /**
82     * Block until a connection is established, with timeout.
83     * <p>Returns a connected {@link BluetoothSocket} on successful connection.
84     * <p>Once this call returns, it can be called again to accept subsequent
85     * incoming connections.
86     * <p>{@link #close} can be used to abort this call from another thread.
87     * @return a connected {@link BluetoothSocket}
88     * @throws IOException on error, for example this call was aborted, or
89     *                     timeout
90     */
91    public BluetoothSocket accept(int timeout) throws IOException {
92        return mSocket.accept(timeout);
93    }
94
95    /**
96     * Immediately close this socket, and release all associated resources.
97     * <p>Causes blocked calls on this socket in other threads to immediately
98     * throw an IOException.
99     */
100    public void close() throws IOException {
101        mSocket.close();
102    }
103}
104