blob: c15852dcdaa7f28ef1482b1119d237181a408306 [file] [log] [blame]
Nick Pelly0b6955a2009-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
Nick Pelly24bb9b82009-10-02 20:34:18 -070019import android.os.Handler;
zzy3b147b72012-04-03 19:48:32 -070020import android.os.ParcelUuid;
Casper Bonde238e0f92015-04-09 09:24:48 +020021import android.util.Log;
Nick Pelly24bb9b82009-10-02 20:34:18 -070022
Nick Pelly0b6955a2009-05-26 19:13:43 -070023import java.io.Closeable;
24import java.io.IOException;
25
26/**
Nick Pelly45e27042009-08-19 11:00:00 -070027 * A listening Bluetooth socket.
Nick Pelly0b6955a2009-05-26 19:13:43 -070028 *
Nick Pelly45e27042009-08-19 11:00:00 -070029 * <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 Main9fab0ae2009-11-03 18:17:59 -080032 * socket. When a connection is accepted by the {@link BluetoothServerSocket},
33 * it will return a new {@link BluetoothSocket} to manage the connection.
Jake Hambyf51eada2010-09-21 13:39:53 -070034 * On the client side, use a single {@link BluetoothSocket} to both initiate
Scott Main9fab0ae2009-11-03 18:17:59 -080035 * an outgoing connection and to manage the connection.
Nick Pelly0b6955a2009-05-26 19:13:43 -070036 *
Scott Main9fab0ae2009-11-03 18:17:59 -080037 * <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 Pelly0b6955a2009-05-26 19:13:43 -070040 *
Jake Hambyf51eada2010-09-21 13:39:53 -070041 * <p>To create a listening {@link BluetoothServerSocket} that's ready for
Scott Main9fab0ae2009-11-03 18:17:59 -080042 * 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 Main6d95fc02009-11-19 17:00:19 -080047 * 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 Pelly45e27042009-08-19 11:00:00 -070052 *
Scott Main9fab0ae2009-11-03 18:17:59 -080053 * <p>{@link BluetoothServerSocket} is thread
Nick Pelly45e27042009-08-19 11:00:00 -070054 * safe. In particular, {@link #close} will always immediately abort ongoing
Scott Main9fab0ae2009-11-03 18:17:59 -080055 * operations and close the server socket.
Nick Pellycf440592009-09-08 10:12:06 -070056 *
Scott Main9fab0ae2009-11-03 18:17:59 -080057 * <p class="note"><strong>Note:</strong>
58 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
59 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080060 * <div class="special reference">
61 * <h3>Developer Guides</h3>
62 * <p>For more information about using Bluetooth, read the
63 * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p>
64 * </div>
65 *
Scott Main9fab0ae2009-11-03 18:17:59 -080066 * {@see BluetoothSocket}
Nick Pelly0b6955a2009-05-26 19:13:43 -070067 */
68public final class BluetoothServerSocket implements Closeable {
Nick Pelly71c3c782009-09-02 11:51:35 -070069
Casper Bonde238e0f92015-04-09 09:24:48 +020070 private static final String TAG = "BluetoothServerSocket";
Nick Pellybd022f42009-08-14 18:33:38 -070071 /*package*/ final BluetoothSocket mSocket;
Nick Pelly24bb9b82009-10-02 20:34:18 -070072 private Handler mHandler;
73 private int mMessage;
Casper Bonde238e0f92015-04-09 09:24:48 +020074 private int mChannel;
Nick Pelly0b6955a2009-05-26 19:13:43 -070075
76 /**
77 * Construct a socket for incoming connections.
Nick Pelly6a669fa2009-06-02 15:57:18 -070078 * @param type type of socket
79 * @param auth require the remote device to be authenticated
80 * @param encrypt require the connection to be encrypted
81 * @param port remote port
Nick Pelly0b6955a2009-05-26 19:13:43 -070082 * @throws IOException On error, for example Bluetooth not available, or
Jake Hambyf51eada2010-09-21 13:39:53 -070083 * insufficient privileges
Nick Pelly0b6955a2009-05-26 19:13:43 -070084 */
Nick Pellybd022f42009-08-14 18:33:38 -070085 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
Nick Pelly6a669fa2009-06-02 15:57:18 -070086 throws IOException {
Ben Dodson6d8b80d2011-07-08 14:36:42 -070087 mChannel = port;
Nick Pelly16fb88a2009-10-07 07:44:03 +020088 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null);
Casper Bonde23284232015-04-21 13:12:05 +020089 if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
90 mSocket.setExcludeSdp(true);
91 }
92 }
93
94 /**
95 * Construct a socket for incoming connections.
96 * @param type type of socket
97 * @param auth require the remote device to be authenticated
98 * @param encrypt require the connection to be encrypted
99 * @param port remote port
100 * @param mitm enforce man-in-the-middle protection for authentication.
Casper Bonde3b3d1fe2015-05-08 14:32:24 +0200101 * @param min16DigitPin enforce a minimum length of 16 digits for a sec mode 2 connection
Casper Bonde23284232015-04-21 13:12:05 +0200102 * @throws IOException On error, for example Bluetooth not available, or
103 * insufficient privileges
104 */
105 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port,
Casper Bonde3b3d1fe2015-05-08 14:32:24 +0200106 boolean mitm, boolean min16DigitPin)
Casper Bonde23284232015-04-21 13:12:05 +0200107 throws IOException {
108 mChannel = port;
Casper Bonde3b3d1fe2015-05-08 14:32:24 +0200109 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null, mitm,
110 min16DigitPin);
Casper Bonde238e0f92015-04-09 09:24:48 +0200111 if(port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
112 mSocket.setExcludeSdp(true);
113 }
Nick Pelly0b6955a2009-05-26 19:13:43 -0700114 }
115
116 /**
zzy3b147b72012-04-03 19:48:32 -0700117 * Construct a socket for incoming connections.
118 * @param type type of socket
119 * @param auth require the remote device to be authenticated
120 * @param encrypt require the connection to be encrypted
121 * @param uuid uuid
122 * @throws IOException On error, for example Bluetooth not available, or
123 * insufficient privileges
124 */
125 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid)
126 throws IOException {
127 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, -1, uuid);
Casper Bonde238e0f92015-04-09 09:24:48 +0200128 // TODO: This is the same as mChannel = -1 - is this intentional?
zzy3b147b72012-04-03 19:48:32 -0700129 mChannel = mSocket.getPort();
130 }
131
132
133 /**
Nick Pelly0b6955a2009-05-26 19:13:43 -0700134 * Block until a connection is established.
Nick Pelly45e27042009-08-19 11:00:00 -0700135 * <p>Returns a connected {@link BluetoothSocket} on successful connection.
136 * <p>Once this call returns, it can be called again to accept subsequent
137 * incoming connections.
138 * <p>{@link #close} can be used to abort this call from another thread.
139 * @return a connected {@link BluetoothSocket}
140 * @throws IOException on error, for example this call was aborted, or
141 * timeout
Nick Pelly0b6955a2009-05-26 19:13:43 -0700142 */
143 public BluetoothSocket accept() throws IOException {
144 return accept(-1);
145 }
146
147 /**
148 * Block until a connection is established, with timeout.
Nick Pelly45e27042009-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.
153 * @return a connected {@link BluetoothSocket}
154 * @throws IOException on error, for example this call was aborted, or
Nick Pelly0b6955a2009-05-26 19:13:43 -0700155 * timeout
156 */
157 public BluetoothSocket accept(int timeout) throws IOException {
Nick Pelly71c3c782009-09-02 11:51:35 -0700158 return mSocket.accept(timeout);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700159 }
160
161 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700162 * Immediately close this socket, and release all associated resources.
163 * <p>Causes blocked calls on this socket in other threads to immediately
Nick Pelly0b6955a2009-05-26 19:13:43 -0700164 * throw an IOException.
Scott Main6d95fc02009-11-19 17:00:19 -0800165 * <p>Closing the {@link BluetoothServerSocket} will <em>not</em>
166 * close any {@link BluetoothSocket} received from {@link #accept()}.
Nick Pelly0b6955a2009-05-26 19:13:43 -0700167 */
168 public void close() throws IOException {
Nick Pelly24bb9b82009-10-02 20:34:18 -0700169 synchronized (this) {
170 if (mHandler != null) {
171 mHandler.obtainMessage(mMessage).sendToTarget();
172 }
173 }
Nick Pelly71c3c782009-09-02 11:51:35 -0700174 mSocket.close();
Nick Pelly0b6955a2009-05-26 19:13:43 -0700175 }
Nick Pelly24bb9b82009-10-02 20:34:18 -0700176
177 /*package*/ synchronized void setCloseHandler(Handler handler, int message) {
178 mHandler = handler;
179 mMessage = message;
180 }
zzy3b147b72012-04-03 19:48:32 -0700181 /*package*/ void setServiceName(String ServiceName) {
182 mSocket.setServiceName(ServiceName);
183 }
Casper Bonde238e0f92015-04-09 09:24:48 +0200184
Ben Dodson6d8b80d2011-07-08 14:36:42 -0700185 /**
186 * Returns the channel on which this socket is bound.
187 * @hide
188 */
189 public int getChannel() {
190 return mChannel;
191 }
Casper Bonde238e0f92015-04-09 09:24:48 +0200192
193 /**
194 * Sets the channel on which future sockets are bound.
195 * Currently used only when a channel is auto generated.
196 */
197 /*package*/ void setChannel(int newChannel) {
198 /* TODO: From a design/architecture perspective this is wrong.
199 * The bind operation should be conducted through this class
200 * and the resulting port should be kept in mChannel, and
201 * not set from BluetoothAdapter. */
202 if(mSocket != null) {
203 if(mSocket.getPort() != newChannel) {
204 Log.w(TAG,"The port set is different that the underlying port. mSocket.getPort(): "
205 + mSocket.getPort() + " requested newChannel: " + newChannel);
206 }
207 }
208 mChannel = newChannel;
209 }
210
211 @Override
212 public String toString() {
213 StringBuilder sb = new StringBuilder();
214 sb.append("ServerSocket: Type: ");
215 switch(mSocket.getConnectionType()) {
216 case BluetoothSocket.TYPE_RFCOMM:
217 {
218 sb.append("TYPE_RFCOMM");
219 break;
220 }
221 case BluetoothSocket.TYPE_L2CAP:
222 {
223 sb.append("TYPE_L2CAP");
224 break;
225 }
226 case BluetoothSocket.TYPE_SCO:
227 {
228 sb.append("TYPE_SCO");
229 break;
230 }
231 }
232 sb.append(" Channel: ").append(mChannel);
233 return sb.toString();
234 }
Nick Pelly0b6955a2009-05-26 19:13:43 -0700235}