• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.bluetooth;
18 
19 import android.os.Handler;
20 
21 import java.io.Closeable;
22 import 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  * <div class="special reference">
59  * <h3>Developer Guides</h3>
60  * <p>For more information about using Bluetooth, read the
61  * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p>
62  * </div>
63  *
64  * {@see BluetoothSocket}
65  */
66 public final class BluetoothServerSocket implements Closeable {
67 
68     /*package*/ final BluetoothSocket mSocket;
69     private Handler mHandler;
70     private int mMessage;
71     private final int mChannel;
72 
73     /**
74      * Construct a socket for incoming connections.
75      * @param type    type of socket
76      * @param auth    require the remote device to be authenticated
77      * @param encrypt require the connection to be encrypted
78      * @param port    remote port
79      * @throws IOException On error, for example Bluetooth not available, or
80      *                     insufficient privileges
81      */
BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)82     /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
83             throws IOException {
84         mChannel = port;
85         mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null);
86     }
87 
88     /**
89      * Block until a connection is established.
90      * <p>Returns a connected {@link BluetoothSocket} on successful connection.
91      * <p>Once this call returns, it can be called again to accept subsequent
92      * incoming connections.
93      * <p>{@link #close} can be used to abort this call from another thread.
94      * @return a connected {@link BluetoothSocket}
95      * @throws IOException on error, for example this call was aborted, or
96      *                     timeout
97      */
accept()98     public BluetoothSocket accept() throws IOException {
99         return accept(-1);
100     }
101 
102     /**
103      * Block until a connection is established, with timeout.
104      * <p>Returns a connected {@link BluetoothSocket} on successful connection.
105      * <p>Once this call returns, it can be called again to accept subsequent
106      * incoming connections.
107      * <p>{@link #close} can be used to abort this call from another thread.
108      * @return a connected {@link BluetoothSocket}
109      * @throws IOException on error, for example this call was aborted, or
110      *                     timeout
111      */
accept(int timeout)112     public BluetoothSocket accept(int timeout) throws IOException {
113         return mSocket.accept(timeout);
114     }
115 
116     /**
117      * Immediately close this socket, and release all associated resources.
118      * <p>Causes blocked calls on this socket in other threads to immediately
119      * throw an IOException.
120      * <p>Closing the {@link BluetoothServerSocket} will <em>not</em>
121      * close any {@link BluetoothSocket} received from {@link #accept()}.
122      */
close()123     public void close() throws IOException {
124         synchronized (this) {
125             if (mHandler != null) {
126                 mHandler.obtainMessage(mMessage).sendToTarget();
127             }
128         }
129         mSocket.close();
130     }
131 
setCloseHandler(Handler handler, int message)132     /*package*/ synchronized void setCloseHandler(Handler handler, int message) {
133         mHandler = handler;
134         mMessage = message;
135     }
136 
137     /**
138      * Returns the channel on which this socket is bound.
139      * @hide
140      */
getChannel()141     public int getChannel() {
142         return mChannel;
143     }
144 }
145