Home | History | Annotate | Download | only in bluetooth
      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 
     17 package android.bluetooth;
     18 
     19 import android.os.Handler;
     20 import android.os.Message;
     21 import android.os.ParcelUuid;
     22 
     23 import java.io.Closeable;
     24 import java.io.IOException;
     25 
     26 /**
     27  * A listening Bluetooth socket.
     28  *
     29  * <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
     32  * socket. When a connection is accepted by the {@link BluetoothServerSocket},
     33  * it will return a new {@link BluetoothSocket} to manage the connection.
     34  * On the client side, use a single {@link BluetoothSocket} to both initiate
     35  * an outgoing connection and to manage the connection.
     36  *
     37  * <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).
     40  *
     41  * <p>To create a listening {@link BluetoothServerSocket} that's ready for
     42  * 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
     47  * 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}.
     52  *
     53  * <p>{@link BluetoothServerSocket} is thread
     54  * safe. In particular, {@link #close} will always immediately abort ongoing
     55  * operations and close the server socket.
     56  *
     57  * <p class="note"><strong>Note:</strong>
     58  * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
     59  *
     60  * <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  *
     66  * {@see BluetoothSocket}
     67  */
     68 public final class BluetoothServerSocket implements Closeable {
     69 
     70     /*package*/ final BluetoothSocket mSocket;
     71     private Handler mHandler;
     72     private int mMessage;
     73     private final int mChannel;
     74 
     75     /**
     76      * Construct a socket for incoming connections.
     77      * @param type    type of socket
     78      * @param auth    require the remote device to be authenticated
     79      * @param encrypt require the connection to be encrypted
     80      * @param port    remote port
     81      * @throws IOException On error, for example Bluetooth not available, or
     82      *                     insufficient privileges
     83      */
     84     /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
     85             throws IOException {
     86         mChannel = port;
     87         mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null);
     88     }
     89 
     90     /**
     91      * Construct a socket for incoming connections.
     92      * @param type    type of socket
     93      * @param auth    require the remote device to be authenticated
     94      * @param encrypt require the connection to be encrypted
     95      * @param uuid    uuid
     96      * @throws IOException On error, for example Bluetooth not available, or
     97      *                     insufficient privileges
     98      */
     99     /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid)
    100             throws IOException {
    101         mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, -1, uuid);
    102         mChannel = mSocket.getPort();
    103     }
    104 
    105 
    106     /**
    107      * Block until a connection is established.
    108      * <p>Returns a connected {@link BluetoothSocket} on successful connection.
    109      * <p>Once this call returns, it can be called again to accept subsequent
    110      * incoming connections.
    111      * <p>{@link #close} can be used to abort this call from another thread.
    112      * @return a connected {@link BluetoothSocket}
    113      * @throws IOException on error, for example this call was aborted, or
    114      *                     timeout
    115      */
    116     public BluetoothSocket accept() throws IOException {
    117         return accept(-1);
    118     }
    119 
    120     /**
    121      * Block until a connection is established, with timeout.
    122      * <p>Returns a connected {@link BluetoothSocket} on successful connection.
    123      * <p>Once this call returns, it can be called again to accept subsequent
    124      * incoming connections.
    125      * <p>{@link #close} can be used to abort this call from another thread.
    126      * @return a connected {@link BluetoothSocket}
    127      * @throws IOException on error, for example this call was aborted, or
    128      *                     timeout
    129      */
    130     public BluetoothSocket accept(int timeout) throws IOException {
    131         return mSocket.accept(timeout);
    132     }
    133 
    134     /**
    135      * Immediately close this socket, and release all associated resources.
    136      * <p>Causes blocked calls on this socket in other threads to immediately
    137      * throw an IOException.
    138      * <p>Closing the {@link BluetoothServerSocket} will <em>not</em>
    139      * close any {@link BluetoothSocket} received from {@link #accept()}.
    140      */
    141     public void close() throws IOException {
    142         synchronized (this) {
    143             if (mHandler != null) {
    144                 mHandler.obtainMessage(mMessage).sendToTarget();
    145             }
    146         }
    147         mSocket.close();
    148     }
    149 
    150     /*package*/ synchronized void setCloseHandler(Handler handler, int message) {
    151         mHandler = handler;
    152         mMessage = message;
    153     }
    154     /*package*/ void setServiceName(String ServiceName) {
    155         mSocket.setServiceName(ServiceName);
    156     }
    157     /**
    158      * Returns the channel on which this socket is bound.
    159      * @hide
    160      */
    161     public int getChannel() {
    162         return mChannel;
    163     }
    164 }
    165