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.
pull/192/head
mws-rmain 2019-01-09 11:25:21 -05:00 zatwierdzone przez GitHub
rodzic d5da84072b
commit aa4a19cd46
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 19 dodań i 10 usunięć

Wyświetl plik

@ -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 // This snippet will try to open the first encountered usb device connected, excluding usb root hubs
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList(); HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
if (!usbDevices.isEmpty()) { if (!usbDevices.isEmpty()) {
boolean keep = true;
// first, dump the hashmap for diagnostic purposes
for (Map.Entry<String, UsbDevice> 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<String, UsbDevice> entry : usbDevices.entrySet()) { for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
device = entry.getValue(); device = entry.getValue();
int deviceVID = device.getVendorId(); int deviceVID = device.getVendorId();
int devicePID = device.getProductId(); int devicePID = device.getProductId();
if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) && deviceVID != 0x5c6 && devicePID != 0x904c) { // if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) && deviceVID != 0x5c6 && devicePID != 0x904c) {
if (UsbSerialDevice.isSupported(device)) {
// There is a device connected to our Android device. Try to open it as a Serial Port. // There is a supported device connected - request permission to access it.
requestUserPermission(); requestUserPermission();
keep = false; break;
} else { } else {
connection = null; connection = null;
device = 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. // There are no USB devices connected (but usb host were listed). Send an intent to MainActivity.
Intent intent = new Intent(ACTION_NO_USB); Intent intent = new Intent(ACTION_NO_USB);
sendBroadcast(intent); sendBroadcast(intent);
} }
} else { } else {
Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." );
// There is no USB devices connected. Send an intent to MainActivity // There is no USB devices connected. Send an intent to MainActivity
Intent intent = new Intent(ACTION_NO_USB); Intent intent = new Intent(ACTION_NO_USB);
sendBroadcast(intent); sendBroadcast(intent);
@ -218,6 +226,7 @@ public class UsbService extends Service {
* Request user permission. The response will be received in the BroadcastReceiver * Request user permission. The response will be received in the BroadcastReceiver
*/ */
private void requestUserPermission() { 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); PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
usbManager.requestPermission(device, mPendingIntent); usbManager.requestPermission(device, mPendingIntent);
} }