diff options
author | Nick Pelly <npelly@google.com> | 2009-06-02 15:57:18 -0700 |
---|---|---|
committer | Nick Pelly <npelly@google.com> | 2009-06-02 16:05:09 -0700 |
commit | 6a669fac385b51b8bb01844b77a9a43840dda854 (patch) | |
tree | ae86e3e41162c08fa56b1f84f68a50fe52d0493a /core/java/android | |
parent | 47e82dee6b18c33fab8c2cdf4f68b20d3663079e (diff) | |
download | frameworks_base-6a669fac385b51b8bb01844b77a9a43840dda854.zip frameworks_base-6a669fac385b51b8bb01844b77a9a43840dda854.tar.gz frameworks_base-6a669fac385b51b8bb01844b77a9a43840dda854.tar.bz2 |
Implement and expose SCO socket support in BluetoothSocket.java.
Implement L2CAP socket support, but do not expose it (untested).
NEXT: Switch to Builder style constructor instead of factory method.
Diffstat (limited to 'core/java/android')
-rw-r--r-- | core/java/android/bluetooth/BluetoothServerSocket.java | 44 | ||||
-rw-r--r-- | core/java/android/bluetooth/BluetoothSocket.java | 53 |
2 files changed, 72 insertions, 25 deletions
diff --git a/core/java/android/bluetooth/BluetoothServerSocket.java b/core/java/android/bluetooth/BluetoothServerSocket.java index ca46701..f3baeab 100644 --- a/core/java/android/bluetooth/BluetoothServerSocket.java +++ b/core/java/android/bluetooth/BluetoothServerSocket.java @@ -27,7 +27,7 @@ import java.io.IOException; * RFCOMM is a connection orientated, streaming transport over Bluetooth. It is * also known as the Serial Port Profile (SPP). * - * TODO: Consider implementing SCO and L2CAP sockets. + * TODO: Consider exposing L2CAP sockets. * TODO: Clean up javadoc grammer and formatting. * TODO: Remove @hide * @hide @@ -45,9 +45,10 @@ public final class BluetoothServerSocket implements Closeable { * insufficient permissions. */ public static BluetoothServerSocket listenUsingRfcommOn(int port) throws IOException { - BluetoothServerSocket socket = new BluetoothServerSocket(true, true); + BluetoothServerSocket socket = new BluetoothServerSocket( + BluetoothSocket.TYPE_RFCOMM, true, true, port); try { - socket.mSocket.bindListenNative(port); + socket.mSocket.bindListenNative(); } catch (IOException e) { try { socket.close(); @@ -65,9 +66,31 @@ public final class BluetoothServerSocket implements Closeable { * insufficient permissions. */ public static BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException { - BluetoothServerSocket socket = new BluetoothServerSocket(false, false); + BluetoothServerSocket socket = new BluetoothServerSocket( + BluetoothSocket.TYPE_RFCOMM, false, false, port); try { - socket.mSocket.bindListenNative(port); + socket.mSocket.bindListenNative(); + } catch (IOException e) { + try { + socket.close(); + } catch (IOException e2) { } + throw e; + } + return socket; + } + + /** + * Construct a SCO server socket. + * Call #accept to retrieve connections to this socket. + * @return A SCO BluetoothServerSocket + * @throws IOException On error, for example Bluetooth not available, or + * insufficient permissions. + */ + public static BluetoothServerSocket listenUsingScoOn() throws IOException { + BluetoothServerSocket socket = new BluetoothServerSocket( + BluetoothSocket.TYPE_SCO, false, false, -1); + try { + socket.mSocket.bindListenNative(); } catch (IOException e) { try { socket.close(); @@ -79,13 +102,16 @@ public final class BluetoothServerSocket implements Closeable { /** * Construct a socket for incoming connections. - * @param auth Require the remote device to be authenticated - * @param encrypt Require the connection to be encrypted + * @param type type of socket + * @param auth require the remote device to be authenticated + * @param encrypt require the connection to be encrypted + * @param port remote port * @throws IOException On error, for example Bluetooth not available, or * insufficient priveleges */ - private BluetoothServerSocket(boolean auth, boolean encrypt) throws IOException { - mSocket = new BluetoothSocket(-1, auth, encrypt, null, -1); + private BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port) + throws IOException { + mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port); } /** diff --git a/core/java/android/bluetooth/BluetoothSocket.java b/core/java/android/bluetooth/BluetoothSocket.java index 670146b..de1f326 100644 --- a/core/java/android/bluetooth/BluetoothSocket.java +++ b/core/java/android/bluetooth/BluetoothSocket.java @@ -29,13 +29,19 @@ import java.io.OutputStream; * RFCOMM is a connection orientated, streaming transport over Bluetooth. It is * also known as the Serial Port Profile (SPP). * - * TODO: Consider implementing SCO and L2CAP sockets. + * TODO: Consider exposing L2CAP sockets. * TODO: Clean up javadoc grammer and formatting. * TODO: Remove @hide * @hide */ public final class BluetoothSocket implements Closeable { - private final int mPort; + /** Keep TYPE_RFCOMM etc in sync with BluetoothSocket.cpp */ + /*package*/ static final int TYPE_RFCOMM = 1; + /*package*/ static final int TYPE_SCO = 2; + /*package*/ static final int TYPE_L2CAP = 3; + + private final int mType; /* one of TYPE_RFCOMM etc */ + private final int mPort; /* RFCOMM channel or L2CAP psm */ private final String mAddress; /* remote address */ private final boolean mAuth; private final boolean mEncrypt; @@ -57,7 +63,7 @@ public final class BluetoothSocket implements Closeable { */ public static BluetoothSocket createRfcommSocket(String address, int port) throws IOException { - return new BluetoothSocket(-1, true, true, address, port); + return new BluetoothSocket(TYPE_RFCOMM, -1, true, true, address, port); } /** @@ -74,11 +80,25 @@ public final class BluetoothSocket implements Closeable { */ public static BluetoothSocket createInsecureRfcommSocket(String address, int port) throws IOException { - return new BluetoothSocket(-1, false, false, address, port); + return new BluetoothSocket(TYPE_RFCOMM, -1, false, false, address, port); + } + + /** + * Construct a SCO socket ready to start an outgoing connection. + * Call #connect on the returned #BluetoothSocket to begin the connection. + * @param address remote Bluetooth address that this socket can connect to + * @return a SCO BluetoothSocket + * @throws IOException on error, for example Bluetooth not available, or + * insufficient permissions. + */ + public static BluetoothSocket createScoSocket(String address, int port) + throws IOException { + return new BluetoothSocket(TYPE_SCO, -1, true, true, address, port); } /** * Construct a Bluetooth. + * @param type type of socket * @param fd fd to use for connected socket, or -1 for a new socket * @param auth require the remote device to be authenticated * @param encrypt require the connection to be encrypted @@ -87,8 +107,9 @@ public final class BluetoothSocket implements Closeable { * @throws IOException On error, for example Bluetooth not available, or * insufficient priveleges */ - /*package*/ BluetoothSocket(int fd, boolean auth, boolean encrypt, String address, int port) - throws IOException { + /*package*/ BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, String address, + int port) throws IOException { + mType = type; mAuth = auth; mEncrypt = encrypt; mAddress = address; @@ -120,7 +141,7 @@ public final class BluetoothSocket implements Closeable { * @throws IOException On error, for example connection failure */ public void connect() throws IOException { - connectNative(mAddress, mPort, -1); + connectNative(); } /** @@ -163,14 +184,14 @@ public final class BluetoothSocket implements Closeable { return mOutputStream; } - private native void initSocketNative(); - private native void initSocketFromFdNative(int fd); - private native void connectNative(String address, int port, int timeout); - /*package*/ native void bindListenNative(int port) throws IOException; + private native void initSocketNative() throws IOException; + private native void initSocketFromFdNative(int fd) throws IOException; + private native void connectNative() throws IOException; + /*package*/ native void bindListenNative() throws IOException; /*package*/ native BluetoothSocket acceptNative(int timeout) throws IOException; - /*package*/ native int availableNative(); - /*package*/ native int readNative(byte[] b, int offset, int length); - /*package*/ native int writeNative(byte[] b, int offset, int length); - /*package*/ native void closeNative(); - private native void destroyNative(); + /*package*/ native int availableNative() throws IOException; + /*package*/ native int readNative(byte[] b, int offset, int length) throws IOException; + /*package*/ native int writeNative(byte[] b, int offset, int length) throws IOException; + /*package*/ native void closeNative() throws IOException; + private native void destroyNative() throws IOException; } |