Use IntRange for setPsm
Configures an IntRange [0, 255] for the L2CAP BLE PSM. Note that L2CAP
over Bluetooth Classic uses PSM values that exceed this range though is
explicitly not supported.
This CL also changes the value for PSM_ANY from -1 to 0 to make the
IntRange continuous as 0 is not a valid PSM (in both BLE and Classic).
Bug: 388438641
Test: TH
Change-Id: I15a69cfb80545c4f3723d089afd3cf026eec0696
diff --git a/framework/api/current.txt b/framework/api/current.txt
index a080149..323c533 100644
--- a/framework/api/current.txt
+++ b/framework/api/current.txt
@@ -244,7 +244,7 @@
field public static final int HEADER_COMPRESSION_6LOWPAN = 2; // 0x2
field public static final int HEADER_COMPRESSION_ANY = 0; // 0x0
field public static final int HEADER_COMPRESSION_NONE = 1; // 0x1
- field public static final int PSM_ANY = -1; // 0xffffffff
+ field public static final int PSM_ANY = 0; // 0x0
field public static final int ROLE_ANY = 0; // 0x0
field public static final int ROLE_CLIENT = 1; // 0x1
field public static final int ROLE_SERVER = 2; // 0x2
@@ -254,7 +254,7 @@
ctor public L2capNetworkSpecifier.Builder();
method @NonNull public android.net.L2capNetworkSpecifier build();
method @NonNull public android.net.L2capNetworkSpecifier.Builder setHeaderCompression(int);
- method @NonNull public android.net.L2capNetworkSpecifier.Builder setPsm(int);
+ method @NonNull public android.net.L2capNetworkSpecifier.Builder setPsm(@IntRange(from=0, to=255) int);
method @NonNull public android.net.L2capNetworkSpecifier.Builder setRemoteAddress(@Nullable android.net.MacAddress);
method @NonNull public android.net.L2capNetworkSpecifier.Builder setRole(int);
}
diff --git a/framework/src/android/net/L2capNetworkSpecifier.java b/framework/src/android/net/L2capNetworkSpecifier.java
index 0d46a3e..6770add 100644
--- a/framework/src/android/net/L2capNetworkSpecifier.java
+++ b/framework/src/android/net/L2capNetworkSpecifier.java
@@ -18,6 +18,7 @@
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
+import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Build;
@@ -31,7 +32,7 @@
import java.util.Objects;
/**
- * A {@link NetworkSpecifier} used to identify an L2CAP network.
+ * A {@link NetworkSpecifier} used to identify an L2CAP network over BLE.
*
* An L2CAP network is not symmetrical, meaning there exists both a server (Bluetooth peripheral)
* and a client (Bluetooth central) node. This specifier contains the information required to
@@ -119,7 +120,7 @@
* This PSM value is only meaningful in {@link NetworkRequest}s. Specifiers for actual L2CAP
* networks never have this value set.
*/
- public static final int PSM_ANY = -1;
+ public static final int PSM_ANY = 0;
/** The Bluetooth L2CAP Protocol/Service Multiplexer (PSM). */
private final int mPsm;
@@ -228,7 +229,10 @@
* @param psm the Protocol/Service Multiplexer (PSM) to connect to.
*/
@NonNull
- public Builder setPsm(int psm) {
+ public Builder setPsm(@IntRange(from = 0, to = 255) int psm) {
+ if (psm < 0 /* PSM_ANY */ || psm > 0xFF) {
+ throw new IllegalArgumentException("PSM must be PSM_ANY or within range [1, 255]");
+ }
mPsm = psm;
return this;
}