Skip to content

Commit 8f00b97

Browse files
authored
Merge pull request #193 from mws-rmain/master
Ensure findSerialPortDevice() requests access ONLY to a supported device
2 parents 77ab67d + 02201fe commit 8f00b97

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

example/src/main/java/com/felhr/serialportexample/UsbService.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.os.Binder;
1313
import android.os.Handler;
1414
import android.os.IBinder;
15+
import android.util.Log;
1516

1617
import com.felhr.usbserial.CDCSerialDevice;
1718
import com.felhr.usbserial.UsbSerialDevice;
@@ -23,6 +24,8 @@
2324

2425
public class UsbService extends Service {
2526

27+
public static final String TAG = "UsbService";
28+
2629
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
2730
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
2831
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
@@ -175,31 +178,39 @@ private void findSerialPortDevice() {
175178
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
176179
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
177180
if (!usbDevices.isEmpty()) {
178-
boolean keep = true;
181+
182+
// first, dump the hashmap for diagnostic purposes
183+
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
184+
device = entry.getValue();
185+
Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s",
186+
device.getVendorId(), device.getProductId(),
187+
UsbSerialDevice.isSupported(device),
188+
device.getDeviceClass(), device.getDeviceSubclass(),
189+
device.getDeviceName()));
190+
}
191+
179192
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
180193
device = entry.getValue();
181194
int deviceVID = device.getVendorId();
182195
int devicePID = device.getProductId();
183196

184-
if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) && deviceVID != 0x5c6 && devicePID != 0x904c) {
185-
186-
// There is a device connected to our Android device. Try to open it as a Serial Port.
197+
// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) && deviceVID != 0x5c6 && devicePID != 0x904c) {
198+
if (UsbSerialDevice.isSupported(device)) {
199+
// There is a supported device connected - request permission to access it.
187200
requestUserPermission();
188-
keep = false;
201+
break;
189202
} else {
190203
connection = null;
191204
device = null;
192205
}
193-
194-
if (!keep)
195-
break;
196206
}
197-
if (!keep) {
198-
// There is no USB devices connected (but usb host were listed). Send an intent to MainActivity.
207+
if (device==null) {
208+
// There are no USB devices connected (but usb host were listed). Send an intent to MainActivity.
199209
Intent intent = new Intent(ACTION_NO_USB);
200210
sendBroadcast(intent);
201211
}
202212
} else {
213+
Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." );
203214
// There is no USB devices connected. Send an intent to MainActivity
204215
Intent intent = new Intent(ACTION_NO_USB);
205216
sendBroadcast(intent);
@@ -218,6 +229,7 @@ private void setFilter() {
218229
* Request user permission. The response will be received in the BroadcastReceiver
219230
*/
220231
private void requestUserPermission() {
232+
Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) );
221233
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
222234
usbManager.requestPermission(device, mPendingIntent);
223235
}

examplestreams/src/main/java/com/felhr/examplestreams/UsbService.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.os.Binder;
1313
import android.os.Handler;
1414
import android.os.IBinder;
15+
import android.util.Log;
1516

1617
import com.felhr.usbserial.CDCSerialDevice;
1718
import com.felhr.usbserial.SerialInputStream;
@@ -25,6 +26,8 @@
2526

2627
public class UsbService extends Service {
2728

29+
public static final String TAG = "UsbService";
30+
2831
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
2932
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
3033
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
@@ -155,30 +158,39 @@ private void findSerialPortDevice() {
155158
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
156159
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
157160
if (!usbDevices.isEmpty()) {
158-
boolean keep = true;
161+
162+
// first, dump the map for diagnostic purposes
163+
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
164+
device = entry.getValue();
165+
Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s",
166+
device.getVendorId(), device.getProductId(),
167+
UsbSerialDevice.isSupported(device),
168+
device.getDeviceClass(), device.getDeviceSubclass(),
169+
device.getDeviceName()));
170+
}
171+
159172
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
160173
device = entry.getValue();
161174
int deviceVID = device.getVendorId();
162175
int devicePID = device.getProductId();
163176

164-
if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) {
177+
// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) {
178+
if (UsbSerialDevice.isSupported(device)) {
165179
// There is a device connected to our Android device. Try to open it as a Serial Port.
166180
requestUserPermission();
167-
keep = false;
181+
break;
168182
} else {
169183
connection = null;
170184
device = null;
171185
}
172-
173-
if (!keep)
174-
break;
175186
}
176-
if (!keep) {
177-
// There is no USB devices connected (but usb host were listed). Send an intent to MainActivity.
187+
if (device==null) {
188+
// There are no USB devices connected (but usb host were listed). Send an intent to MainActivity.
178189
Intent intent = new Intent(ACTION_NO_USB);
179190
sendBroadcast(intent);
180191
}
181192
} else {
193+
Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." );
182194
// There is no USB devices connected. Send an intent to MainActivity
183195
Intent intent = new Intent(ACTION_NO_USB);
184196
sendBroadcast(intent);
@@ -197,6 +209,7 @@ private void setFilter() {
197209
* Request user permission. The response will be received in the BroadcastReceiver
198210
*/
199211
private void requestUserPermission() {
212+
Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) );
200213
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
201214
usbManager.requestPermission(device, mPendingIntent);
202215
}

examplesync/src/main/java/com/felhr/serialportexamplesync/UsbService.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.os.Binder;
1313
import android.os.Handler;
1414
import android.os.IBinder;
15+
import android.util.Log;
1516

1617
import com.felhr.usbserial.CDCSerialDevice;
1718
import com.felhr.usbserial.UsbSerialDevice;
@@ -23,6 +24,8 @@
2324

2425
public class UsbService extends Service {
2526

27+
public static final String TAG = "UsbService";
28+
2629
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
2730
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
2831
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
@@ -185,30 +188,39 @@ private void findSerialPortDevice() {
185188
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
186189
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
187190
if (!usbDevices.isEmpty()) {
188-
boolean keep = true;
191+
192+
// first, dump the map for diagnostic purposes
193+
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
194+
device = entry.getValue();
195+
Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s",
196+
device.getVendorId(), device.getProductId(),
197+
UsbSerialDevice.isSupported(device),
198+
device.getDeviceClass(), device.getDeviceSubclass(),
199+
device.getDeviceName()));
200+
}
201+
189202
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
190203
device = entry.getValue();
191204
int deviceVID = device.getVendorId();
192205
int devicePID = device.getProductId();
193206

194-
if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) {
207+
// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) {
208+
if (UsbSerialDevice.isSupported(device)) {
195209
// There is a device connected to our Android device. Try to open it as a Serial Port.
196210
requestUserPermission();
197-
keep = false;
211+
break;
198212
} else {
199213
connection = null;
200214
device = null;
201215
}
202-
203-
if (!keep)
204-
break;
205216
}
206-
if (!keep) {
217+
if (device==null) {
207218
// There is no USB devices connected (but usb host were listed). Send an intent to MainActivity.
208219
Intent intent = new Intent(ACTION_NO_USB);
209220
sendBroadcast(intent);
210221
}
211222
} else {
223+
Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." );
212224
// There is no USB devices connected. Send an intent to MainActivity
213225
Intent intent = new Intent(ACTION_NO_USB);
214226
sendBroadcast(intent);
@@ -227,6 +239,7 @@ private void setFilter() {
227239
* Request user permission. The response will be received in the BroadcastReceiver
228240
*/
229241
private void requestUserPermission() {
242+
Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) );
230243
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
231244
usbManager.requestPermission(device, mPendingIntent);
232245
}

0 commit comments

Comments
 (0)