Skip to content

Commit 8584fe4

Browse files
authored
Allow Unlimited Read Size for Android 9+ (#609)
1 parent 0b5950c commit 8584fe4

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.hardware.usb.UsbDeviceConnection;
1111
import android.hardware.usb.UsbEndpoint;
1212
import android.hardware.usb.UsbRequest;
13+
import android.os.Build;
1314
import android.util.Log;
1415

1516
import com.hoho.android.usbserial.util.MonotonicClock;
@@ -28,7 +29,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
2829
public static boolean DEBUG = false;
2930

3031
private static final String TAG = CommonUsbSerialPort.class.getSimpleName();
31-
private static final int MAX_READ_SIZE = 16 * 1024; // = old bulkTransfer limit
32+
private static final int MAX_READ_SIZE = 16 * 1024; // = old bulkTransfer limit prior to Android 9
3233

3334
protected final UsbDevice mDevice;
3435
protected final int mPortNumber;
@@ -207,7 +208,7 @@ protected int read(final byte[] dest, int length, final int timeout, boolean tes
207208
// /system/lib64/libandroid_runtime.so (android_hardware_UsbDeviceConnection_request_wait(_JNIEnv*, _jobject*, long)+84)
208209
// data loss / crashes were observed with timeout up to 200 msec
209210
long endTime = testConnection ? MonotonicClock.millis() + timeout : 0;
210-
int readMax = Math.min(length, MAX_READ_SIZE);
211+
int readMax = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) ? length : Math.min(length, MAX_READ_SIZE);
211212
nread = mConnection.bulkTransfer(mReadEndpoint, dest, readMax, timeout);
212213
// Android error propagation is improvable:
213214
// nread == -1 can be: timeout, connection lost, buffer to small, ???

0 commit comments

Comments
 (0)