如何以编程方式获取android中连接的usb设备列表?以下代码不起作用

时间:2017-01-25 18:56:11

标签: android

public class HardwareActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String ACTION_USB_PERMISSION = "com.android.missilelauncher.USB_PERMISSION";
    private PendingIntent PermissionIntent;
    private Button button;
    private ImageView back;
    private TextView textInfo;
    private UsbDevice usbDevice;
    private UsbManager usbManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String action = getIntent().getAction();
        setContentView(R.layout.activity_hardware);
        button = (Button) findViewById(R.id.check);
        back = (ImageView) findViewById(R.id.back);
        textInfo = (TextView) findViewById(R.id.usb_info);
        back.setOnClickListener(this);
        button.setOnClickListener(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getAction().equals(UsbManager.ACTION_USB_ACCESSORY_ATTACHED) || intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
            Log.d("USB DEVICE ", " device attached");
        } else if (intent.getAction().equals(UsbManager.ACTION_USB_ACCESSORY_DETACHED) || intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
            Log.d("USB DEVICE ", " device detached");
        }
    }

    private void checkInfo() {
        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        PermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
                ACTION_USB_PERMISSION), 0);
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        String deviceString = "";
        while (deviceIterator.hasNext()) {
            usbDevice = deviceIterator.next();
            deviceString += "\n" + "DeviceID: " + usbDevice.getDeviceId() + "\n" +
                    "DeviceName: " + usbDevice.getDeviceName() + "\n" +
                    "DeviceClass: " + usbDevice.getDeviceClass() + " - " +
                    "VendorID: " + usbDevice.getVendorId() + "\n" +
                    "ProductID: " + usbDevice.getProductId() + "\n" ;
        }
        Log.d("USB Info", "USB " + deviceString);
        textInfo.setText(deviceString);
    }

1 个答案:

答案 0 :(得分:0)

请确保您的设备具有系统权限

android.hardware.usb.host.xml里面/ system / etc / permissions /

要启用USB主机API支持,您应该添加一个名为android.hardware.usb.host.xml的文件,其中包含以下行:

<permissions>

进入文件夹

/系统的/ etc /权限 在该文件夹中查找名为

的文件

handheld_core_hardware.xml或tablet_core_hardware.xml 并添加

<feature name="android.hardware.usb.host" />
into <permissions> section.

重新启动您的设备。 USB主机API应该可以工作。

步骤: adb pull /system/etc/permissions/tablet_core_hardware.xml

更新该文件并按指定创建android.hardware.usb.host.xml

adb push android.hardware.usb.host.xml / system / etc / permissions adb push tablet_core_hardware.xml / system / etc / permissions 重新启动。