BluetoothServerSocket.java revision 45e2704ff512d41e22af2801d76e96955469ce8d
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 * A listening Bluetooth socket. 24 * 25 * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets: 26 * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server 27 * side, use a {@link BluetoothServerSocket} to create a listening server 28 * socket. It will return a new, connected {@link BluetoothSocket} on an 29 * accepted connection. On the client side, use the same 30 * {@link BluetoothSocket} object to both intiate the outgoing connection, 31 * and to manage the connected socket. 32 * 33 * <p>The most common type of Bluetooth Socket is RFCOMM. RFCOMM is a 34 * connection orientated, streaming transport over Bluetooth. It is also known 35 * as the Serial Port Profile (SPP). 36 * 37 * <p>Use {@link BluetoothDevice#createRfcommSocket} to create a new {@link 38 * BluetoothSocket} ready for an outgoing connection to a remote 39 * {@link BluetoothDevice}. 40 * 41 * <p>Use {@link BluetoothAdapter#listenUsingRfcommOn} to create a listening 42 * {@link BluetoothServerSocket} ready for incoming connections to the local 43 * {@link BluetoothAdapter}. 44 * 45 * <p>{@link BluetoothSocket} and {@link BluetoothServerSocket} are thread 46 * safe. In particular, {@link #close} will always immediately abort ongoing 47 * operations and close the socket. 48 */ 49public final class BluetoothServerSocket implements Closeable { 50 /*package*/ final BluetoothSocket mSocket; 51 52 /** 53 * Construct a socket for incoming connections. 54 * @param type type of socket 55 * @param auth require the remote device to be authenticated 56 * @param encrypt require the connection to be encrypted 57 * @param port remote port 58 * @throws IOException On error, for example Bluetooth not available, or 59 * insufficient priveleges 60 */ 61 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port) 62 throws IOException { 63 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port); 64 } 65 66 /** 67 * Block until a connection is established. 68 * <p>Returns a connected {@link BluetoothSocket} on successful connection. 69 * <p>Once this call returns, it can be called again to accept subsequent 70 * incoming connections. 71 * <p>{@link #close} can be used to abort this call from another thread. 72 * @return a connected {@link BluetoothSocket} 73 * @throws IOException on error, for example this call was aborted, or 74 * timeout 75 */ 76 public BluetoothSocket accept() throws IOException { 77 return accept(-1); 78 } 79 80 /** 81 * Block until a connection is established, with timeout. 82 * <p>Returns a connected {@link BluetoothSocket} on successful connection. 83 * <p>Once this call returns, it can be called again to accept subsequent 84 * incoming connections. 85 * <p>{@link #close} can be used to abort this call from another thread. 86 * @return a connected {@link BluetoothSocket} 87 * @throws IOException on error, for example this call was aborted, or 88 * timeout 89 */ 90 public BluetoothSocket accept(int timeout) throws IOException { 91 return mSocket.acceptNative(timeout); 92 } 93 94 /** 95 * Immediately close this socket, and release all associated resources. 96 * <p>Causes blocked calls on this socket in other threads to immediately 97 * throw an IOException. 98 */ 99 public void close() throws IOException { 100 mSocket.closeNative(); 101 } 102} 103