From e671840be397d266419e8a3fb47c08387f58998d Mon Sep 17 00:00:00 2001 From: tadvana Date: Wed, 26 Jan 2022 10:35:38 -0800 Subject: Introduces mechanism for background rfcomm servers This change adds a mechanism in the AdapterService which allows it to register RFCOMM listeners requested by a BluetoothAdapter. This is so that applications can request the framework to listen for incoming RFCOMM connections in the background. Apps can request that the bluetooth manager, via a BluetoothAdapter, listen on a specific service record, and then transact incoming socket connections after receiving a notification of availability via a PendingIntent. Tag: #feature Test: Run the CTS Bluetooth Rfcomm Handoff service Test. This requires two devices. Bug: 186494155 Ignore-AOSP-First: Need to commit to downstream first in order to resolve a merge conflict. Change-Id: Ia0c71969e691e6353f22fe3b7dae4a7500230e03 Merged-In: Ia0c71969e691e6353f22fe3b7dae4a7500230e03 --- .../java/android/bluetooth/BluetoothSocket.java | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'framework/java/android/bluetooth/BluetoothSocket.java') diff --git a/framework/java/android/bluetooth/BluetoothSocket.java b/framework/java/android/bluetooth/BluetoothSocket.java index 9d58447607..032f9df4f7 100644 --- a/framework/java/android/bluetooth/BluetoothSocket.java +++ b/framework/java/android/bluetooth/BluetoothSocket.java @@ -236,6 +236,33 @@ public final class BluetoothSocket implements Closeable { mOutputStream = new BluetoothOutputStream(this); } + /** + * Creates a BluetoothSocket from a {@link ParcelFileDescriptor}. This is used for when the + * underlying mPfd is transferred to a separate process (e.g. over a binder), and the socket + * must be reconstructed. + *

+ * The socket should already be connected in this case, so {@link #connect()} should not be + * called. + * + * @param pfd is the {@link ParcelFileDescriptor} for an already connected BluetoothSocket + * @param device is the remote {@link BluetoothDevice} that this socket is connected to + * @param uuid is the service ID that this RFCOMM connection is using + * @throws IOException if socket creation fails. + */ + /*package*/ static BluetoothSocket createSocketFromOpenFd( + ParcelFileDescriptor pfd, BluetoothDevice device, ParcelUuid uuid) throws IOException { + BluetoothSocket bluetoothSocket = + new BluetoothSocket(TYPE_RFCOMM, pfd.getFd(), true, true, device, -1, uuid); + + bluetoothSocket.mPfd = pfd; + bluetoothSocket.mSocket = new LocalSocket(pfd.getFileDescriptor()); + bluetoothSocket.mSocketIS = bluetoothSocket.mSocket.getInputStream(); + bluetoothSocket.mSocketOS = bluetoothSocket.mSocket.getOutputStream(); + bluetoothSocket.mSocketState = SocketState.CONNECTED; + + return bluetoothSocket; + } + private BluetoothSocket(BluetoothSocket s) { if (VDBG) Log.d(TAG, "Creating new Private BluetoothSocket of type: " + s.mType); mUuid = s.mUuid; @@ -718,6 +745,11 @@ public final class BluetoothSocket implements Closeable { } } + /** @hide */ + public ParcelFileDescriptor getParcelFileDescriptor() { + return mPfd; + } + private String convertAddr(final byte[] addr) { return String.format(Locale.US, "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); -- cgit v1.2.3