blob: 5fc344a14f99fcfcd243bfb8dc8b262e93c02550 [file] [log] [blame]
Nick Pelly0b6955a2009-05-26 19:13:43 -07001/*
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
Mathew Inwood4dc66d32018-08-01 15:07:20 +010019import android.annotation.UnsupportedAppUsage;
Nick Pelly24bb9b82009-10-02 20:34:18 -070020import android.os.Handler;
zzy3b147b72012-04-03 19:48:32 -070021import android.os.ParcelUuid;
Casper Bonde238e0f92015-04-09 09:24:48 +020022import android.util.Log;
Nick Pelly24bb9b82009-10-02 20:34:18 -070023
Nick Pelly0b6955a2009-05-26 19:13:43 -070024import java.io.Closeable;
25import java.io.IOException;
26
27/**
Nick Pelly45e27042009-08-19 11:00:00 -070028 * A listening Bluetooth socket.
Nick Pelly0b6955a2009-05-26 19:13:43 -070029 *
Nick Pelly45e27042009-08-19 11:00:00 -070030 * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets:
31 * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server
32 * side, use a {@link BluetoothServerSocket} to create a listening server
Scott Main9fab0ae2009-11-03 18:17:59 -080033 * socket. When a connection is accepted by the {@link BluetoothServerSocket},
34 * it will return a new {@link BluetoothSocket} to manage the connection.
Jake Hambyf51eada2010-09-21 13:39:53 -070035 * On the client side, use a single {@link BluetoothSocket} to both initiate
Scott Main9fab0ae2009-11-03 18:17:59 -080036 * an outgoing connection and to manage the connection.
Nick Pelly0b6955a2009-05-26 19:13:43 -070037 *
Scott Main9fab0ae2009-11-03 18:17:59 -080038 * <p>The most common type of Bluetooth socket is RFCOMM, which is the type
39 * supported by the Android APIs. RFCOMM is a connection-oriented, streaming
40 * transport over Bluetooth. It is also known as the Serial Port Profile (SPP).
Nick Pelly0b6955a2009-05-26 19:13:43 -070041 *
Jake Hambyf51eada2010-09-21 13:39:53 -070042 * <p>To create a listening {@link BluetoothServerSocket} that's ready for
Scott Main9fab0ae2009-11-03 18:17:59 -080043 * incoming connections, use
44 * {@link BluetoothAdapter#listenUsingRfcommWithServiceRecord
45 * BluetoothAdapter.listenUsingRfcommWithServiceRecord()}. Then call
46 * {@link #accept()} to listen for incoming connection requests. This call
47 * will block until a connection is established, at which point, it will return
Scott Main6d95fc02009-11-19 17:00:19 -080048 * a {@link BluetoothSocket} to manage the connection. Once the {@link
49 * BluetoothSocket} is acquired, it's a good idea to call {@link #close()} on
50 * the {@link BluetoothServerSocket} when it's no longer needed for accepting
51 * connections. Closing the {@link BluetoothServerSocket} will <em>not</em>
52 * close the returned {@link BluetoothSocket}.
Nick Pelly45e27042009-08-19 11:00:00 -070053 *
Scott Main9fab0ae2009-11-03 18:17:59 -080054 * <p>{@link BluetoothServerSocket} is thread
Nick Pelly45e27042009-08-19 11:00:00 -070055 * safe. In particular, {@link #close} will always immediately abort ongoing
Scott Main9fab0ae2009-11-03 18:17:59 -080056 * operations and close the server socket.
Nick Pellycf440592009-09-08 10:12:06 -070057 *
Scott Main9fab0ae2009-11-03 18:17:59 -080058 * <p class="note"><strong>Note:</strong>
59 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
60 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080061 * <div class="special reference">
62 * <h3>Developer Guides</h3>
63 * <p>For more information about using Bluetooth, read the
Marie Janssen382871b2016-06-20 10:26:31 -070064 * <a href="{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080065 * </div>
66 *
Scott Main9fab0ae2009-11-03 18:17:59 -080067 * {@see BluetoothSocket}
Nick Pelly0b6955a2009-05-26 19:13:43 -070068 */
69public final class BluetoothServerSocket implements Closeable {
Nick Pelly71c3c782009-09-02 11:51:35 -070070
Casper Bonde238e0f92015-04-09 09:24:48 +020071 private static final String TAG = "BluetoothServerSocket";
Stanley Tnge48468d2017-11-22 16:04:40 -080072 private static final boolean DBG = false;
Mathew Inwood4dc66d32018-08-01 15:07:20 +010073 @UnsupportedAppUsage
Nick Pellybd022f42009-08-14 18:33:38 -070074 /*package*/ final BluetoothSocket mSocket;
Nick Pelly24bb9b82009-10-02 20:34:18 -070075 private Handler mHandler;
76 private int mMessage;
Casper Bonde238e0f92015-04-09 09:24:48 +020077 private int mChannel;
Nick Pelly0b6955a2009-05-26 19:13:43 -070078
79 /**
80 * Construct a socket for incoming connections.
Jack Hea355e5e2017-08-22 16:06:54 -070081 *
82 * @param type type of socket
83 * @param auth require the remote device to be authenticated
Nick Pelly6a669fa2009-06-02 15:57:18 -070084 * @param encrypt require the connection to be encrypted
Jack Hea355e5e2017-08-22 16:06:54 -070085 * @param port remote port
86 * @throws IOException On error, for example Bluetooth not available, or insufficient
87 * privileges
Nick Pelly0b6955a2009-05-26 19:13:43 -070088 */
Nick Pellybd022f42009-08-14 18:33:38 -070089 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
Nick Pelly6a669fa2009-06-02 15:57:18 -070090 throws IOException {
Ben Dodson6d8b80d2011-07-08 14:36:42 -070091 mChannel = port;
Nick Pelly16fb88a2009-10-07 07:44:03 +020092 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null);
Casper Bonde23284232015-04-21 13:12:05 +020093 if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
94 mSocket.setExcludeSdp(true);
95 }
96 }
97
98 /**
99 * Construct a socket for incoming connections.
Jack Hea355e5e2017-08-22 16:06:54 -0700100 *
101 * @param type type of socket
102 * @param auth require the remote device to be authenticated
Casper Bonde23284232015-04-21 13:12:05 +0200103 * @param encrypt require the connection to be encrypted
Jack Hea355e5e2017-08-22 16:06:54 -0700104 * @param port remote port
105 * @param mitm enforce man-in-the-middle protection for authentication.
Casper Bonde3b3d1fe2015-05-08 14:32:24 +0200106 * @param min16DigitPin enforce a minimum length of 16 digits for a sec mode 2 connection
Jack Hea355e5e2017-08-22 16:06:54 -0700107 * @throws IOException On error, for example Bluetooth not available, or insufficient
108 * privileges
Casper Bonde23284232015-04-21 13:12:05 +0200109 */
110 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port,
Casper Bonde3b3d1fe2015-05-08 14:32:24 +0200111 boolean mitm, boolean min16DigitPin)
Casper Bonde23284232015-04-21 13:12:05 +0200112 throws IOException {
113 mChannel = port;
Casper Bonde3b3d1fe2015-05-08 14:32:24 +0200114 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null, mitm,
115 min16DigitPin);
Jack Hea355e5e2017-08-22 16:06:54 -0700116 if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
Casper Bonde238e0f92015-04-09 09:24:48 +0200117 mSocket.setExcludeSdp(true);
118 }
Nick Pelly0b6955a2009-05-26 19:13:43 -0700119 }
120
121 /**
zzy3b147b72012-04-03 19:48:32 -0700122 * Construct a socket for incoming connections.
Jack Hea355e5e2017-08-22 16:06:54 -0700123 *
124 * @param type type of socket
125 * @param auth require the remote device to be authenticated
zzy3b147b72012-04-03 19:48:32 -0700126 * @param encrypt require the connection to be encrypted
Jack Hea355e5e2017-08-22 16:06:54 -0700127 * @param uuid uuid
128 * @throws IOException On error, for example Bluetooth not available, or insufficient
129 * privileges
zzy3b147b72012-04-03 19:48:32 -0700130 */
131 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid)
132 throws IOException {
133 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, -1, uuid);
Casper Bonde238e0f92015-04-09 09:24:48 +0200134 // TODO: This is the same as mChannel = -1 - is this intentional?
zzy3b147b72012-04-03 19:48:32 -0700135 mChannel = mSocket.getPort();
136 }
137
138
139 /**
Nick Pelly0b6955a2009-05-26 19:13:43 -0700140 * Block until a connection is established.
Nick Pelly45e27042009-08-19 11:00:00 -0700141 * <p>Returns a connected {@link BluetoothSocket} on successful connection.
142 * <p>Once this call returns, it can be called again to accept subsequent
143 * incoming connections.
144 * <p>{@link #close} can be used to abort this call from another thread.
Jack Hea355e5e2017-08-22 16:06:54 -0700145 *
Nick Pelly45e27042009-08-19 11:00:00 -0700146 * @return a connected {@link BluetoothSocket}
Jack Hea355e5e2017-08-22 16:06:54 -0700147 * @throws IOException on error, for example this call was aborted, or timeout
Nick Pelly0b6955a2009-05-26 19:13:43 -0700148 */
149 public BluetoothSocket accept() throws IOException {
150 return accept(-1);
151 }
152
153 /**
154 * Block until a connection is established, with timeout.
Nick Pelly45e27042009-08-19 11:00:00 -0700155 * <p>Returns a connected {@link BluetoothSocket} on successful connection.
156 * <p>Once this call returns, it can be called again to accept subsequent
157 * incoming connections.
158 * <p>{@link #close} can be used to abort this call from another thread.
Jack Hea355e5e2017-08-22 16:06:54 -0700159 *
Nick Pelly45e27042009-08-19 11:00:00 -0700160 * @return a connected {@link BluetoothSocket}
Jack Hea355e5e2017-08-22 16:06:54 -0700161 * @throws IOException on error, for example this call was aborted, or timeout
Nick Pelly0b6955a2009-05-26 19:13:43 -0700162 */
163 public BluetoothSocket accept(int timeout) throws IOException {
Nick Pelly71c3c782009-09-02 11:51:35 -0700164 return mSocket.accept(timeout);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700165 }
166
167 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700168 * Immediately close this socket, and release all associated resources.
169 * <p>Causes blocked calls on this socket in other threads to immediately
Nick Pelly0b6955a2009-05-26 19:13:43 -0700170 * throw an IOException.
Scott Main6d95fc02009-11-19 17:00:19 -0800171 * <p>Closing the {@link BluetoothServerSocket} will <em>not</em>
172 * close any {@link BluetoothSocket} received from {@link #accept()}.
Nick Pelly0b6955a2009-05-26 19:13:43 -0700173 */
174 public void close() throws IOException {
Stanley Tnge48468d2017-11-22 16:04:40 -0800175 if (DBG) Log.d(TAG, "BluetoothServerSocket:close() called. mChannel=" + mChannel);
Nick Pelly24bb9b82009-10-02 20:34:18 -0700176 synchronized (this) {
177 if (mHandler != null) {
178 mHandler.obtainMessage(mMessage).sendToTarget();
179 }
180 }
Nick Pelly71c3c782009-09-02 11:51:35 -0700181 mSocket.close();
Nick Pelly0b6955a2009-05-26 19:13:43 -0700182 }
Nick Pelly24bb9b82009-10-02 20:34:18 -0700183
Jack Hea355e5e2017-08-22 16:06:54 -0700184 /*package*/
185 synchronized void setCloseHandler(Handler handler, int message) {
Nick Pelly24bb9b82009-10-02 20:34:18 -0700186 mHandler = handler;
187 mMessage = message;
188 }
Jack Hea355e5e2017-08-22 16:06:54 -0700189
Jack He2992cd02017-08-22 21:21:23 -0700190 /*package*/ void setServiceName(String serviceName) {
191 mSocket.setServiceName(serviceName);
zzy3b147b72012-04-03 19:48:32 -0700192 }
Casper Bonde238e0f92015-04-09 09:24:48 +0200193
Ben Dodson6d8b80d2011-07-08 14:36:42 -0700194 /**
195 * Returns the channel on which this socket is bound.
Jack Hea355e5e2017-08-22 16:06:54 -0700196 *
Ben Dodson6d8b80d2011-07-08 14:36:42 -0700197 * @hide
198 */
199 public int getChannel() {
200 return mChannel;
201 }
Casper Bonde238e0f92015-04-09 09:24:48 +0200202
203 /**
Stanley Tnge48468d2017-11-22 16:04:40 -0800204 * Returns the assigned dynamic protocol/service multiplexer (PSM) value for the listening L2CAP
205 * Connection-oriented Channel (CoC) server socket. This server socket must be returned by the
Stanley Tng1bff4ba2018-06-29 14:05:04 -0700206 * {#link BluetoothAdapter.listenUsingL2capChannel()} or {#link
207 * BluetoothAdapter.listenUsingInsecureL2capChannel()}. The returned value is undefined if this
Stanley Tnge48468d2017-11-22 16:04:40 -0800208 * method is called on non-L2CAP server sockets.
209 *
210 * @return the assigned PSM or LE_PSM value depending on transport
Stanley Tnge48468d2017-11-22 16:04:40 -0800211 */
212 public int getPsm() {
213 return mChannel;
214 }
215
216 /**
Casper Bonde238e0f92015-04-09 09:24:48 +0200217 * Sets the channel on which future sockets are bound.
218 * Currently used only when a channel is auto generated.
219 */
220 /*package*/ void setChannel(int newChannel) {
221 /* TODO: From a design/architecture perspective this is wrong.
222 * The bind operation should be conducted through this class
223 * and the resulting port should be kept in mChannel, and
224 * not set from BluetoothAdapter. */
Jack Hea355e5e2017-08-22 16:06:54 -0700225 if (mSocket != null) {
226 if (mSocket.getPort() != newChannel) {
227 Log.w(TAG, "The port set is different that the underlying port. mSocket.getPort(): "
228 + mSocket.getPort() + " requested newChannel: " + newChannel);
Casper Bonde238e0f92015-04-09 09:24:48 +0200229 }
230 }
231 mChannel = newChannel;
232 }
233
234 @Override
235 public String toString() {
236 StringBuilder sb = new StringBuilder();
237 sb.append("ServerSocket: Type: ");
Jack Hea355e5e2017-08-22 16:06:54 -0700238 switch (mSocket.getConnectionType()) {
239 case BluetoothSocket.TYPE_RFCOMM: {
Casper Bonde238e0f92015-04-09 09:24:48 +0200240 sb.append("TYPE_RFCOMM");
241 break;
242 }
Jack Hea355e5e2017-08-22 16:06:54 -0700243 case BluetoothSocket.TYPE_L2CAP: {
Casper Bonde238e0f92015-04-09 09:24:48 +0200244 sb.append("TYPE_L2CAP");
245 break;
246 }
Stanley Tnge48468d2017-11-22 16:04:40 -0800247 case BluetoothSocket.TYPE_L2CAP_LE: {
248 sb.append("TYPE_L2CAP_LE");
249 break;
250 }
Jack Hea355e5e2017-08-22 16:06:54 -0700251 case BluetoothSocket.TYPE_SCO: {
Casper Bonde238e0f92015-04-09 09:24:48 +0200252 sb.append("TYPE_SCO");
253 break;
254 }
255 }
256 sb.append(" Channel: ").append(mChannel);
257 return sb.toString();
258 }
Nick Pelly0b6955a2009-05-26 19:13:43 -0700259}