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