|
| 1 | +# Android USB Debugging Guide |
| 2 | + |
| 3 | +## Current Situation |
| 4 | + |
| 5 | +**Status**: APK builds and installs, but USB ports not detected on Android tablet |
| 6 | + |
| 7 | +**What We Know**: |
| 8 | +- ✅ Build succeeds without errors |
| 9 | +- ✅ Plugin v2.16.0 has Android Kotlin code |
| 10 | +- ✅ Manifest patch script runs in CI |
| 11 | +- ✅ App prompts for USB device handling when device attached |
| 12 | +- ❌ No ports show in the UI after permission granted |
| 13 | + |
| 14 | +## Investigation Steps |
| 15 | + |
| 16 | +### 1. Verify Manifest Patching (FIRST PRIORITY) |
| 17 | + |
| 18 | +Run the test workflow: |
| 19 | +```bash |
| 20 | +# Push the test workflow |
| 21 | +git add .github/workflows/test-android-patch.yml |
| 22 | +git commit -m "Add manifest patching test workflow" |
| 23 | +git push |
| 24 | + |
| 25 | +# Then go to GitHub Actions and run "Test Android Patch" manually |
| 26 | +``` |
| 27 | + |
| 28 | +Check the workflow output for: |
| 29 | +- `BEFORE patch:` section should show unpatched manifest |
| 30 | +- `AFTER patch:` section should show USB permissions |
| 31 | +- `device_filter.xml` should exist with VID/PID entries |
| 32 | +- `build.gradle.kts` should have usb-serial-for-android dependency |
| 33 | + |
| 34 | +### 2. Check Your USB Device VID/PID |
| 35 | + |
| 36 | +On your Linux machine with the flight controller connected: |
| 37 | +```bash |
| 38 | +lsusb |
| 39 | +``` |
| 40 | + |
| 41 | +Look for output like: |
| 42 | +``` |
| 43 | +Bus 001 Device 005: ID 0483:5740 STMicroelectronics Virtual COM Port |
| 44 | + ^^^^ ^^^^ |
| 45 | + VID PID |
| 46 | +``` |
| 47 | + |
| 48 | +Verify this VID/PID combination is in `device_filter.xml`: |
| 49 | +```xml |
| 50 | +<usb-device vendor-id="0x0483" product-id="0x5740"/> |
| 51 | +``` |
| 52 | + |
| 53 | +If your device isn't listed, we need to add it to the patch script. |
| 54 | + |
| 55 | +### 3. Check Plugin Initialization |
| 56 | + |
| 57 | +The plugin should auto-initialize on Android, but we can verify by checking the Rust code: |
| 58 | + |
| 59 | +**Current initialization** (src-tauri/src/main.rs): |
| 60 | +```rust |
| 61 | +.plugin(tauri_plugin_serialplugin::init()) |
| 62 | +``` |
| 63 | + |
| 64 | +This should work, but the plugin may need the Android UsbManager to be explicitly initialized. |
| 65 | + |
| 66 | +### 4. Potential Issues |
| 67 | + |
| 68 | +#### Issue A: Runtime Permission Not Requested |
| 69 | +Android requires apps to request USB permission at runtime, not just declare it in the manifest. The plugin's Kotlin code should handle this, but it may need: |
| 70 | + |
| 71 | +```kotlin |
| 72 | +// In MainActivity.kt or plugin initialization |
| 73 | +val usbManager = getSystemService(Context.USB_SERVICE) as UsbManager |
| 74 | +val permissionIntent = PendingIntent.getBroadcast(this, 0, Intent(ACTION_USB_PERMISSION), 0) |
| 75 | +usbManager.requestPermission(device, permissionIntent) |
| 76 | +``` |
| 77 | + |
| 78 | +#### Issue B: Plugin Not Using usb-serial-for-android |
| 79 | +The plugin may have its own USB implementation that conflicts with usb-serial-for-android. We need to verify the plugin actually uses this library. |
| 80 | + |
| 81 | +#### Issue C: Port Enumeration Timing |
| 82 | +Ports may need to be enumerated AFTER USB permission is granted. The app may need to: |
| 83 | +1. Wait for USB_DEVICE_ATTACHED broadcast |
| 84 | +2. Request permission via dialog |
| 85 | +3. THEN enumerate ports after permission granted |
| 86 | + |
| 87 | +### 5. Next Steps |
| 88 | + |
| 89 | +1. **Run test workflow** to verify patches are applied |
| 90 | +2. **Check device VID/PID** matches filter |
| 91 | +3. **Review plugin source code** at https://github.com/s00d/tauri-plugin-serialplugin/tree/main/android |
| 92 | +4. **Add logging** to see what the plugin sees: |
| 93 | + - Is UsbManager finding devices? |
| 94 | + - Is permission actually granted? |
| 95 | + - Does the plugin call port enumeration after permission? |
| 96 | + |
| 97 | +### 6. Workaround: Custom Android Code |
| 98 | + |
| 99 | +If the plugin doesn't properly handle USB, we may need to add custom Android code: |
| 100 | + |
| 101 | +**Option 1**: Fork and patch the plugin |
| 102 | +**Option 2**: Add custom Kotlin code to MainActivity |
| 103 | +**Option 3**: Use a different approach (WebUSB, custom serial implementation) |
| 104 | + |
| 105 | +## Key Files |
| 106 | + |
| 107 | +- `scripts/patch-android-manifest.sh` - Adds USB permissions and filters |
| 108 | +- `src-tauri/gen/android/app/src/main/AndroidManifest.xml` - Generated manifest (check after patch) |
| 109 | +- `src-tauri/gen/android/app/src/main/res/xml/device_filter.xml` - USB VID/PID filter |
| 110 | +- `src-tauri/gen/android/app/build.gradle.kts` - Should have usb-serial-for-android dependency |
| 111 | + |
| 112 | +## Supported USB Chips (Currently in device_filter.xml) |
| 113 | + |
| 114 | +- FTDI (VID: 0x0403) |
| 115 | +- STM32 Virtual COM (VID: 0x0483) |
| 116 | +- Silicon Labs CP210x (VID: 0x10c4) |
| 117 | +- GD32 (VID: 0x28e9) |
| 118 | +- AT32 (VID: 0x2e3c) |
| 119 | +- APM32 (VID: 0x314b) |
| 120 | +- Raspberry Pi Pico (VID: 0x2e8a) |
| 121 | + |
| 122 | +If your device isn't listed, add it to `patch-android-manifest.sh` in the device_filter.xml section. |
0 commit comments