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 android.os.Handler;
20import android.os.ParcelUuid;
21
22import java.io.Closeable;
23import java.io.IOException;
24
25/**
26 * A listening Bluetooth socket.
27 *
28 * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets:
29 * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server
30 * side, use a {@link BluetoothServerSocket} to create a listening server
31 * socket. When a connection is accepted by the {@link BluetoothServerSocket},
32 * it will return a new {@link BluetoothSocket} to manage the connection.
33 * On the client side, use a single {@link BluetoothSocket} to both initiate
34 * an outgoing connection and to manage the connection.
35 *
36 * <p>The most common type of Bluetooth socket is RFCOMM, which is the type
37 * supported by the Android APIs. RFCOMM is a connection-oriented, streaming
38 * transport over Bluetooth. It is also known as the Serial Port Profile (SPP).
39 *
40 * <p>To create a listening {@link BluetoothServerSocket} that's ready for
41 * incoming connections, use
42 * {@link BluetoothAdapter#listenUsingRfcommWithServiceRecord
43 * BluetoothAdapter.listenUsingRfcommWithServiceRecord()}. Then call
44 * {@link #accept()} to listen for incoming connection requests. This call
45 * will block until a connection is established, at which point, it will return
46 * a {@link BluetoothSocket} to manage the connection. Once the {@link
47 * BluetoothSocket} is acquired, it's a good idea to call {@link #close()} on
48 * the {@link BluetoothServerSocket} when it's no longer needed for accepting
49 * connections. Closing the {@link BluetoothServerSocket} will <em>not</em>
50 * close the returned {@link BluetoothSocket}.
51 *
52 * <p>{@link BluetoothServerSocket} is thread
53 * safe. In particular, {@link #close} will always immediately abort ongoing
54 * operations and close the server socket.
55 *
56 * <p class="note"><strong>Note:</strong>
57 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
58 *
59 * <div class="special reference">
60 * <h3>Developer Guides</h3>
61 * <p>For more information about using Bluetooth, read the
62 * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p>
63 * </div>
64 *
65 * {@see BluetoothSocket}
66 */
67public final class BluetoothServerSocket implements Closeable {
68
69    /*package*/ final BluetoothSocket mSocket;
70    private Handler mHandler;
71    private int mMessage;
72    private final int mChannel;
73
74    /**
75     * Construct a socket for incoming connections.
76     * @param type    type of socket
77     * @param auth    require the remote device to be authenticated
78     * @param encrypt require the connection to be encrypted
79     * @param port    remote port
80     * @throws IOException On error, for example Bluetooth not available, or
81     *                     insufficient privileges
82     */
83    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
84            throws IOException {
85        mChannel = port;
86        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null);
87    }
88
89    /**
90     * Construct a socket for incoming connections.
91     * @param type    type of socket
92     * @param auth    require the remote device to be authenticated
93     * @param encrypt require the connection to be encrypted
94     * @param uuid    uuid
95     * @throws IOException On error, for example Bluetooth not available, or
96     *                     insufficient privileges
97     */
98    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid)
99            throws IOException {
100        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, -1, uuid);
101        mChannel = mSocket.getPort();
102    }
103
104
105    /**
106     * Block until a connection is established.
107     * <p>Returns a connected {@link BluetoothSocket} on successful connection.
108     * <p>Once this call returns, it can be called again to accept subsequent
109     * incoming connections.
110     * <p>{@link #close} can be used to abort this call from another thread.
111     * @return a connected {@link BluetoothSocket}
112     * @throws IOException on error, for example this call was aborted, or
113     *                     timeout
114     */
115    public BluetoothSocket accept() throws IOException {
116        return accept(-1);
117    }
118
119    /**
120     * Block until a connection is established, with timeout.
121     * <p>Returns a connected {@link BluetoothSocket} on successful connection.
122     * <p>Once this call returns, it can be called again to accept subsequent
123     * incoming connections.
124     * <p>{@link #close} can be used to abort this call from another thread.
125     * @return a connected {@link BluetoothSocket}
126     * @throws IOException on error, for example this call was aborted, or
127     *                     timeout
128     */
129    public BluetoothSocket accept(int timeout) throws IOException {
130        return mSocket.accept(timeout);
131    }
132
133    /**
134     * Immediately close this socket, and release all associated resources.
135     * <p>Causes blocked calls on this socket in other threads to immediately
136     * throw an IOException.
137     * <p>Closing the {@link BluetoothServerSocket} will <em>not</em>
138     * close any {@link BluetoothSocket} received from {@link #accept()}.
139     */
140    public void close() throws IOException {
141        synchronized (this) {
142            if (mHandler != null) {
143                mHandler.obtainMessage(mMessage).sendToTarget();
144            }
145        }
146        mSocket.close();
147    }
148
149    /*package*/ synchronized void setCloseHandler(Handler handler, int message) {
150        mHandler = handler;
151        mMessage = message;
152    }
153    /*package*/ void setServiceName(String ServiceName) {
154        mSocket.setServiceName(ServiceName);
155    }
156    /**
157     * Returns the channel on which this socket is bound.
158     * @hide
159     */
160    public int getChannel() {
161        return mChannel;
162    }
163}
164