blob: acce182189dc2d95fa6d97aad687beb7ec0b85e0 [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;
20
Nick Pelly0b6955a2009-05-26 19:13:43 -070021import java.io.Closeable;
22import java.io.IOException;
23
24/**
Nick Pelly45e27042009-08-19 11:00:00 -070025 * A listening Bluetooth socket.
Nick Pelly0b6955a2009-05-26 19:13:43 -070026 *
Nick Pelly45e27042009-08-19 11:00:00 -070027 * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets:
28 * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server
29 * side, use a {@link BluetoothServerSocket} to create a listening server
Scott Main9fab0ae2009-11-03 18:17:59 -080030 * socket. When a connection is accepted by the {@link BluetoothServerSocket},
31 * it will return a new {@link BluetoothSocket} to manage the connection.
Jake Hambyf51eada2010-09-21 13:39:53 -070032 * On the client side, use a single {@link BluetoothSocket} to both initiate
Scott Main9fab0ae2009-11-03 18:17:59 -080033 * an outgoing connection and to manage the connection.
Nick Pelly0b6955a2009-05-26 19:13:43 -070034 *
Scott Main9fab0ae2009-11-03 18:17:59 -080035 * <p>The most common type of Bluetooth socket is RFCOMM, which is the type
36 * supported by the Android APIs. RFCOMM is a connection-oriented, streaming
37 * transport over Bluetooth. It is also known as the Serial Port Profile (SPP).
Nick Pelly0b6955a2009-05-26 19:13:43 -070038 *
Jake Hambyf51eada2010-09-21 13:39:53 -070039 * <p>To create a listening {@link BluetoothServerSocket} that's ready for
Scott Main9fab0ae2009-11-03 18:17:59 -080040 * incoming connections, use
41 * {@link BluetoothAdapter#listenUsingRfcommWithServiceRecord
42 * BluetoothAdapter.listenUsingRfcommWithServiceRecord()}. Then call
43 * {@link #accept()} to listen for incoming connection requests. This call
44 * will block until a connection is established, at which point, it will return
Scott Main6d95fc02009-11-19 17:00:19 -080045 * a {@link BluetoothSocket} to manage the connection. Once the {@link
46 * BluetoothSocket} is acquired, it's a good idea to call {@link #close()} on
47 * the {@link BluetoothServerSocket} when it's no longer needed for accepting
48 * connections. Closing the {@link BluetoothServerSocket} will <em>not</em>
49 * close the returned {@link BluetoothSocket}.
Nick Pelly45e27042009-08-19 11:00:00 -070050 *
Scott Main9fab0ae2009-11-03 18:17:59 -080051 * <p>{@link BluetoothServerSocket} is thread
Nick Pelly45e27042009-08-19 11:00:00 -070052 * safe. In particular, {@link #close} will always immediately abort ongoing
Scott Main9fab0ae2009-11-03 18:17:59 -080053 * operations and close the server socket.
Nick Pellycf440592009-09-08 10:12:06 -070054 *
Scott Main9fab0ae2009-11-03 18:17:59 -080055 * <p class="note"><strong>Note:</strong>
56 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
57 *
58 * {@see BluetoothSocket}
Nick Pelly0b6955a2009-05-26 19:13:43 -070059 */
60public final class BluetoothServerSocket implements Closeable {
Nick Pelly71c3c782009-09-02 11:51:35 -070061
Nick Pellybd022f42009-08-14 18:33:38 -070062 /*package*/ final BluetoothSocket mSocket;
Nick Pelly24bb9b82009-10-02 20:34:18 -070063 private Handler mHandler;
64 private int mMessage;
Ben Dodson6d8b80d2011-07-08 14:36:42 -070065 private final int mChannel;
Nick Pelly0b6955a2009-05-26 19:13:43 -070066
67 /**
68 * Construct a socket for incoming connections.
Nick Pelly6a669fa2009-06-02 15:57:18 -070069 * @param type type of socket
70 * @param auth require the remote device to be authenticated
71 * @param encrypt require the connection to be encrypted
72 * @param port remote port
Nick Pelly0b6955a2009-05-26 19:13:43 -070073 * @throws IOException On error, for example Bluetooth not available, or
Jake Hambyf51eada2010-09-21 13:39:53 -070074 * insufficient privileges
Nick Pelly0b6955a2009-05-26 19:13:43 -070075 */
Nick Pellybd022f42009-08-14 18:33:38 -070076 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
Nick Pelly6a669fa2009-06-02 15:57:18 -070077 throws IOException {
Ben Dodson6d8b80d2011-07-08 14:36:42 -070078 mChannel = port;
Nick Pelly16fb88a2009-10-07 07:44:03 +020079 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null);
Nick Pelly0b6955a2009-05-26 19:13:43 -070080 }
81
82 /**
83 * Block until a connection is established.
Nick Pelly45e27042009-08-19 11:00:00 -070084 * <p>Returns a connected {@link BluetoothSocket} on successful connection.
85 * <p>Once this call returns, it can be called again to accept subsequent
86 * incoming connections.
87 * <p>{@link #close} can be used to abort this call from another thread.
88 * @return a connected {@link BluetoothSocket}
89 * @throws IOException on error, for example this call was aborted, or
90 * timeout
Nick Pelly0b6955a2009-05-26 19:13:43 -070091 */
92 public BluetoothSocket accept() throws IOException {
93 return accept(-1);
94 }
95
96 /**
97 * Block until a connection is established, with timeout.
Nick Pelly45e27042009-08-19 11:00:00 -070098 * <p>Returns a connected {@link BluetoothSocket} on successful connection.
99 * <p>Once this call returns, it can be called again to accept subsequent
100 * incoming connections.
101 * <p>{@link #close} can be used to abort this call from another thread.
102 * @return a connected {@link BluetoothSocket}
103 * @throws IOException on error, for example this call was aborted, or
Nick Pelly0b6955a2009-05-26 19:13:43 -0700104 * timeout
105 */
106 public BluetoothSocket accept(int timeout) throws IOException {
Nick Pelly71c3c782009-09-02 11:51:35 -0700107 return mSocket.accept(timeout);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700108 }
109
110 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700111 * Immediately close this socket, and release all associated resources.
112 * <p>Causes blocked calls on this socket in other threads to immediately
Nick Pelly0b6955a2009-05-26 19:13:43 -0700113 * throw an IOException.
Scott Main6d95fc02009-11-19 17:00:19 -0800114 * <p>Closing the {@link BluetoothServerSocket} will <em>not</em>
115 * close any {@link BluetoothSocket} received from {@link #accept()}.
Nick Pelly0b6955a2009-05-26 19:13:43 -0700116 */
117 public void close() throws IOException {
Nick Pelly24bb9b82009-10-02 20:34:18 -0700118 synchronized (this) {
119 if (mHandler != null) {
120 mHandler.obtainMessage(mMessage).sendToTarget();
121 }
122 }
Nick Pelly71c3c782009-09-02 11:51:35 -0700123 mSocket.close();
Nick Pelly0b6955a2009-05-26 19:13:43 -0700124 }
Nick Pelly24bb9b82009-10-02 20:34:18 -0700125
126 /*package*/ synchronized void setCloseHandler(Handler handler, int message) {
127 mHandler = handler;
128 mMessage = message;
129 }
Ben Dodson6d8b80d2011-07-08 14:36:42 -0700130
131 /**
132 * Returns the channel on which this socket is bound.
133 * @hide
134 */
135 public int getChannel() {
136 return mChannel;
137 }
Nick Pelly0b6955a2009-05-26 19:13:43 -0700138}