From aa4a19cd463bd9f8af905cc74142b91b76f22817 Mon Sep 17 00:00:00 2001 From: mws-rmain <30533684+mws-rmain@users.noreply.github.com> Date: Wed, 9 Jan 2019 11:25:21 -0500 Subject: [PATCH 1/6] Ensure findSerialPortDevice() requests access ONLY to a supported device The hashmap returned by usbManager.getDeviceList() contains devices in random order. Access will be requested for the first supported device. --- .../felhr/serialportexample/UsbService.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/example/src/main/java/com/felhr/serialportexample/UsbService.java b/example/src/main/java/com/felhr/serialportexample/UsbService.java index 336ad55..946206a 100644 --- a/example/src/main/java/com/felhr/serialportexample/UsbService.java +++ b/example/src/main/java/com/felhr/serialportexample/UsbService.java @@ -175,31 +175,39 @@ public class UsbService extends Service { // This snippet will try to open the first encountered usb device connected, excluding usb root hubs HashMap usbDevices = usbManager.getDeviceList(); if (!usbDevices.isEmpty()) { - boolean keep = true; + + // first, dump the hashmap for diagnostic purposes + for (Map.Entry entry : usbDevices.entrySet()) { + device = entry.getValue(); + Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s", + device.getVendorId(), device.getProductId(), + UsbSerialDevice.isSupported(device), + device.getDeviceClass(), device.getDeviceSubclass(), + device.getDeviceName())); + } + for (Map.Entry entry : usbDevices.entrySet()) { device = entry.getValue(); int deviceVID = device.getVendorId(); int devicePID = device.getProductId(); - if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) && deviceVID != 0x5c6 && devicePID != 0x904c) { - - // There is a device connected to our Android device. Try to open it as a Serial Port. +// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) && deviceVID != 0x5c6 && devicePID != 0x904c) { + if (UsbSerialDevice.isSupported(device)) { + // There is a supported device connected - request permission to access it. requestUserPermission(); - keep = false; + break; } else { connection = null; device = null; } - - if (!keep) - break; } - if (!keep) { - // There is no USB devices connected (but usb host were listed). Send an intent to MainActivity. + if (device==null) { + // There are no USB devices connected (but usb host were listed). Send an intent to MainActivity. Intent intent = new Intent(ACTION_NO_USB); sendBroadcast(intent); } } else { + Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." ); // There is no USB devices connected. Send an intent to MainActivity Intent intent = new Intent(ACTION_NO_USB); sendBroadcast(intent); @@ -218,6 +226,7 @@ public class UsbService extends Service { * Request user permission. The response will be received in the BroadcastReceiver */ private void requestUserPermission() { + Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) ); PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); usbManager.requestPermission(device, mPendingIntent); } From b5b9a1649936c6fa8ce67e9ddde4292ed5d56a37 Mon Sep 17 00:00:00 2001 From: mws-rmain <30533684+mws-rmain@users.noreply.github.com> Date: Wed, 9 Jan 2019 11:33:48 -0500 Subject: [PATCH 2/6] Ensure findSerialPortDevice() requests access ONLY to a supported device The hashmap returned by usbManager.getDeviceList() contains devices in random order. Access will be requested for the first supported device. --- .../serialportexamplesync/UsbService.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java b/examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java index 7a96b23..7db0d43 100644 --- a/examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java +++ b/examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java @@ -185,30 +185,39 @@ public class UsbService extends Service { // This snippet will try to open the first encountered usb device connected, excluding usb root hubs HashMap usbDevices = usbManager.getDeviceList(); if (!usbDevices.isEmpty()) { - boolean keep = true; + + // first, dump the map for diagnostic purposes + for (Map.Entry entry : usbDevices.entrySet()) { + device = entry.getValue(); + Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s", + device.getVendorId(), device.getProductId(), + UsbSerialDevice.isSupported(device), + device.getDeviceClass(), device.getDeviceSubclass(), + device.getDeviceName())); + } + for (Map.Entry entry : usbDevices.entrySet()) { device = entry.getValue(); int deviceVID = device.getVendorId(); int devicePID = device.getProductId(); - if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) { +// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) { + if (UsbSerialDevice.isSupported(device)) { // There is a device connected to our Android device. Try to open it as a Serial Port. requestUserPermission(); - keep = false; + break; } else { connection = null; device = null; } - - if (!keep) - break; } - if (!keep) { + if (device==null) { // There is no USB devices connected (but usb host were listed). Send an intent to MainActivity. Intent intent = new Intent(ACTION_NO_USB); sendBroadcast(intent); } } else { + Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." ); // There is no USB devices connected. Send an intent to MainActivity Intent intent = new Intent(ACTION_NO_USB); sendBroadcast(intent); @@ -227,6 +236,7 @@ public class UsbService extends Service { * Request user permission. The response will be received in the BroadcastReceiver */ private void requestUserPermission() { + Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) ); PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); usbManager.requestPermission(device, mPendingIntent); } From e0af79487790eaea9e0519540f5d36cbf222b9d0 Mon Sep 17 00:00:00 2001 From: mws-rmain <30533684+mws-rmain@users.noreply.github.com> Date: Wed, 9 Jan 2019 11:38:56 -0500 Subject: [PATCH 3/6] Ensure findSerialPortDevice() requests access ONLY to a supported device The hashmap returned by usbManager.getDeviceList() contains devices in random order. Access will be requested for the first supported device. --- .../com/felhr/examplestreams/UsbService.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java b/examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java index 51549b5..db4575e 100644 --- a/examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java +++ b/examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java @@ -155,30 +155,39 @@ public class UsbService extends Service { // This snippet will try to open the first encountered usb device connected, excluding usb root hubs HashMap usbDevices = usbManager.getDeviceList(); if (!usbDevices.isEmpty()) { - boolean keep = true; + + // first, dump the map for diagnostic purposes + for (Map.Entry entry : usbDevices.entrySet()) { + device = entry.getValue(); + Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s", + device.getVendorId(), device.getProductId(), + UsbSerialDevice.isSupported(device), + device.getDeviceClass(), device.getDeviceSubclass(), + device.getDeviceName())); + } + for (Map.Entry entry : usbDevices.entrySet()) { device = entry.getValue(); int deviceVID = device.getVendorId(); int devicePID = device.getProductId(); - if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) { +// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) { + if (UsbSerialDevice.isSupported(device)) { // There is a device connected to our Android device. Try to open it as a Serial Port. requestUserPermission(); - keep = false; + break; } else { connection = null; device = null; } - - if (!keep) - break; } - if (!keep) { - // There is no USB devices connected (but usb host were listed). Send an intent to MainActivity. + if (device==null) { + // There are no USB devices connected (but usb host were listed). Send an intent to MainActivity. Intent intent = new Intent(ACTION_NO_USB); sendBroadcast(intent); } } else { + Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." ); // There is no USB devices connected. Send an intent to MainActivity Intent intent = new Intent(ACTION_NO_USB); sendBroadcast(intent); @@ -197,6 +206,7 @@ public class UsbService extends Service { * Request user permission. The response will be received in the BroadcastReceiver */ private void requestUserPermission() { + Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) ); PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); usbManager.requestPermission(device, mPendingIntent); } From e7439e3551d0f6f7285a24fa3a5c162c18ae048e Mon Sep 17 00:00:00 2001 From: mws-rmain <30533684+mws-rmain@users.noreply.github.com> Date: Thu, 10 Jan 2019 13:58:01 -0500 Subject: [PATCH 4/6] Fix build issues Add missing import & TAG --- .../src/main/java/com/felhr/serialportexample/UsbService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/example/src/main/java/com/felhr/serialportexample/UsbService.java b/example/src/main/java/com/felhr/serialportexample/UsbService.java index 946206a..80cc241 100644 --- a/example/src/main/java/com/felhr/serialportexample/UsbService.java +++ b/example/src/main/java/com/felhr/serialportexample/UsbService.java @@ -12,6 +12,7 @@ import android.hardware.usb.UsbManager; import android.os.Binder; import android.os.Handler; import android.os.IBinder; +import android.util.Log; import com.felhr.usbserial.CDCSerialDevice; import com.felhr.usbserial.UsbSerialDevice; @@ -23,6 +24,8 @@ import java.util.Map; public class UsbService extends Service { + public static final String TAG = "UsbService"; + public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY"; public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED"; public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED"; From dce98bb5ea906ab283d1f35fbb8ee83e92437d10 Mon Sep 17 00:00:00 2001 From: mws-rmain <30533684+mws-rmain@users.noreply.github.com> Date: Thu, 10 Jan 2019 13:59:18 -0500 Subject: [PATCH 5/6] Fix Build Issues Add missing import & TAG --- .../src/main/java/com/felhr/examplestreams/UsbService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java b/examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java index db4575e..7460066 100644 --- a/examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java +++ b/examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java @@ -12,6 +12,7 @@ import android.hardware.usb.UsbManager; import android.os.Binder; import android.os.Handler; import android.os.IBinder; +import android.util.Log; import com.felhr.usbserial.CDCSerialDevice; import com.felhr.usbserial.SerialInputStream; @@ -25,6 +26,8 @@ import java.util.concurrent.atomic.AtomicBoolean; public class UsbService extends Service { + public static final String TAG = "UsbService"; + public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY"; public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED"; public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED"; From 02201fef82a7f1e762a690a71dd411103868def1 Mon Sep 17 00:00:00 2001 From: mws-rmain <30533684+mws-rmain@users.noreply.github.com> Date: Thu, 10 Jan 2019 14:00:29 -0500 Subject: [PATCH 6/6] Fix Build Issues Add missing import & TAG --- .../main/java/com/felhr/serialportexamplesync/UsbService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java b/examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java index 7db0d43..5ea9811 100644 --- a/examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java +++ b/examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java @@ -12,6 +12,7 @@ import android.hardware.usb.UsbManager; import android.os.Binder; import android.os.Handler; import android.os.IBinder; +import android.util.Log; import com.felhr.usbserial.CDCSerialDevice; import com.felhr.usbserial.UsbSerialDevice; @@ -23,6 +24,8 @@ import java.util.Map; public class UsbService extends Service { + public static final String TAG = "UsbService"; + public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY"; public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED"; public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";