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 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 19 | import android.os.Handler; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 20 | import android.os.ParcelUuid; |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 21 | import android.util.Log; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 22 | |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 23 | import java.io.Closeable; |
| 24 | import java.io.IOException; |
| 25 | |
| 26 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 27 | * A listening Bluetooth socket. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 28 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 29 | * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets: |
| 30 | * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server |
| 31 | * side, use a {@link BluetoothServerSocket} to create a listening server |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 32 | * socket. When a connection is accepted by the {@link BluetoothServerSocket}, |
| 33 | * it will return a new {@link BluetoothSocket} to manage the connection. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 34 | * On the client side, use a single {@link BluetoothSocket} to both initiate |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 35 | * an outgoing connection and to manage the connection. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 36 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 37 | * <p>The most common type of Bluetooth socket is RFCOMM, which is the type |
| 38 | * supported by the Android APIs. RFCOMM is a connection-oriented, streaming |
| 39 | * 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] | 40 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 41 | * <p>To create a listening {@link BluetoothServerSocket} that's ready for |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 42 | * incoming connections, use |
| 43 | * {@link BluetoothAdapter#listenUsingRfcommWithServiceRecord |
| 44 | * BluetoothAdapter.listenUsingRfcommWithServiceRecord()}. Then call |
| 45 | * {@link #accept()} to listen for incoming connection requests. This call |
| 46 | * 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] | 47 | * a {@link BluetoothSocket} to manage the connection. Once the {@link |
| 48 | * BluetoothSocket} is acquired, it's a good idea to call {@link #close()} on |
| 49 | * the {@link BluetoothServerSocket} when it's no longer needed for accepting |
| 50 | * connections. Closing the {@link BluetoothServerSocket} will <em>not</em> |
| 51 | * close the returned {@link BluetoothSocket}. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 52 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 53 | * <p>{@link BluetoothServerSocket} is thread |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 54 | * safe. In particular, {@link #close} will always immediately abort ongoing |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 55 | * operations and close the server socket. |
Nick Pelly | cf44059 | 2009-09-08 10:12:06 -0700 | [diff] [blame] | 56 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 57 | * <p class="note"><strong>Note:</strong> |
| 58 | * Requires the {@link android.Manifest.permission#BLUETOOTH} permission. |
| 59 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 60 | * <div class="special reference"> |
| 61 | * <h3>Developer Guides</h3> |
| 62 | * <p>For more information about using Bluetooth, read the |
Marie Janssen | 382871b | 2016-06-20 10:26:31 -0700 | [diff] [blame] | 63 | * <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] | 64 | * </div> |
| 65 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 66 | * {@see BluetoothSocket} |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 67 | */ |
| 68 | public final class BluetoothServerSocket implements Closeable { |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 69 | |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 70 | private static final String TAG = "BluetoothServerSocket"; |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame^] | 71 | private static final boolean DBG = false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 72 | /*package*/ final BluetoothSocket mSocket; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 73 | private Handler mHandler; |
| 74 | private int mMessage; |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 75 | private int mChannel; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Construct a socket for incoming connections. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 79 | * |
| 80 | * @param type type of socket |
| 81 | * @param auth require the remote device to be authenticated |
Nick Pelly | 6a669fa | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 82 | * @param encrypt require the connection to be encrypted |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 83 | * @param port remote port |
| 84 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 85 | * privileges |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 86 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 87 | /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port) |
Nick Pelly | 6a669fa | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 88 | throws IOException { |
Ben Dodson | 6d8b80d | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 89 | mChannel = port; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 90 | mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null); |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 91 | if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
| 92 | mSocket.setExcludeSdp(true); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Construct a socket for incoming connections. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 98 | * |
| 99 | * @param type type of socket |
| 100 | * @param auth require the remote device to be authenticated |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 101 | * @param encrypt require the connection to be encrypted |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 102 | * @param port remote port |
| 103 | * @param mitm enforce man-in-the-middle protection for authentication. |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 104 | * @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] | 105 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 106 | * privileges |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 107 | */ |
| 108 | /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port, |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 109 | boolean mitm, boolean min16DigitPin) |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 110 | throws IOException { |
| 111 | mChannel = port; |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 112 | mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null, mitm, |
| 113 | min16DigitPin); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 114 | if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 115 | mSocket.setExcludeSdp(true); |
| 116 | } |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 120 | * Construct a socket for incoming connections. |
Jack He | a355e5e | 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 |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 124 | * @param encrypt require the connection to be encrypted |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 125 | * @param uuid uuid |
| 126 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 127 | * privileges |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 128 | */ |
| 129 | /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid) |
| 130 | throws IOException { |
| 131 | mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, -1, uuid); |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 132 | // TODO: This is the same as mChannel = -1 - is this intentional? |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 133 | mChannel = mSocket.getPort(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 138 | * Block until a connection is established. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 139 | * <p>Returns a connected {@link BluetoothSocket} on successful connection. |
| 140 | * <p>Once this call returns, it can be called again to accept subsequent |
| 141 | * incoming connections. |
| 142 | * <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] | 143 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 144 | * @return a connected {@link BluetoothSocket} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 145 | * @throws IOException on error, for example this call was aborted, or timeout |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 146 | */ |
| 147 | public BluetoothSocket accept() throws IOException { |
| 148 | return accept(-1); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Block until a connection is established, with timeout. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 153 | * <p>Returns a connected {@link BluetoothSocket} on successful connection. |
| 154 | * <p>Once this call returns, it can be called again to accept subsequent |
| 155 | * incoming connections. |
| 156 | * <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] | 157 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 158 | * @return a connected {@link BluetoothSocket} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 159 | * @throws IOException on error, for example this call was aborted, or timeout |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 160 | */ |
| 161 | public BluetoothSocket accept(int timeout) throws IOException { |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 162 | return mSocket.accept(timeout); |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 166 | * Immediately close this socket, and release all associated resources. |
| 167 | * <p>Causes blocked calls on this socket in other threads to immediately |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 168 | * throw an IOException. |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 169 | * <p>Closing the {@link BluetoothServerSocket} will <em>not</em> |
| 170 | * close any {@link BluetoothSocket} received from {@link #accept()}. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 171 | */ |
| 172 | public void close() throws IOException { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame^] | 173 | if (DBG) Log.d(TAG, "BluetoothServerSocket:close() called. mChannel=" + mChannel); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 174 | synchronized (this) { |
| 175 | if (mHandler != null) { |
| 176 | mHandler.obtainMessage(mMessage).sendToTarget(); |
| 177 | } |
| 178 | } |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 179 | mSocket.close(); |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 180 | } |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 181 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 182 | /*package*/ |
| 183 | synchronized void setCloseHandler(Handler handler, int message) { |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 184 | mHandler = handler; |
| 185 | mMessage = message; |
| 186 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 187 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 188 | /*package*/ void setServiceName(String serviceName) { |
| 189 | mSocket.setServiceName(serviceName); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 190 | } |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 191 | |
Ben Dodson | 6d8b80d | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Returns the channel on which this socket is bound. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 194 | * |
Ben Dodson | 6d8b80d | 2011-07-08 14:36:42 -0700 | [diff] [blame] | 195 | * @hide |
| 196 | */ |
| 197 | public int getChannel() { |
| 198 | return mChannel; |
| 199 | } |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 200 | |
| 201 | /** |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame^] | 202 | * Returns the assigned dynamic protocol/service multiplexer (PSM) value for the listening L2CAP |
| 203 | * Connection-oriented Channel (CoC) server socket. This server socket must be returned by the |
| 204 | * {#link BluetoothAdapter.listenUsingL2capCoc(int)} or {#link |
| 205 | * BluetoothAdapter.listenUsingInsecureL2capCoc(int)}. The returned value is undefined if this |
| 206 | * method is called on non-L2CAP server sockets. |
| 207 | * |
| 208 | * @return the assigned PSM or LE_PSM value depending on transport |
| 209 | * @hide |
| 210 | */ |
| 211 | public int getPsm() { |
| 212 | return mChannel; |
| 213 | } |
| 214 | |
| 215 | /** |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 216 | * Sets the channel on which future sockets are bound. |
| 217 | * Currently used only when a channel is auto generated. |
| 218 | */ |
| 219 | /*package*/ void setChannel(int newChannel) { |
| 220 | /* TODO: From a design/architecture perspective this is wrong. |
| 221 | * The bind operation should be conducted through this class |
| 222 | * and the resulting port should be kept in mChannel, and |
| 223 | * not set from BluetoothAdapter. */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 224 | if (mSocket != null) { |
| 225 | if (mSocket.getPort() != newChannel) { |
| 226 | Log.w(TAG, "The port set is different that the underlying port. mSocket.getPort(): " |
| 227 | + mSocket.getPort() + " requested newChannel: " + newChannel); |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | mChannel = newChannel; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public String toString() { |
| 235 | StringBuilder sb = new StringBuilder(); |
| 236 | sb.append("ServerSocket: Type: "); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 237 | switch (mSocket.getConnectionType()) { |
| 238 | case BluetoothSocket.TYPE_RFCOMM: { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 239 | sb.append("TYPE_RFCOMM"); |
| 240 | break; |
| 241 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 242 | case BluetoothSocket.TYPE_L2CAP: { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 243 | sb.append("TYPE_L2CAP"); |
| 244 | break; |
| 245 | } |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame^] | 246 | case BluetoothSocket.TYPE_L2CAP_LE: { |
| 247 | sb.append("TYPE_L2CAP_LE"); |
| 248 | break; |
| 249 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 250 | case BluetoothSocket.TYPE_SCO: { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 251 | sb.append("TYPE_SCO"); |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | sb.append(" Channel: ").append(mChannel); |
| 256 | return sb.toString(); |
| 257 | } |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 258 | } |