summaryrefslogtreecommitdiff
path: root/framework/java/android/bluetooth/BluetoothSocket.java
diff options
context:
space:
mode:
authorWilliam Escande <wescande@google.com>2022-01-10 19:13:28 +0100
committerWilliam Escande <wescande@google.com>2022-01-28 02:44:28 +0100
commit335de95b1764401962bce6f0d9ae237078cf25b1 (patch)
treec62e223ca86d044b5d5278fe6efe68050781b9ab /framework/java/android/bluetooth/BluetoothSocket.java
parentaa63bd2617a90f4d83724802b024a97e3a623709 (diff)
Remove allowBlocking from BluetoothManagerService
Since Bluetooth is becoming a mainline module, it can no longer call the allowBlocking hidden api. Instead, corresponding aidl are moved to be oneway and use a synchronous data to handle the return value. also: aosp/1927380 for similar work on all bluetooth profiles BluetoothGatt will be done next Bug: 211851706 Test: build + start bt Tag: #refactor Ignore-AOSP-First: merge conflict resolution Change-Id: I715cc2d8f2ae42be6d1099cd73095d37fa9e30f4
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothSocket.java')
-rw-r--r--framework/java/android/bluetooth/BluetoothSocket.java47
1 files changed, 36 insertions, 11 deletions
diff --git a/framework/java/android/bluetooth/BluetoothSocket.java b/framework/java/android/bluetooth/BluetoothSocket.java
index 032f9df4f7..7b5d2fe24b 100644
--- a/framework/java/android/bluetooth/BluetoothSocket.java
+++ b/framework/java/android/bluetooth/BluetoothSocket.java
@@ -16,6 +16,8 @@
package android.bluetooth;
+import static android.bluetooth.BluetoothUtils.getSyncTimeout;
+
import android.annotation.RequiresNoPermission;
import android.annotation.RequiresPermission;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
@@ -27,6 +29,8 @@ import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;
+import com.android.modules.utils.SynchronousResultReceiver;
+
import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.IOException;
@@ -37,6 +41,7 @@ import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Locale;
import java.util.UUID;
+import java.util.concurrent.TimeoutException;
/**
* A connected or connecting Bluetooth socket.
@@ -427,8 +432,13 @@ public final class BluetoothSocket implements Closeable {
IBluetooth bluetoothProxy =
BluetoothAdapter.getDefaultAdapter().getBluetoothService();
if (bluetoothProxy == null) throw new IOException("Bluetooth is off");
- mPfd = bluetoothProxy.getSocketManager().connectSocket(mDevice, mType,
- mUuid, mPort, getSecurityFlags());
+ final SynchronousResultReceiver<IBluetoothSocketManager> recv =
+ new SynchronousResultReceiver();
+ bluetoothProxy.getSocketManager(recv);
+ IBluetoothSocketManager socketManager =
+ recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
+ if (socketManager == null) throw new IOException("bt get socket manager failed");
+ mPfd = socketManager.connectSocket(mDevice, mType, mUuid, mPort, getSecurityFlags());
synchronized (this) {
if (DBG) Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
@@ -450,8 +460,8 @@ public final class BluetoothSocket implements Closeable {
}
mSocketState = SocketState.CONNECTED;
}
- } catch (RemoteException e) {
- Log.e(TAG, Log.getStackTraceString(new Throwable()));
+ } catch (RemoteException | TimeoutException e) {
+ Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
throw new IOException("unable to send RPC: " + e.getMessage());
}
}
@@ -471,10 +481,19 @@ public final class BluetoothSocket implements Closeable {
}
try {
if (DBG) Log.d(TAG, "bindListen(): mPort=" + mPort + ", mType=" + mType);
- mPfd = bluetoothProxy.getSocketManager().createSocketChannel(mType, mServiceName,
- mUuid, mPort, getSecurityFlags());
- } catch (RemoteException e) {
- Log.e(TAG, Log.getStackTraceString(new Throwable()));
+ final SynchronousResultReceiver<IBluetoothSocketManager> recv =
+ new SynchronousResultReceiver();
+ bluetoothProxy.getSocketManager(recv);
+ IBluetoothSocketManager socketManager =
+ recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
+ if (socketManager == null) {
+ Log.e(TAG, "bindListen() bt get socket manager failed");
+ return -1;
+ }
+ mPfd = socketManager
+ .createSocketChannel(mType, mServiceName, mUuid, mPort, getSecurityFlags());
+ } catch (RemoteException | TimeoutException e) {
+ Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
return -1;
}
@@ -738,9 +757,15 @@ public final class BluetoothSocket implements Closeable {
}
if (DBG) Log.d(TAG, "requestMaximumTxDataLength");
- bluetoothProxy.getSocketManager().requestMaximumTxDataLength(mDevice);
- } catch (RemoteException e) {
- Log.e(TAG, Log.getStackTraceString(new Throwable()));
+ final SynchronousResultReceiver<IBluetoothSocketManager> recv =
+ new SynchronousResultReceiver();
+ bluetoothProxy.getSocketManager(recv);
+ IBluetoothSocketManager socketManager =
+ recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
+ if (socketManager == null) throw new IOException("bt get socket manager failed");
+ socketManager.requestMaximumTxDataLength(mDevice);
+ } catch (RemoteException | TimeoutException e) {
+ Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
throw new IOException("unable to send RPC: " + e.getMessage());
}
}