blob: 5c1bcaf313197a1d2a6c47d5087fc90e398ab411 [file] [log] [blame]
Nick Pelly53f441b2009-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
Artur Satayevd8fe38c2019-12-10 17:47:52 +000019import android.compat.annotation.UnsupportedAppUsage;
Nick Pellyee1402d2009-10-02 20:34:18 -070020import android.os.Handler;
zzyfab62db2012-04-03 19:48:32 -070021import android.os.ParcelUuid;
Casper Bondec0a7c932015-04-09 09:24:48 +020022import android.util.Log;
Nick Pellyee1402d2009-10-02 20:34:18 -070023
Nick Pelly53f441b2009-05-26 19:13:43 -070024import java.io.Closeable;
25import java.io.IOException;
26
27/**
Nick Pelly753da532009-08-19 11:00:00 -070028 * A listening Bluetooth socket.
Nick Pelly53f441b2009-05-26 19:13:43 -070029 *
Nick Pelly753da532009-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 Mainbeef8092009-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 Hamby04c71382010-09-21 13:39:53 -070035 * On the client side, use a single {@link BluetoothSocket} to both initiate
Scott Mainbeef8092009-11-03 18:17:59 -080036 * an outgoing connection and to manage the connection.
Nick Pelly53f441b2009-05-26 19:13:43 -070037 *
Stanley Tng3b285452019-04-19 14:27:09 -070038 * <p>For Bluetooth BR/EDR, the most common type of socket is RFCOMM, which is the type supported by
39 * the Android APIs. RFCOMM is a connection-oriented, streaming transport over Bluetooth BR/EDR. It
40 * is also known as the Serial Port Profile (SPP). To create a listening
41 * {@link BluetoothServerSocket} that's ready for incoming Bluetooth BR/EDR connections, use {@link
42 * BluetoothAdapter#listenUsingRfcommWithServiceRecord
43 * BluetoothAdapter.listenUsingRfcommWithServiceRecord()}.
Nick Pelly53f441b2009-05-26 19:13:43 -070044 *
Stanley Tng3b285452019-04-19 14:27:09 -070045 * <p>For Bluetooth LE, the socket uses LE Connection-oriented Channel (CoC). LE CoC is a
46 * connection-oriented, streaming transport over Bluetooth LE and has a credit-based flow control.
47 * Correspondingly, use {@link BluetoothAdapter#listenUsingL2capChannel
48 * BluetoothAdapter.listenUsingL2capChannel()} to create a listening {@link BluetoothServerSocket}
49 * that's ready for incoming Bluetooth LE CoC connections. For LE CoC, you can use {@link #getPsm()}
50 * to get the protocol/service multiplexer (PSM) value that the peer needs to use to connect to your
51 * socket.
52 *
53 * <p> After the listening {@link BluetoothServerSocket} is created, call {@link #accept()} to
54 * listen for incoming connection requests. This call will block until a connection is established,
55 * at which point, it will return a {@link BluetoothSocket} to manage the connection. Once the
56 * {@link BluetoothSocket} is acquired, it's a good idea to call {@link #close()} on the {@link
57 * BluetoothServerSocket} when it's no longer needed for accepting
58 * connections. Closing the {@link BluetoothServerSocket} will <em>not</em> close the returned
59 * {@link BluetoothSocket}.
Nick Pelly753da532009-08-19 11:00:00 -070060 *
Scott Mainbeef8092009-11-03 18:17:59 -080061 * <p>{@link BluetoothServerSocket} is thread
Nick Pelly753da532009-08-19 11:00:00 -070062 * safe. In particular, {@link #close} will always immediately abort ongoing
Scott Mainbeef8092009-11-03 18:17:59 -080063 * operations and close the server socket.
Nick Pellyed6f2ce2009-09-08 10:12:06 -070064 *
Scott Mainbeef8092009-11-03 18:17:59 -080065 * <p class="note"><strong>Note:</strong>
66 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
67 *
Joe Fernandezda4e2ab2011-12-20 10:38:34 -080068 * <div class="special reference">
69 * <h3>Developer Guides</h3>
70 * <p>For more information about using Bluetooth, read the
Marie Janssenf242df22016-06-20 10:26:31 -070071 * <a href="{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer guide.</p>
Joe Fernandezda4e2ab2011-12-20 10:38:34 -080072 * </div>
73 *
Scott Mainbeef8092009-11-03 18:17:59 -080074 * {@see BluetoothSocket}
Nick Pelly53f441b2009-05-26 19:13:43 -070075 */
76public final class BluetoothServerSocket implements Closeable {
Nick Pelly4cd2cd92009-09-02 11:51:35 -070077
Casper Bondec0a7c932015-04-09 09:24:48 +020078 private static final String TAG = "BluetoothServerSocket";
Stanley Tngd67d5e42017-11-22 16:04:40 -080079 private static final boolean DBG = false;
Andrei Onea147146f2019-06-17 11:26:14 +010080 @UnsupportedAppUsage(publicAlternatives = "Use public {@link BluetoothServerSocket} API "
81 + "instead.")
Nick Pelly2d664882009-08-14 18:33:38 -070082 /*package*/ final BluetoothSocket mSocket;
Nick Pellyee1402d2009-10-02 20:34:18 -070083 private Handler mHandler;
84 private int mMessage;
Casper Bondec0a7c932015-04-09 09:24:48 +020085 private int mChannel;
Nick Pelly53f441b2009-05-26 19:13:43 -070086
87 /**
88 * Construct a socket for incoming connections.
Jack He910201b2017-08-22 16:06:54 -070089 *
90 * @param type type of socket
91 * @param auth require the remote device to be authenticated
Nick Pellycb32d7c2009-06-02 15:57:18 -070092 * @param encrypt require the connection to be encrypted
Jack He910201b2017-08-22 16:06:54 -070093 * @param port remote port
94 * @throws IOException On error, for example Bluetooth not available, or insufficient
95 * privileges
Nick Pelly53f441b2009-05-26 19:13:43 -070096 */
Nick Pelly2d664882009-08-14 18:33:38 -070097 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
Nick Pellycb32d7c2009-06-02 15:57:18 -070098 throws IOException {
Ben Dodson48d0cca2011-07-08 14:36:42 -070099 mChannel = port;
Nick Pelly07b84cb2009-10-07 07:44:03 +0200100 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null);
Casper Bonde60d77c22015-04-21 13:12:05 +0200101 if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
102 mSocket.setExcludeSdp(true);
103 }
104 }
105
106 /**
107 * Construct a socket for incoming connections.
Jack He910201b2017-08-22 16:06:54 -0700108 *
109 * @param type type of socket
110 * @param auth require the remote device to be authenticated
Casper Bonde60d77c22015-04-21 13:12:05 +0200111 * @param encrypt require the connection to be encrypted
Jack He910201b2017-08-22 16:06:54 -0700112 * @param port remote port
Jeff Sharkeyc5386af2020-09-11 14:57:21 -0600113 * @param mitm enforce person-in-the-middle protection for authentication.
Casper Bonde91276012015-05-08 14:32:24 +0200114 * @param min16DigitPin enforce a minimum length of 16 digits for a sec mode 2 connection
Jack He910201b2017-08-22 16:06:54 -0700115 * @throws IOException On error, for example Bluetooth not available, or insufficient
116 * privileges
Casper Bonde60d77c22015-04-21 13:12:05 +0200117 */
118 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port,
Casper Bonde91276012015-05-08 14:32:24 +0200119 boolean mitm, boolean min16DigitPin)
Casper Bonde60d77c22015-04-21 13:12:05 +0200120 throws IOException {
121 mChannel = port;
Casper Bonde91276012015-05-08 14:32:24 +0200122 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null, mitm,
123 min16DigitPin);
Jack He910201b2017-08-22 16:06:54 -0700124 if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
Casper Bondec0a7c932015-04-09 09:24:48 +0200125 mSocket.setExcludeSdp(true);
126 }
Nick Pelly53f441b2009-05-26 19:13:43 -0700127 }
128
129 /**
zzyfab62db2012-04-03 19:48:32 -0700130 * Construct a socket for incoming connections.
Jack He910201b2017-08-22 16:06:54 -0700131 *
132 * @param type type of socket
133 * @param auth require the remote device to be authenticated
zzyfab62db2012-04-03 19:48:32 -0700134 * @param encrypt require the connection to be encrypted
Jack He910201b2017-08-22 16:06:54 -0700135 * @param uuid uuid
136 * @throws IOException On error, for example Bluetooth not available, or insufficient
137 * privileges
zzyfab62db2012-04-03 19:48:32 -0700138 */
139 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid)
140 throws IOException {
141 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, -1, uuid);
Casper Bondec0a7c932015-04-09 09:24:48 +0200142 // TODO: This is the same as mChannel = -1 - is this intentional?
zzyfab62db2012-04-03 19:48:32 -0700143 mChannel = mSocket.getPort();
144 }
145
146
147 /**
Nick Pelly53f441b2009-05-26 19:13:43 -0700148 * Block until a connection is established.
Nick Pelly753da532009-08-19 11:00:00 -0700149 * <p>Returns a connected {@link BluetoothSocket} on successful connection.
150 * <p>Once this call returns, it can be called again to accept subsequent
151 * incoming connections.
152 * <p>{@link #close} can be used to abort this call from another thread.
Jack He910201b2017-08-22 16:06:54 -0700153 *
Nick Pelly753da532009-08-19 11:00:00 -0700154 * @return a connected {@link BluetoothSocket}
Jack He910201b2017-08-22 16:06:54 -0700155 * @throws IOException on error, for example this call was aborted, or timeout
Nick Pelly53f441b2009-05-26 19:13:43 -0700156 */
157 public BluetoothSocket accept() throws IOException {
158 return accept(-1);
159 }
160
161 /**
162 * Block until a connection is established, with timeout.
Nick Pelly753da532009-08-19 11:00:00 -0700163 * <p>Returns a connected {@link BluetoothSocket} on successful connection.
164 * <p>Once this call returns, it can be called again to accept subsequent
165 * incoming connections.
166 * <p>{@link #close} can be used to abort this call from another thread.
Jack He910201b2017-08-22 16:06:54 -0700167 *
Nick Pelly753da532009-08-19 11:00:00 -0700168 * @return a connected {@link BluetoothSocket}
Jack He910201b2017-08-22 16:06:54 -0700169 * @throws IOException on error, for example this call was aborted, or timeout
Nick Pelly53f441b2009-05-26 19:13:43 -0700170 */
171 public BluetoothSocket accept(int timeout) throws IOException {
Nick Pelly4cd2cd92009-09-02 11:51:35 -0700172 return mSocket.accept(timeout);
Nick Pelly53f441b2009-05-26 19:13:43 -0700173 }
174
175 /**
Nick Pelly753da532009-08-19 11:00:00 -0700176 * Immediately close this socket, and release all associated resources.
177 * <p>Causes blocked calls on this socket in other threads to immediately
Nick Pelly53f441b2009-05-26 19:13:43 -0700178 * throw an IOException.
Scott Maina7848ce2009-11-19 17:00:19 -0800179 * <p>Closing the {@link BluetoothServerSocket} will <em>not</em>
180 * close any {@link BluetoothSocket} received from {@link #accept()}.
Nick Pelly53f441b2009-05-26 19:13:43 -0700181 */
182 public void close() throws IOException {
Stanley Tngd67d5e42017-11-22 16:04:40 -0800183 if (DBG) Log.d(TAG, "BluetoothServerSocket:close() called. mChannel=" + mChannel);
Nick Pellyee1402d2009-10-02 20:34:18 -0700184 synchronized (this) {
185 if (mHandler != null) {
186 mHandler.obtainMessage(mMessage).sendToTarget();
187 }
188 }
Nick Pelly4cd2cd92009-09-02 11:51:35 -0700189 mSocket.close();
Nick Pelly53f441b2009-05-26 19:13:43 -0700190 }
Nick Pellyee1402d2009-10-02 20:34:18 -0700191
Jack He910201b2017-08-22 16:06:54 -0700192 /*package*/
193 synchronized void setCloseHandler(Handler handler, int message) {
Nick Pellyee1402d2009-10-02 20:34:18 -0700194 mHandler = handler;
195 mMessage = message;
196 }
Jack He910201b2017-08-22 16:06:54 -0700197
Jack He9e045d22017-08-22 21:21:23 -0700198 /*package*/ void setServiceName(String serviceName) {
199 mSocket.setServiceName(serviceName);
zzyfab62db2012-04-03 19:48:32 -0700200 }
Casper Bondec0a7c932015-04-09 09:24:48 +0200201
Ben Dodson48d0cca2011-07-08 14:36:42 -0700202 /**
203 * Returns the channel on which this socket is bound.
Jack He910201b2017-08-22 16:06:54 -0700204 *
Ben Dodson48d0cca2011-07-08 14:36:42 -0700205 * @hide
206 */
207 public int getChannel() {
208 return mChannel;
209 }
Casper Bondec0a7c932015-04-09 09:24:48 +0200210
211 /**
Stanley Tngd67d5e42017-11-22 16:04:40 -0800212 * Returns the assigned dynamic protocol/service multiplexer (PSM) value for the listening L2CAP
213 * Connection-oriented Channel (CoC) server socket. This server socket must be returned by the
Xin Li10802fb2019-02-13 22:36:25 -0800214 * {@link BluetoothAdapter#listenUsingL2capChannel()} or {@link
215 * BluetoothAdapter#listenUsingInsecureL2capChannel()}. The returned value is undefined if this
Stanley Tngd67d5e42017-11-22 16:04:40 -0800216 * method is called on non-L2CAP server sockets.
217 *
218 * @return the assigned PSM or LE_PSM value depending on transport
Stanley Tngd67d5e42017-11-22 16:04:40 -0800219 */
220 public int getPsm() {
221 return mChannel;
222 }
223
224 /**
Casper Bondec0a7c932015-04-09 09:24:48 +0200225 * Sets the channel on which future sockets are bound.
226 * Currently used only when a channel is auto generated.
227 */
228 /*package*/ void setChannel(int newChannel) {
229 /* TODO: From a design/architecture perspective this is wrong.
230 * The bind operation should be conducted through this class
231 * and the resulting port should be kept in mChannel, and
232 * not set from BluetoothAdapter. */
Jack He910201b2017-08-22 16:06:54 -0700233 if (mSocket != null) {
234 if (mSocket.getPort() != newChannel) {
235 Log.w(TAG, "The port set is different that the underlying port. mSocket.getPort(): "
236 + mSocket.getPort() + " requested newChannel: " + newChannel);
Casper Bondec0a7c932015-04-09 09:24:48 +0200237 }
238 }
239 mChannel = newChannel;
240 }
241
242 @Override
243 public String toString() {
244 StringBuilder sb = new StringBuilder();
245 sb.append("ServerSocket: Type: ");
Jack He910201b2017-08-22 16:06:54 -0700246 switch (mSocket.getConnectionType()) {
247 case BluetoothSocket.TYPE_RFCOMM: {
Casper Bondec0a7c932015-04-09 09:24:48 +0200248 sb.append("TYPE_RFCOMM");
249 break;
250 }
Jack He910201b2017-08-22 16:06:54 -0700251 case BluetoothSocket.TYPE_L2CAP: {
Casper Bondec0a7c932015-04-09 09:24:48 +0200252 sb.append("TYPE_L2CAP");
253 break;
254 }
Stanley Tngd67d5e42017-11-22 16:04:40 -0800255 case BluetoothSocket.TYPE_L2CAP_LE: {
256 sb.append("TYPE_L2CAP_LE");
257 break;
258 }
Jack He910201b2017-08-22 16:06:54 -0700259 case BluetoothSocket.TYPE_SCO: {
Casper Bondec0a7c932015-04-09 09:24:48 +0200260 sb.append("TYPE_SCO");
261 break;
262 }
263 }
264 sb.append(" Channel: ").append(mChannel);
265 return sb.toString();
266 }
Nick Pelly53f441b2009-05-26 19:13:43 -0700267}