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 21 import java.io.Closeable; 22 import java.io.IOException; 23 24 /** 25 * A listening Bluetooth socket. 26 * 27 * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets: 28 * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server 29 * side, use a {@link BluetoothServerSocket} to create a listening server 30 * socket. When a connection is accepted by the {@link BluetoothServerSocket}, 31 * it will return a new {@link BluetoothSocket} to manage the connection. 32 * On the client side, use a single {@link BluetoothSocket} to both intiate 33 * an outgoing connection and to manage the connection. 34 * 35 * <p>The most common type of Bluetooth socket is RFCOMM, which is the type 36 * supported by the Android APIs. RFCOMM is a connection-oriented, streaming 37 * transport over Bluetooth. It is also known as the Serial Port Profile (SPP). 38 * 39 * <p>To create a listenting {@link BluetoothServerSocket} that's ready for 40 * incoming connections, use 41 * {@link BluetoothAdapter#listenUsingRfcommWithServiceRecord 42 * BluetoothAdapter.listenUsingRfcommWithServiceRecord()}. Then call 43 * {@link #accept()} to listen for incoming connection requests. This call 44 * will block until a connection is established, at which point, it will return 45 * a {@link BluetoothSocket} to manage the connection. Once the {@link 46 * BluetoothSocket} is acquired, it's a good idea to call {@link #close()} on 47 * the {@link BluetoothServerSocket} when it's no longer needed for accepting 48 * connections. Closing the {@link BluetoothServerSocket} will <em>not</em> 49 * close the returned {@link BluetoothSocket}. 50 * 51 * <p>{@link BluetoothServerSocket} is thread 52 * safe. In particular, {@link #close} will always immediately abort ongoing 53 * operations and close the server socket. 54 * 55 * <p class="note"><strong>Note:</strong> 56 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission. 57 * 58 * {@see BluetoothSocket} 59 */ 60 public final class BluetoothServerSocket implements Closeable { 61 62 /*package*/ final BluetoothSocket mSocket; 63 private Handler mHandler; 64 private int mMessage; 65 66 /** 67 * Construct a socket for incoming connections. 68 * @param type type of socket 69 * @param auth require the remote device to be authenticated 70 * @param encrypt require the connection to be encrypted 71 * @param port remote port 72 * @throws IOException On error, for example Bluetooth not available, or 73 * insufficient priveleges 74 */ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)75 /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port) 76 throws IOException { 77 mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null); 78 } 79 80 /** 81 * Block until a connection is established. 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 */ accept()90 public BluetoothSocket accept() throws IOException { 91 return accept(-1); 92 } 93 94 /** 95 * Block until a connection is established, with timeout. 96 * <p>Returns a connected {@link BluetoothSocket} on successful connection. 97 * <p>Once this call returns, it can be called again to accept subsequent 98 * incoming connections. 99 * <p>{@link #close} can be used to abort this call from another thread. 100 * @return a connected {@link BluetoothSocket} 101 * @throws IOException on error, for example this call was aborted, or 102 * timeout 103 */ accept(int timeout)104 public BluetoothSocket accept(int timeout) throws IOException { 105 return mSocket.accept(timeout); 106 } 107 108 /** 109 * Immediately close this socket, and release all associated resources. 110 * <p>Causes blocked calls on this socket in other threads to immediately 111 * throw an IOException. 112 * <p>Closing the {@link BluetoothServerSocket} will <em>not</em> 113 * close any {@link BluetoothSocket} received from {@link #accept()}. 114 */ close()115 public void close() throws IOException { 116 synchronized (this) { 117 if (mHandler != null) { 118 mHandler.obtainMessage(mMessage).sendToTarget(); 119 } 120 } 121 mSocket.close(); 122 } 123 setCloseHandler(Handler handler, int message)124 /*package*/ synchronized void setCloseHandler(Handler handler, int message) { 125 mHandler = handler; 126 mMessage = message; 127 } 128 } 129