Nick Pelly | 0b6955a | 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 | |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 20 | import android.os.Handler; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 21 | import android.os.ParcelUuid; |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 22 | import android.util.Log; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 23 | |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 24 | import java.io.Closeable; |
| 25 | import java.io.IOException; |
| 26 | |
| 27 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 28 | * A listening Bluetooth socket. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 29 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 30 | * <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 Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 33 | * socket. When a connection is accepted by the {@link BluetoothServerSocket}, |
| 34 | * it will return a new {@link BluetoothSocket} to manage the connection. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 35 | * On the client side, use a single {@link BluetoothSocket} to both initiate |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 36 | * an outgoing connection and to manage the connection. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 37 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 38 | * <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 Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 41 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 42 | * <p>To create a listening {@link BluetoothServerSocket} that's ready for |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 43 | * 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 Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 48 | * 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 Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 53 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 54 | * <p>{@link BluetoothServerSocket} is thread |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 55 | * safe. In particular, {@link #close} will always immediately abort ongoing |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 56 | * operations and close the server socket. |
Nick Pelly | cf44059 | 2009-09-08 10:12:06 -0700 | [diff] [blame] | 57 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 58 | * <p class="note"><strong>Note:</strong> |
| 59 | * Requires the {@link android.Manifest.permission#BLUETOOTH} permission. |
| 60 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 61 | * <div class="special reference"> |
| 62 | * <h3>Developer Guides</h3> |
| 63 | * <p>For more information about using Bluetooth, read the |
Marie Janssen | 382871b | 2016-06-20 10:26:31 -0700 | [diff] [blame] | 64 | * <a href="{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer guide.</p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 65 | * </div> |
| 66 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 67 | * {@see BluetoothSocket} |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 68 | */ |
| 69 | public final class BluetoothServerSocket implements Closeable { |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 70 | |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 71 | private static final String TAG = "BluetoothServerSocket"; |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 72 | private static final boolean DBG = false; |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 73 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 74 | /*package*/ final BluetoothSocket mSocket; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 75 | private Handler mHandler; |
| 76 | private int mMessage; |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 77 | private int mChannel; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Construct a socket for incoming connections. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 81 | * |
| 82 | * @param type type of socket |
| 83 | * @param auth require the remote device to be authenticated |
Nick Pelly | 6a669fa | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 84 | * @param encrypt require the connection to be encrypted |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 85 | * @param port remote port |
| 86 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 87 | * privileges |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 88 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 89 | /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port) |
Nick Pelly | 6a669fa | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 90 | throws IOException { |
Ben Dodson | 6d8b80d | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 91 | mChannel = port; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 92 | mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null); |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 93 | 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 He | a355e5e | 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 |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 103 | * @param encrypt require the connection to be encrypted |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 104 | * @param port remote port |
| 105 | * @param mitm enforce man-in-the-middle protection for authentication. |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 106 | * @param min16DigitPin enforce a minimum length of 16 digits for a sec mode 2 connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 107 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 108 | * privileges |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 109 | */ |
| 110 | /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port, |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 111 | boolean mitm, boolean min16DigitPin) |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 112 | throws IOException { |
| 113 | mChannel = port; |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 114 | mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null, mitm, |
| 115 | min16DigitPin); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 116 | if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 117 | mSocket.setExcludeSdp(true); |
| 118 | } |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 122 | * Construct a socket for incoming connections. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 123 | * |
| 124 | * @param type type of socket |
| 125 | * @param auth require the remote device to be authenticated |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 126 | * @param encrypt require the connection to be encrypted |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 127 | * @param uuid uuid |
| 128 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 129 | * privileges |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 130 | */ |
| 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 Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 134 | // TODO: This is the same as mChannel = -1 - is this intentional? |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 135 | mChannel = mSocket.getPort(); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 140 | * Block until a connection is established. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 141 | * <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 He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 145 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 146 | * @return a connected {@link BluetoothSocket} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 147 | * @throws IOException on error, for example this call was aborted, or timeout |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 148 | */ |
| 149 | public BluetoothSocket accept() throws IOException { |
| 150 | return accept(-1); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Block until a connection is established, with timeout. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 155 | * <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 He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 159 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 160 | * @return a connected {@link BluetoothSocket} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 161 | * @throws IOException on error, for example this call was aborted, or timeout |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 162 | */ |
| 163 | public BluetoothSocket accept(int timeout) throws IOException { |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 164 | return mSocket.accept(timeout); |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 168 | * Immediately close this socket, and release all associated resources. |
| 169 | * <p>Causes blocked calls on this socket in other threads to immediately |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 170 | * throw an IOException. |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 171 | * <p>Closing the {@link BluetoothServerSocket} will <em>not</em> |
| 172 | * close any {@link BluetoothSocket} received from {@link #accept()}. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 173 | */ |
| 174 | public void close() throws IOException { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 175 | if (DBG) Log.d(TAG, "BluetoothServerSocket:close() called. mChannel=" + mChannel); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 176 | synchronized (this) { |
| 177 | if (mHandler != null) { |
| 178 | mHandler.obtainMessage(mMessage).sendToTarget(); |
| 179 | } |
| 180 | } |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 181 | mSocket.close(); |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 182 | } |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 183 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 184 | /*package*/ |
| 185 | synchronized void setCloseHandler(Handler handler, int message) { |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 186 | mHandler = handler; |
| 187 | mMessage = message; |
| 188 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 189 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 190 | /*package*/ void setServiceName(String serviceName) { |
| 191 | mSocket.setServiceName(serviceName); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 192 | } |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 193 | |
Ben Dodson | 6d8b80d | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Returns the channel on which this socket is bound. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 196 | * |
Ben Dodson | 6d8b80d | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 197 | * @hide |
| 198 | */ |
| 199 | public int getChannel() { |
| 200 | return mChannel; |
| 201 | } |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 202 | |
| 203 | /** |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 204 | * 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 |
Nan Zhang | 55581d9 | 2018-10-11 17:54:36 -0700 | [diff] [blame] | 206 | * {@link BluetoothAdapter#listenUsingL2capChannel()} or {@link |
| 207 | * BluetoothAdapter#listenUsingInsecureL2capChannel()}. The returned value is undefined if this |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 208 | * method is called on non-L2CAP server sockets. |
| 209 | * |
| 210 | * @return the assigned PSM or LE_PSM value depending on transport |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 211 | */ |
| 212 | public int getPsm() { |
| 213 | return mChannel; |
| 214 | } |
| 215 | |
| 216 | /** |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 217 | * 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 He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 225 | 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 Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | mChannel = newChannel; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public String toString() { |
| 236 | StringBuilder sb = new StringBuilder(); |
| 237 | sb.append("ServerSocket: Type: "); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 238 | switch (mSocket.getConnectionType()) { |
| 239 | case BluetoothSocket.TYPE_RFCOMM: { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 240 | sb.append("TYPE_RFCOMM"); |
| 241 | break; |
| 242 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 243 | case BluetoothSocket.TYPE_L2CAP: { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 244 | sb.append("TYPE_L2CAP"); |
| 245 | break; |
| 246 | } |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 247 | case BluetoothSocket.TYPE_L2CAP_LE: { |
| 248 | sb.append("TYPE_L2CAP_LE"); |
| 249 | break; |
| 250 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 251 | case BluetoothSocket.TYPE_SCO: { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 252 | sb.append("TYPE_SCO"); |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | sb.append(" Channel: ").append(mChannel); |
| 257 | return sb.toString(); |
| 258 | } |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 259 | } |