Stop forwarding when BluetoothSocket read returns -1

When BluetoothSocket.InputStream#read() returns -1, the Bluetooth stack
closes the socket. Instead of dropping these packets, throw an exception
to tear down the link.

Test: TH
Change-Id: I7b78d01bc62383d9f93fa70cc4237e3604c98913
This commit is contained in:
Patrick Rohr 2025-02-25 09:43:18 -08:00
commit acbd2cce99

View file

@ -109,10 +109,10 @@ public class L2capPacketForwarder {
public int read(byte[] bytes, int off, int len) throws IOException {
// Note: EINTR is handled internally and automatically triggers a retry loop.
int bytesRead = mInputStream.read(bytes, off, len);
if (bytesRead > MTU) {
if (bytesRead < 0 || bytesRead > MTU) {
// Don't try to recover, just trigger network teardown. This might indicate a bug in
// the Bluetooth stack.
throw new IOException("Packet exceeds MTU");
throw new IOException("Packet exceeds MTU or reached EOF. Read: " + bytesRead);
}
return bytesRead;
}