Skip to content

Commit d8af1b3

Browse files
committed
Improvements to the android usb-sample apps: changed mutability flag for pending intent, fixed OnConnectReturn switch case, added permission request for devices already connected and fetched during the check() function call
1 parent 83ff190 commit d8af1b3

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

Examples_Android/SuperpoweredUSBExample/complexusb/src/main/java/com/superpowered/complexusb/SuperpoweredUSBAudio.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class SuperpoweredUSBAudio {
2121
public SuperpoweredUSBAudio(Context c, SuperpoweredUSBAudioHandler h) {
2222
context = c;
2323
handler = h;
24-
permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE);
24+
permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE);
2525

2626
BroadcastReceiver usbReceiver = new BroadcastReceiver() {
2727
@Override
@@ -61,16 +61,23 @@ public void onReceive(Context context, Intent intent) {
6161
private void addUSBDevice(UsbDevice device) {
6262
UsbManager manager = (UsbManager)context.getSystemService(Context.USB_SERVICE);
6363
if (manager == null) return;
64+
65+
if (!manager.hasPermission(device)) {
66+
manager.requestPermission(device, permissionIntent);
67+
return;
68+
}
69+
6470
UsbDeviceConnection connection = manager.openDevice(device);
6571
if (connection != null) {
6672
int id = device.getDeviceId();
6773
switch (onConnect(id, connection.getFileDescriptor(), connection.getRawDescriptors())) {
68-
case 1: if (handler != null) handler.onUSBAudioDeviceAttached(id); break; // Audio device.
69-
case 2: if (handler != null) handler.onUSBMIDIDeviceAttached(id); break; // MIDI device.
70-
case 3: // Audio and MIDI device.
74+
case 0: // Audio and MIDI device.
7175
if (handler != null) handler.onUSBAudioDeviceAttached(id);
7276
if (handler != null) handler.onUSBMIDIDeviceAttached(id);
7377
break;
78+
case 1: if (handler != null) handler.onUSBAudioDeviceAttached(id); break; // Audio device.
79+
case 2: if (handler != null) handler.onUSBMIDIDeviceAttached(id); break; // MIDI device.
80+
case 3: break; // Found no audio or MIDI features for this device.
7481
}
7582
}
7683
}

Examples_Android/SuperpoweredUSBExample/simpleusb/src/main/java/com/superpowered/simpleusb/SuperpoweredUSBAudio.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class SuperpoweredUSBAudio {
2121
public SuperpoweredUSBAudio(Context c, SuperpoweredUSBAudioHandler h) {
2222
context = c;
2323
handler = h;
24-
permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE);
24+
permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE);
2525

2626
BroadcastReceiver usbReceiver = new BroadcastReceiver() {
2727
@Override
@@ -61,20 +61,27 @@ public void onReceive(Context context, Intent intent) {
6161
private void addUSBDevice(UsbDevice device) {
6262
UsbManager manager = (UsbManager)context.getSystemService(Context.USB_SERVICE);
6363
if (manager == null) return;
64+
65+
if (!manager.hasPermission(device)) {
66+
manager.requestPermission(device, permissionIntent);
67+
return;
68+
}
69+
6470
UsbDeviceConnection connection = manager.openDevice(device);
6571
if (connection != null) {
6672
int id = device.getDeviceId();
6773
switch (onConnect(id, connection.getFileDescriptor(), connection.getRawDescriptors())) {
68-
case 1: // Audio device.
74+
case 0: // Audio and MIDI device.
6975
if (handler != null) handler.onUSBAudioDeviceAttached(id);
70-
break;
71-
case 2: // MIDI device.
7276
if (handler != null) handler.onUSBMIDIDeviceAttached(id);
7377
break;
74-
case 3: // Audio and MIDI device.
78+
case 1: // Audio device.
7579
if (handler != null) handler.onUSBAudioDeviceAttached(id);
80+
break;
81+
case 2: // MIDI device.
7682
if (handler != null) handler.onUSBMIDIDeviceAttached(id);
7783
break;
84+
case 3: break; // Found no audio or MIDI features for this device.
7885
}
7986
}
8087
}

0 commit comments

Comments
 (0)