BluetoothServerSocket.java revision 6a669fac385b51b8bb01844b77a9a43840dda854
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
17package android.bluetooth;
18
19import java.io.Closeable;
20import java.io.IOException;
21
22/**
23 * Server (listening) Bluetooth Socket.
24 *
25 * Currently only supports RFCOMM sockets.
26 *
27 * RFCOMM is a connection orientated, streaming transport over Bluetooth. It is
28 * also known as the Serial Port Profile (SPP).
29 *
30 * TODO: Consider exposing L2CAP sockets.
31 * TODO: Clean up javadoc grammer and formatting.
32 * TODO: Remove @hide
33 * @hide
34 */
35public final class BluetoothServerSocket implements Closeable {
36    private final BluetoothSocket mSocket;
37
38    /**
39     * Construct a listening, secure RFCOMM server socket.
40     * The remote device connecting to this socket will be authenticated and
41     * communication on this socket will be encrypted.
42     * Call #accept to retrieve connections to this socket.
43     * @return An RFCOMM BluetoothServerSocket
44     * @throws IOException On error, for example Bluetooth not available, or
45     *                     insufficient permissions.
46     */
47    public static BluetoothServerSocket listenUsingRfcommOn(int port) throws IOException {
48        BluetoothServerSocket socket = new BluetoothServerSocket(
49                BluetoothSocket.TYPE_RFCOMM, true, true, port);
50        try {
51            socket.mSocket.bindListenNative();
52        } catch (IOException e) {
53            try {
54                socket.close();
55            } catch (IOException e2) { }
56            throw e;
57        }
58        return socket;
59    }
60
61    /**
62     * Construct an unencrypted, unauthenticated, RFCOMM server socket.
63     * Call #accept to retrieve connections to this socket.
64     * @return An RFCOMM BluetoothServerSocket
65     * @throws IOException On error, for example Bluetooth not available, or
66     *                     insufficient permissions.
67     */
68    public static BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException {
69        BluetoothServerSocket socket = new BluetoothServerSocket(
70                BluetoothSocket.TYPE_RFCOMM, false, false, port);
71        try {
72            socket.mSocket.bindListenNative();
73        } catch (IOException e) {
74            try {
75                socket.close();
76            } catch (IOException e2) { }
77            throw e;
78        }
79        return socket;
80    }
81
82    /**
83     * Construct a SCO server socket.
84     * Call #accept to retrieve connections to this socket.
85     * @return A SCO BluetoothServerSocket
86     * @throws IOException On error, for example Bluetooth not available, or
87     *                     insufficient permissions.
88     */
89    public static BluetoothServerSocket listenUsingScoOn() throws IOException {
90        BluetoothServerSocket socket = new BluetoothServerSocket(
91                BluetoothSocket.TYPE_SCO, false, false, -1);
92        try {
93            socket.mSocket.bindListenNative();
94        } catch (IOException e) {
95            try {
96                socket.close();
97            } catch (IOException e2) { }
98            throw e;
99        }
100        return socket;
101    }
102
103    /**
104     * Construct a socket for incoming connections.
105     * @param type    type of socket
106     * @param auth    require the remote device to be authenticated
107     * @param encrypt require the connection to be encrypted
108     * @param port    remote port
109     * @throws IOException On error, for example Bluetooth not available, or
110     *                     insufficient priveleges
111     */
112    private BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
113            throws IOException {
114        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port);
115    }
116
117    /**
118     * Block until a connection is established.
119     * Returns a connected #BluetoothSocket. This server socket can be reused
120     * for subsequent incoming connections by calling #accept repeatedly.
121     * #close can be used to abort this call from another thread.
122     * @return A connected #BluetoothSocket
123     * @throws IOException On error, for example this call was aborted
124     */
125    public BluetoothSocket accept() throws IOException {
126        return accept(-1);
127    }
128
129    /**
130     * Block until a connection is established, with timeout.
131     * Returns a connected #BluetoothSocket. This server socket can be reused
132     * for subsequent incoming connections by calling #accept repeatedly.
133     * #close can be used to abort this call from another thread.
134     * @return A connected #BluetoothSocket
135     * @throws IOException On error, for example this call was aborted, or
136     *                     timeout
137     */
138    public BluetoothSocket accept(int timeout) throws IOException {
139        return mSocket.acceptNative(timeout);
140    }
141
142    /**
143     * Closes this socket.
144     * This will cause other blocking calls on this socket to immediately
145     * throw an IOException.
146     */
147    public void close() throws IOException {
148        mSocket.closeNative();
149    }
150}
151