You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

WebUSB API连接三星J7报错:设备已断开,求问题原因排查

Troubleshooting "DOMException: The device was disconnected" with WebUSB and Samsung J7

Let's break down the possible causes of this error and walk through actionable fixes—since you're seeing this across both Windows and Ubuntu, it's likely related to device setup, driver permissions, or device-side configurations rather than a browser-specific bug.

1. Fix USB Driver/Permission Issues

Windows

Samsung devices rely on their official USB drivers for proper low-level communication (generic system drivers often don't work for WebUSB use cases):

  • Download the Samsung USB Driver matching your J7 model from Samsung's official support resources.
  • Install the driver, restart your computer, then reconnect your device.

Ubuntu/Linux

Linux restricts USB device access for regular users by default. You'll need to add a udev rule to grant the necessary permissions:

  1. Create a new rule file using a terminal editor:
    sudo nano /etc/udev/rules.d/51-samsung-webusb.rules
    
  2. Paste this line into the file to open access for your Samsung device:
    SUBSYSTEM=="usb", ATTRS{idVendor}=="04e8", MODE="0666"
    
  3. Save and exit the editor, then reload udev rules and apply changes:
    sudo udevadm control --reload-rules && sudo udevadm trigger
    
  4. Unplug and replug your Samsung J7 to apply the new permissions.

2. Verify Device-Side Settings

Your J7 needs to be configured to allow USB debugging and WebUSB access:

  • Enable Developer Options: Go to Settings > About Phone, then tap the Build Number 7 times until you see the "You are now a developer!" prompt.
  • Turn on USB Debugging: Open Settings > Developer Options and toggle on USB Debugging.
  • Authorize Your Computer: When you connect your J7 to your PC, a pop-up will appear on the device asking to "Allow USB debugging from this computer?" Tap Allow (check "Always allow from this computer" to avoid repeated prompts).
  • Check USB Connection Mode: Avoid MTP/PTP modes (media/picture transfer) as they can block low-level USB access. Switch your device to Charging Only mode and try again.

3. Check WebUSB Compatibility & Browser Setup

  • Update Chrome: Make sure you're running the latest stable version of Chrome—older versions may have unpatched WebUSB bugs. You can check for updates in Chrome > Settings > About Chrome.
  • Inspect Device Logs: Open Chrome's chrome://device-log/ to view detailed USB connection logs. Look for errors related to device enumeration or permission failures—this can give you specific clues about what's going wrong.
  • Test with Hardware Changes: Use a high-quality USB data cable (charging-only cables won't work) and try connecting to a rear USB port on your computer (front ports often have weaker power and connectivity).

4. Updated Code with Improved Error Handling

Here's a refined version of your code using async/await (for better readability) and more detailed error feedback:

<body>
  <button onclick="myFunction()">Click me</button>
  <script>
    async function myFunction() {
      console.log('Clicked');
      try {
        const device = await navigator.usb.requestDevice({ 
          filters: [{ vendorId: 0x04e8 }] 
        });
        console.log('Device Selected:', device.productName, device.manufacturerName);
        
        await device.open();
        console.log('Device opened successfully!');
        
        // Select the default configuration (most devices use config ID 1)
        if (!device.configuration) {
          await device.selectConfiguration(1);
          console.log('Default configuration selected');
        }

        // Add further endpoint communication logic here
      } catch (error) {
        console.error('Full Error:', error);
        if (error.name === 'DOMException' && error.message.includes('disconnected')) {
          console.error('Likely causes: Missing drivers, unapproved USB debugging, or incompatible USB mode');
        }
      }
    }
  </script>
</body>

If you've tried all these steps and still see the error, it's possible that the Samsung J7's stock firmware blocks WebUSB access (some manufacturers restrict low-level USB APIs). In that case, you might need to explore custom ROMs that enable WebUSB support, but that's a more advanced troubleshooting step.

内容的提问来源于stack exchange,提问作者Mohamad Ali Mhaidly

火山引擎 最新活动