| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 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 | |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 19 | import static android.bluetooth.BluetoothUtils.getSyncTimeout; |
| 20 | |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 21 | import android.annotation.SuppressLint; |
| Artur Satayev | d8fe38c | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 24 | import android.os.ParcelUuid; |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 25 | import android.os.RemoteException; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 26 | import android.util.Log; |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 27 | |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 28 | import com.android.modules.utils.SynchronousResultReceiver; |
| 29 | |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 30 | import java.io.Closeable; |
| 31 | import java.io.IOException; |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 32 | import java.util.concurrent.TimeoutException; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 35 | * A listening Bluetooth socket. |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 36 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 37 | * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets: {@link java.net.Socket} |
| 38 | * and {@link java.net.ServerSocket}. On the server side, use a {@link BluetoothServerSocket} to |
| 39 | * create a listening server socket. When a connection is accepted by the {@link |
| 40 | * BluetoothServerSocket}, it will return a new {@link BluetoothSocket} to manage the connection. On |
| 41 | * the client side, use a single {@link BluetoothSocket} to both initiate an outgoing connection and |
| 42 | * to manage the connection. |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 43 | * |
| Stanley Tng | 3b28545 | 2019-04-19 14:27:09 -0700 | [diff] [blame] | 44 | * <p>For Bluetooth BR/EDR, the most common type of socket is RFCOMM, which is the type supported by |
| 45 | * the Android APIs. RFCOMM is a connection-oriented, streaming transport over Bluetooth BR/EDR. It |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 46 | * is also known as the Serial Port Profile (SPP). To create a listening {@link |
| 47 | * BluetoothServerSocket} that's ready for incoming Bluetooth BR/EDR connections, use {@link |
| Stanley Tng | 3b28545 | 2019-04-19 14:27:09 -0700 | [diff] [blame] | 48 | * BluetoothAdapter#listenUsingRfcommWithServiceRecord |
| 49 | * BluetoothAdapter.listenUsingRfcommWithServiceRecord()}. |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 50 | * |
| Stanley Tng | 3b28545 | 2019-04-19 14:27:09 -0700 | [diff] [blame] | 51 | * <p>For Bluetooth LE, the socket uses LE Connection-oriented Channel (CoC). LE CoC is a |
| 52 | * connection-oriented, streaming transport over Bluetooth LE and has a credit-based flow control. |
| 53 | * Correspondingly, use {@link BluetoothAdapter#listenUsingL2capChannel |
| 54 | * BluetoothAdapter.listenUsingL2capChannel()} to create a listening {@link BluetoothServerSocket} |
| 55 | * that's ready for incoming Bluetooth LE CoC connections. For LE CoC, you can use {@link #getPsm()} |
| 56 | * to get the protocol/service multiplexer (PSM) value that the peer needs to use to connect to your |
| 57 | * socket. |
| 58 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 59 | * <p>After the listening {@link BluetoothServerSocket} is created, call {@link #accept()} to listen |
| 60 | * for incoming connection requests. This call will block until a connection is established, at |
| 61 | * which point, it will return a {@link BluetoothSocket} to manage the connection. Once the {@link |
| 62 | * BluetoothSocket} is acquired, it's a good idea to call {@link #close()} on the {@link |
| 63 | * BluetoothServerSocket} when it's no longer needed for accepting connections. Closing the {@link |
| 64 | * BluetoothServerSocket} will <em>not</em> close the returned {@link BluetoothSocket}. |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 65 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 66 | * <p>{@link BluetoothServerSocket} is thread safe. In particular, {@link #close} will always |
| 67 | * immediately abort ongoing operations and close the server socket. |
| Nick Pelly | ed6f2ce | 2009-09-08 10:12:06 -0700 | [diff] [blame] | 68 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 69 | * <p><div class="special reference"> |
| 70 | * |
| Joe Fernandez | da4e2ab | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 71 | * <h3>Developer Guides</h3> |
| Joe Fernandez | da4e2ab | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 72 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 73 | * <p>For more information about using Bluetooth, read the <a |
| 74 | * href="{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer guide. </div> |
| 75 | * |
| David Duarte | 5a02bb4 | 2023-12-04 23:07:42 +0000 | [diff] [blame^] | 76 | * @see BluetoothSocket |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 77 | */ |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 78 | @SuppressLint("AndroidFrameworkBluetoothPermission") |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 79 | public final class BluetoothServerSocket implements Closeable { |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 80 | |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 81 | private static final String TAG = "BluetoothServerSocket"; |
| Ugo Yu | b6dc15a | 2022-12-28 20:18:00 +0800 | [diff] [blame] | 82 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 83 | |
| 84 | @UnsupportedAppUsage( |
| 85 | publicAlternatives = "Use public {@link BluetoothServerSocket} API " + "instead.") |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 86 | /*package*/ final BluetoothSocket mSocket; |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 87 | |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 88 | private Handler mHandler; |
| 89 | private int mMessage; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 90 | private int mChannel; |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 91 | private long mSocketCreationTimeMillis = 0; |
| 92 | private long mSocketCreationLatencyMillis = 0; |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 93 | |
| 94 | // BluetoothSocket.getConnectionType() will hide L2CAP_LE. |
| 95 | // Therefore a new variable need to be maintained here. |
| 96 | private int mType; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Construct a socket for incoming connections. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 100 | * |
| 101 | * @param type type of socket |
| 102 | * @param auth require the remote device to be authenticated |
| Nick Pelly | cb32d7c | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 103 | * @param encrypt require the connection to be encrypted |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 104 | * @param port remote port |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 105 | * @throws IOException On error, for example Bluetooth not available, or insufficient privileges |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 106 | */ |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 107 | /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port) |
| Nick Pelly | cb32d7c | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 108 | throws IOException { |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 109 | mSocketCreationTimeMillis = System.currentTimeMillis(); |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 110 | mType = type; |
| Ben Dodson | 48d0cca | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 111 | mChannel = port; |
| David Duarte | 5a02bb4 | 2023-12-04 23:07:42 +0000 | [diff] [blame^] | 112 | mSocket = new BluetoothSocket(type, auth, encrypt, null, port, null); |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 113 | if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
| 114 | mSocket.setExcludeSdp(true); |
| 115 | } |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 116 | mSocketCreationLatencyMillis = System.currentTimeMillis() - mSocketCreationTimeMillis; |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Construct a socket for incoming connections. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 121 | * |
| 122 | * @param type type of socket |
| 123 | * @param auth require the remote device to be authenticated |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 124 | * @param encrypt require the connection to be encrypted |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 125 | * @param port remote port |
| Jeff Sharkey | c5386af | 2020-09-11 14:57:21 -0600 | [diff] [blame] | 126 | * @param mitm enforce person-in-the-middle protection for authentication. |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 127 | * @param min16DigitPin enforce a minimum length of 16 digits for a sec mode 2 connection |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 128 | * @throws IOException On error, for example Bluetooth not available, or insufficient privileges |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 129 | */ |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 130 | /*package*/ BluetoothServerSocket( |
| 131 | int type, boolean auth, boolean encrypt, int port, boolean mitm, boolean min16DigitPin) |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 132 | throws IOException { |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 133 | mSocketCreationTimeMillis = System.currentTimeMillis(); |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 134 | mType = type; |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 135 | mChannel = port; |
| David Duarte | 5a02bb4 | 2023-12-04 23:07:42 +0000 | [diff] [blame^] | 136 | mSocket = new BluetoothSocket(type, auth, encrypt, null, port, null, mitm, min16DigitPin); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 137 | if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 138 | mSocket.setExcludeSdp(true); |
| 139 | } |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 140 | mSocketCreationLatencyMillis = System.currentTimeMillis() - mSocketCreationTimeMillis; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 144 | * Construct a socket for incoming connections. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 145 | * |
| 146 | * @param type type of socket |
| 147 | * @param auth require the remote device to be authenticated |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 148 | * @param encrypt require the connection to be encrypted |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 149 | * @param uuid uuid |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 150 | * @throws IOException On error, for example Bluetooth not available, or insufficient privileges |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 151 | */ |
| 152 | /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid) |
| 153 | throws IOException { |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 154 | mSocketCreationTimeMillis = System.currentTimeMillis(); |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 155 | mType = type; |
| David Duarte | 5a02bb4 | 2023-12-04 23:07:42 +0000 | [diff] [blame^] | 156 | mSocket = new BluetoothSocket(type, auth, encrypt, null, -1, uuid); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 157 | // TODO: This is the same as mChannel = -1 - is this intentional? |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 158 | mChannel = mSocket.getPort(); |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 159 | mSocketCreationLatencyMillis = System.currentTimeMillis() - mSocketCreationTimeMillis; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 162 | /** |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 163 | * Block until a connection is established. |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 164 | * |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 165 | * <p>Returns a connected {@link BluetoothSocket} on successful connection. |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 166 | * |
| 167 | * <p>Once this call returns, it can be called again to accept subsequent incoming connections. |
| 168 | * |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 169 | * <p>{@link #close} can be used to abort this call from another thread. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 170 | * |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 171 | * @return a connected {@link BluetoothSocket} |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 172 | * @throws IOException on error, for example this call was aborted, or timeout |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 173 | */ |
| 174 | public BluetoothSocket accept() throws IOException { |
| 175 | return accept(-1); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Block until a connection is established, with timeout. |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 180 | * |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 181 | * <p>Returns a connected {@link BluetoothSocket} on successful connection. |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 182 | * |
| 183 | * <p>Once this call returns, it can be called again to accept subsequent incoming connections. |
| 184 | * |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 185 | * <p>{@link #close} can be used to abort this call from another thread. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 186 | * |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 187 | * @return a connected {@link BluetoothSocket} |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 188 | * @throws IOException on error, for example this call was aborted, or timeout |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 189 | */ |
| 190 | public BluetoothSocket accept(int timeout) throws IOException { |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 191 | long socketConnectionTime = System.currentTimeMillis(); |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 192 | BluetoothSocket acceptedSocket = null; |
| 193 | try { |
| 194 | acceptedSocket = mSocket.accept(timeout); |
| 195 | logL2capcocServerConnection( |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 196 | acceptedSocket, |
| 197 | timeout, |
| 198 | BluetoothSocket.RESULT_L2CAP_CONN_SUCCESS, |
| 199 | socketConnectionTime); |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 200 | return acceptedSocket; |
| 201 | } catch (IOException e) { |
| 202 | logL2capcocServerConnection( |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 203 | acceptedSocket, |
| 204 | timeout, |
| 205 | BluetoothSocket.RESULT_L2CAP_CONN_SERVER_FAILURE, |
| 206 | socketConnectionTime); |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 207 | throw e; |
| 208 | } |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 211 | private void logL2capcocServerConnection( |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 212 | BluetoothSocket acceptedSocket, |
| 213 | int timeout, |
| 214 | int result, |
| 215 | long socketConnectionTimeMillis) { |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 216 | if (mType != BluetoothSocket.TYPE_L2CAP_LE) { |
| 217 | return; |
| 218 | } |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 219 | IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(); |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 220 | if (bluetoothProxy == null) { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 221 | Log.w(TAG, "bluetoothProxy is null while trying to log l2cap soc server connection"); |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | try { |
| 225 | final SynchronousResultReceiver recv = SynchronousResultReceiver.get(); |
| 226 | bluetoothProxy.logL2capcocServerConnection( |
| 227 | acceptedSocket == null ? null : acceptedSocket.getRemoteDevice(), |
| 228 | getPsm(), |
| 229 | mSocket.isAuth(), |
| 230 | result, |
| Chen Chen | 61174be | 2023-03-16 17:42:36 -0700 | [diff] [blame] | 231 | mSocketCreationTimeMillis, // pass creation time to calculate end to end latency |
| 232 | mSocketCreationLatencyMillis, // socket creation latency |
| 233 | socketConnectionTimeMillis, // send connection start time for connection latency |
| Chen Chen | 874e7b0 | 2023-03-07 17:37:43 -0800 | [diff] [blame] | 234 | timeout, |
| 235 | recv); |
| 236 | recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null); |
| 237 | |
| 238 | } catch (RemoteException | TimeoutException e) { |
| 239 | Log.w(TAG, "logL2capcocServerConnection failed due to remote exception"); |
| 240 | } |
| 241 | } |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 242 | |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 243 | /** |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 244 | * Immediately close this socket, and release all associated resources. |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 245 | * |
| 246 | * <p>Causes blocked calls on this socket in other threads to immediately throw an IOException. |
| 247 | * |
| 248 | * <p>Closing the {@link BluetoothServerSocket} will <em>not</em> close any {@link |
| 249 | * BluetoothSocket} received from {@link #accept()}. |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 250 | */ |
| 251 | public void close() throws IOException { |
| Stanley Tng | d67d5e4 | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 252 | if (DBG) Log.d(TAG, "BluetoothServerSocket:close() called. mChannel=" + mChannel); |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 253 | synchronized (this) { |
| 254 | if (mHandler != null) { |
| 255 | mHandler.obtainMessage(mMessage).sendToTarget(); |
| 256 | } |
| 257 | } |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 258 | mSocket.close(); |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 259 | } |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 260 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 261 | /*package*/ |
| 262 | synchronized void setCloseHandler(Handler handler, int message) { |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 263 | mHandler = handler; |
| 264 | mMessage = message; |
| 265 | } |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 266 | |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 267 | /*package*/ void setServiceName(String serviceName) { |
| 268 | mSocket.setServiceName(serviceName); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 269 | } |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 270 | |
| Ben Dodson | 48d0cca | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 271 | /** |
| 272 | * Returns the channel on which this socket is bound. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 273 | * |
| Ben Dodson | 48d0cca | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 274 | * @hide |
| 275 | */ |
| 276 | public int getChannel() { |
| 277 | return mChannel; |
| 278 | } |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 279 | |
| 280 | /** |
| Stanley Tng | d67d5e4 | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 281 | * Returns the assigned dynamic protocol/service multiplexer (PSM) value for the listening L2CAP |
| 282 | * Connection-oriented Channel (CoC) server socket. This server socket must be returned by the |
| Xin Li | 10802fb | 2019-02-13 22:36:25 -0800 | [diff] [blame] | 283 | * {@link BluetoothAdapter#listenUsingL2capChannel()} or {@link |
| 284 | * BluetoothAdapter#listenUsingInsecureL2capChannel()}. The returned value is undefined if this |
| Stanley Tng | d67d5e4 | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 285 | * method is called on non-L2CAP server sockets. |
| 286 | * |
| 287 | * @return the assigned PSM or LE_PSM value depending on transport |
| Stanley Tng | d67d5e4 | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 288 | */ |
| 289 | public int getPsm() { |
| 290 | return mChannel; |
| 291 | } |
| 292 | |
| 293 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 294 | * Sets the channel on which future sockets are bound. Currently used only when a channel is |
| 295 | * auto generated. |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 296 | */ |
| 297 | /*package*/ void setChannel(int newChannel) { |
| 298 | /* TODO: From a design/architecture perspective this is wrong. |
| 299 | * The bind operation should be conducted through this class |
| 300 | * and the resulting port should be kept in mChannel, and |
| 301 | * not set from BluetoothAdapter. */ |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 302 | if (mSocket != null) { |
| 303 | if (mSocket.getPort() != newChannel) { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 304 | Log.w( |
| 305 | TAG, |
| 306 | "The port set is different that the underlying port. mSocket.getPort(): " |
| 307 | + mSocket.getPort() |
| 308 | + " requested newChannel: " |
| 309 | + newChannel); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | mChannel = newChannel; |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public String toString() { |
| 317 | StringBuilder sb = new StringBuilder(); |
| 318 | sb.append("ServerSocket: Type: "); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 319 | switch (mSocket.getConnectionType()) { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 320 | case BluetoothSocket.TYPE_RFCOMM: |
| 321 | { |
| 322 | sb.append("TYPE_RFCOMM"); |
| 323 | break; |
| 324 | } |
| 325 | case BluetoothSocket.TYPE_L2CAP: |
| 326 | { |
| 327 | sb.append("TYPE_L2CAP"); |
| 328 | break; |
| 329 | } |
| 330 | case BluetoothSocket.TYPE_L2CAP_LE: |
| 331 | { |
| 332 | sb.append("TYPE_L2CAP_LE"); |
| 333 | break; |
| 334 | } |
| 335 | case BluetoothSocket.TYPE_SCO: |
| 336 | { |
| 337 | sb.append("TYPE_SCO"); |
| 338 | break; |
| 339 | } |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 340 | } |
| 341 | sb.append(" Channel: ").append(mChannel); |
| 342 | return sb.toString(); |
| 343 | } |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 344 | } |