BluetoothServerSocket.java revision bd022f423a33f0794bb53e5b0720da2d67e4631c
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    /*package*/ final BluetoothSocket mSocket;
37
38    /**
39     * Construct a socket for incoming connections.
40     * @param type    type of socket
41     * @param auth    require the remote device to be authenticated
42     * @param encrypt require the connection to be encrypted
43     * @param port    remote port
44     * @throws IOException On error, for example Bluetooth not available, or
45     *                     insufficient priveleges
46     */
47    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
48            throws IOException {
49        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port);
50    }
51
52    /**
53     * Block until a connection is established.
54     * Returns a connected #BluetoothSocket. This server socket can be reused
55     * for subsequent incoming connections by calling #accept repeatedly.
56     * #close can be used to abort this call from another thread.
57     * @return A connected #BluetoothSocket
58     * @throws IOException On error, for example this call was aborted
59     */
60    public BluetoothSocket accept() throws IOException {
61        return accept(-1);
62    }
63
64    /**
65     * Block until a connection is established, with timeout.
66     * Returns a connected #BluetoothSocket. This server socket can be reused
67     * for subsequent incoming connections by calling #accept repeatedly.
68     * #close can be used to abort this call from another thread.
69     * @return A connected #BluetoothSocket
70     * @throws IOException On error, for example this call was aborted, or
71     *                     timeout
72     */
73    public BluetoothSocket accept(int timeout) throws IOException {
74        return mSocket.acceptNative(timeout);
75    }
76
77    /**
78     * Closes this socket.
79     * This will cause other blocking calls on this socket to immediately
80     * throw an IOException.
81     */
82    public void close() throws IOException {
83        mSocket.closeNative();
84    }
85}
86