BluetoothServerSocket.java revision cf44059813539bf7f36dabd278cef93ba3122c56
10b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly/*
20b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Copyright (C) 2009 The Android Open Source Project
30b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly *
40b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
50b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * you may not use this file except in compliance with the License.
60b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * You may obtain a copy of the License at
70b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly *
80b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
90b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly *
100b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * Unless required by applicable law or agreed to in writing, software
110b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
120b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * See the License for the specific language governing permissions and
140b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly * limitations under the License.
150b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */
160b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
170b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pellypackage android.bluetooth;
180b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
190b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pellyimport java.io.Closeable;
200b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pellyimport java.io.IOException;
210b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
220b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly/**
2345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * A listening Bluetooth socket.
240b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly *
2545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets:
2645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server
2745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * side, use a {@link BluetoothServerSocket} to create a listening server
2845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * socket. It will return a new, connected {@link BluetoothSocket} on an
2945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * accepted connection. On the client side, use the same
3045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * {@link BluetoothSocket} object to both intiate the outgoing connection,
3145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * and to manage the connected socket.
320b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly *
3345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>The most common type of Bluetooth Socket is RFCOMM. RFCOMM is a
3445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * connection orientated, streaming transport over Bluetooth. It is also known
3545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * as the Serial Port Profile (SPP).
360b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly *
3745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Use {@link BluetoothDevice#createRfcommSocket} to create a new {@link
3845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * BluetoothSocket} ready for an outgoing connection to a remote
3945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * {@link BluetoothDevice}.
4045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly *
4145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>Use {@link BluetoothAdapter#listenUsingRfcommOn} to create a listening
4245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * {@link BluetoothServerSocket} ready for incoming connections to the local
4345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * {@link BluetoothAdapter}.
4445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly *
4545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * <p>{@link BluetoothSocket} and {@link BluetoothServerSocket} are thread
4645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * safe. In particular, {@link #close} will always immediately abort ongoing
4745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly * operations and close the socket.
48cf44059813539bf7f36dabd278cef93ba3122c56Nick Pelly *
49cf44059813539bf7f36dabd278cef93ba3122c56Nick Pelly * <p>All methods on a {@link BluetoothServerSocket} require
50cf44059813539bf7f36dabd278cef93ba3122c56Nick Pelly * {@link android.Manifest.permission#BLUETOOTH}
510b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly */
520b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pellypublic final class BluetoothServerSocket implements Closeable {
5371c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly
54bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /*package*/ final BluetoothSocket mSocket;
550b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
560b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    /**
570b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * Construct a socket for incoming connections.
586a669fac385b51b8bb01844b77a9a43840dda854Nick Pelly     * @param type    type of socket
596a669fac385b51b8bb01844b77a9a43840dda854Nick Pelly     * @param auth    require the remote device to be authenticated
606a669fac385b51b8bb01844b77a9a43840dda854Nick Pelly     * @param encrypt require the connection to be encrypted
616a669fac385b51b8bb01844b77a9a43840dda854Nick Pelly     * @param port    remote port
620b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * @throws IOException On error, for example Bluetooth not available, or
630b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     *                     insufficient priveleges
640b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     */
65bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
666a669fac385b51b8bb01844b77a9a43840dda854Nick Pelly            throws IOException {
676a669fac385b51b8bb01844b77a9a43840dda854Nick Pelly        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port);
680b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    }
690b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
700b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    /**
710b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * Block until a connection is established.
7245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>Returns a connected {@link BluetoothSocket} on successful connection.
7345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>Once this call returns, it can be called again to accept subsequent
7445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * incoming connections.
7545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>{@link #close} can be used to abort this call from another thread.
7645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @return a connected {@link BluetoothSocket}
7745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @throws IOException on error, for example this call was aborted, or
7845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     *                     timeout
790b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     */
800b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    public BluetoothSocket accept() throws IOException {
810b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly        return accept(-1);
820b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    }
830b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
840b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    /**
850b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * Block until a connection is established, with timeout.
8645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>Returns a connected {@link BluetoothSocket} on successful connection.
8745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>Once this call returns, it can be called again to accept subsequent
8845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * incoming connections.
8945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>{@link #close} can be used to abort this call from another thread.
9045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @return a connected {@link BluetoothSocket}
9145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @throws IOException on error, for example this call was aborted, or
920b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     *                     timeout
930b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     */
940b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    public BluetoothSocket accept(int timeout) throws IOException {
9571c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly        return mSocket.accept(timeout);
960b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    }
970b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly
980b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    /**
9945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * Immediately close this socket, and release all associated resources.
10045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>Causes blocked calls on this socket in other threads to immediately
1010b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     * throw an IOException.
1020b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly     */
1030b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    public void close() throws IOException {
10471c3c7806acb2b2b7b8441817c26a2101d447bbeNick Pelly        mSocket.close();
1050b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly    }
1060b6955a48bad9aee01ae2f0c06d3f168ca603ab7Nick Pelly}
107