BluetoothServerSocket.java revision 0b6955a48bad9aee01ae2f0c06d3f168ca603ab7
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 implementing SCO and L2CAP sockets. 31 * TODO: Clean up javadoc grammer and formatting. 32 * TODO: Remove @hide 33 * @hide 34 */ 35public final class BluetoothServerSocket implements Closeable { 36 private final BluetoothSocket mSocket; 37 38 /** 39 * Construct a listening, secure RFCOMM server socket. 40 * The remote device connecting to this socket will be authenticated and 41 * communication on this socket will be encrypted. 42 * Call #accept to retrieve connections to this socket. 43 * @return An RFCOMM BluetoothServerSocket 44 * @throws IOException On error, for example Bluetooth not available, or 45 * insufficient permissions. 46 */ 47 public static BluetoothServerSocket listenUsingRfcommOn(int port) throws IOException { 48 BluetoothServerSocket socket = new BluetoothServerSocket(true, true); 49 try { 50 socket.mSocket.bindListenNative(port); 51 } catch (IOException e) { 52 try { 53 socket.close(); 54 } catch (IOException e2) { } 55 throw e; 56 } 57 return socket; 58 } 59 60 /** 61 * Construct an unencrypted, unauthenticated, RFCOMM server socket. 62 * Call #accept to retrieve connections to this socket. 63 * @return An RFCOMM BluetoothServerSocket 64 * @throws IOException On error, for example Bluetooth not available, or 65 * insufficient permissions. 66 */ 67 public static BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException { 68 BluetoothServerSocket socket = new BluetoothServerSocket(false, false); 69 try { 70 socket.mSocket.bindListenNative(port); 71 } catch (IOException e) { 72 try { 73 socket.close(); 74 } catch (IOException e2) { } 75 throw e; 76 } 77 return socket; 78 } 79 80 /** 81 * Construct a socket for incoming connections. 82 * @param auth Require the remote device to be authenticated 83 * @param encrypt Require the connection to be encrypted 84 * @throws IOException On error, for example Bluetooth not available, or 85 * insufficient priveleges 86 */ 87 private BluetoothServerSocket(boolean auth, boolean encrypt) throws IOException { 88 mSocket = new BluetoothSocket(-1, auth, encrypt, null, -1); 89 } 90 91 /** 92 * Block until a connection is established. 93 * Returns a connected #BluetoothSocket. This server socket can be reused 94 * for subsequent incoming connections by calling #accept repeatedly. 95 * #close can be used to abort this call from another thread. 96 * @return A connected #BluetoothSocket 97 * @throws IOException On error, for example this call was aborted 98 */ 99 public BluetoothSocket accept() throws IOException { 100 return accept(-1); 101 } 102 103 /** 104 * Block until a connection is established, with timeout. 105 * Returns a connected #BluetoothSocket. This server socket can be reused 106 * for subsequent incoming connections by calling #accept repeatedly. 107 * #close can be used to abort this call from another thread. 108 * @return A connected #BluetoothSocket 109 * @throws IOException On error, for example this call was aborted, or 110 * timeout 111 */ 112 public BluetoothSocket accept(int timeout) throws IOException { 113 return mSocket.acceptNative(timeout); 114 } 115 116 /** 117 * Closes this socket. 118 * This will cause other blocking calls on this socket to immediately 119 * throw an IOException. 120 */ 121 public void close() throws IOException { 122 mSocket.closeNative(); 123 } 124} 125