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