doum
19 小时以前 6c12dd77bc481aeabec568bfed3dd68e81b80f8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.doumee.keyCabinet.utils.i485;
 
import android.content.Context;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
 
import java.util.ArrayList;
import java.util.List;
 
public class SportUtils {
    public static List<String> getSerialPortPaths(Context context) {
        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        List<UsbDevice> devices = new ArrayList<>(usbManager.getDeviceList().values());
        List<String> serialPaths = new ArrayList<>();
 
        for (UsbDevice device : devices) {
            for (int i = 0; i < device.getInterfaceCount(); i++) {
                UsbInterface intf = device.getInterface(i);
                if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
                    serialPaths.add(device.getDeviceName());
                    break;
                }
            }
        }
        return serialPaths;
    }
}