From ef3e0dbf003b881ded3dd8993f136fffc2d3b069 Mon Sep 17 00:00:00 2001 From: Hemant Gupta Date: Fri, 3 Feb 2017 16:38:59 +0530 Subject: [PATCH] Bluetooth: Expose L2CAP API to support OPP 1.2 Add changes to expose L2CAP API to create an insecure L2CAP socket for supporting OPP 1.2. Test: Connect with Remote OPP Client supporting OPP 1.2 and verify that connection and transfer happens over L2CAP. Connect with Remote OPP Client supporting OPP 1.1 and verify that connection and transfer happens over RFCOMM. Bug: 33010988 Change-Id: I21ed672afb4ed5d2355ff0a0f9691af220921c1f --- core/java/android/bluetooth/BluetoothAdapter.java | 29 +++++++++++++++++++++++ core/java/android/bluetooth/BluetoothDevice.java | 21 ++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 9302cbc480e1..dbc25afcd08f 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -1858,6 +1858,35 @@ public final class BluetoothAdapter { return listenUsingL2capOn(port, false, false); } + + /** + * Construct an insecure L2CAP server socket. + * Call #accept to retrieve connections to this socket. + *

To auto assign a port without creating a SDP record use + * {@link SOCKET_CHANNEL_AUTO_STATIC_NO_SDP} as port number. + * @param port the PSM to listen on + * @return An L2CAP BluetoothServerSocket + * @throws IOException On error, for example Bluetooth not available, or + * insufficient permissions. + * @hide + */ + public BluetoothServerSocket listenUsingInsecureL2capOn(int port) throws IOException { + BluetoothServerSocket socket = new BluetoothServerSocket( + BluetoothSocket.TYPE_L2CAP, false, false, port, false, false); + int errno = socket.mSocket.bindListen(); + if(port == SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { + socket.setChannel(socket.mSocket.getPort()); + } + if (errno != 0) { + //TODO(BT): Throw the same exception error code + // that the previous code was using. + //socket.mSocket.throwErrnoNative(errno); + throw new IOException("Error: " + errno); + } + return socket; + + } + /** * Read the local Out of Band Pairing Data *

Requires {@link android.Manifest.permission#BLUETOOTH} diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java index 5c9e2ee4fc42..7b1e687f6865 100644 --- a/core/java/android/bluetooth/BluetoothDevice.java +++ b/core/java/android/bluetooth/BluetoothDevice.java @@ -1412,6 +1412,27 @@ public final class BluetoothDevice implements Parcelable { } /** + * Create an L2cap {@link BluetoothSocket} ready to start an insecure + * outgoing connection to this remote device on given channel. + *

The remote device will be not authenticated and communication on this + * socket will not be encrypted. + *

Use {@link BluetoothSocket#connect} to initiate the outgoing + * connection. + *

Valid L2CAP PSM channels are in range 1 to 2^16. + *

Requires {@link android.Manifest.permission#BLUETOOTH} + * + * @param channel L2cap PSM/channel to connect to + * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection + * @throws IOException on error, for example Bluetooth not available, or + * insufficient permissions + * @hide + */ + public BluetoothSocket createInsecureL2capSocket(int channel) throws IOException { + return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, false, false, this, channel, + null); + } + + /** * Create an RFCOMM {@link BluetoothSocket} ready to start a secure * outgoing connection to this remote device using SDP lookup of uuid. *

This is designed to be used with {@link -- 2.11.0