USBdevice识别为存储设备并找到路径 [英] USBdevice recognise as storage device and find path

查看:159
本文介绍了USBdevice识别为存储设备并找到路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测可用于存储的已安装设备(例如Pen-Drive)?如何找到已安装存储设备的路径,以便可以从中读取文件?

How can I detect a mounted device such as a Pen-Drive, that can be used for storage? How can I find the path for the mounted storage device so I may read files from it?

我已经使用以下广播接收器来访问已安装设备的权限:

I've used following broadcast receiver taking the permission to access mounted device:

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = (UsbDevice) intent
                        .getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (intent.getBooleanExtra(
                        UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if (device != null) {
                        // call method to set up device communication

                        Log.d(TAG, "onReceive: "+intent.getExtras().toString());
                        Log.d(TAG, "onReceive: "+intent.getData());
                        LinearLayout layoutUsbList = (LinearLayout)findViewById(R.id.layout_usb_list);
                        Button btn = new Button(MainActivity.this);
                        btn.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                        layoutUsbList.addView(btn);
                        btn.setText(device.getDeviceId()+"\t"+device.getDeviceName());
                        Log.d(TAG, "onReceive: "+intent.getExtras().toString());
                        final String path = intent.getData().getPath();
                        Log.e(TAG, "onReceive: path of device received from intent: "+ path );
                        btn.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                File file = new File(path);
                                Toast.makeText(MainActivity.this, "file exists --> "+file.exists()+"", Toast.LENGTH_SHORT).show();
                                Toast.makeText(MainActivity.this, "file is directory --> "+file.isDirectory()+"", Toast.LENGTH_SHORT).show();
                                Log.d(TAG, "onClick: file is directory --> "+file.isDirectory()+"");
                                try{
                                    Toast.makeText(MainActivity.this, file.listFiles().length+"", Toast.LENGTH_LONG).show();
                                }catch(Exception e){
                                    Toast.makeText(MainActivity.this, "error while showing total items", Toast.LENGTH_SHORT).show();
                                }

                            }
                        });

                    }
                } else {
                    Log.d("ERROR", "permission denied for device " + device);
                }
            }
        }
    }
};

清单文件:

<?xml version="1.0" encoding="utf-8"?>

<uses-feature android:name="android.hardware.usb.host"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:configChanges="keyboard|orientation"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>


        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
        </intent-filter>

        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter"/>

        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
        </intent-filter>


        <!---->
        <!---->
        <!---->
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED"/>
            <data android:scheme="file"/>
        </intent-filter>
    </activity>

</application>

推荐答案

遵循此. 每个大容量存储设备都至少具有一个接口描述符,其类别代码为08h, 代表大容量存储类.设备中未定义大容量存储类别 描述符! USB接口有两个端点描述符.一个IN端点 从设备和一个OUT端点读取以写入设备 2 .读写 在这种情况下,不一定意味着要在实际的存储介质上进行读取或写入, 这将在后面描述. 关于大容量存储类,有两种不同的类型.仅散装 传输(BBB)机制是最常见的一种.所有更新的设备都遵循 标准.然后是控制/批量/中断(CBI)标准,该标准不再 重要,因为USB-IF建议使用BBB方法

Accordig to this pdf refrencing this library. Every mass storage device has at least one interface descriptor with the class code 08h, which stands for the mass storage class. The mass storage class is not defined in the device descriptor! The USB interface has exactly two endpoint descriptors. One IN endpoint to read from the device and one OUT endpoint to write to the device2. Reading and writing in this case does not necessarily mean reading or writing on the actual storage medium, this is described later. There are two different types regarding the mass storage class. There is the bulk-only transport (BBB) mechanism which is the most common one. All newer devices follow that standard. Then there is the Control/Bulk/Interrupt (CBI) standard which is no longer important, because the USB-IF recommends using the BBB approach

UsbDevice被识别为massStorage设备,

UsbDevice is recognized as massStorage Device If:

usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_MASS_STORAGE
                        || usbInterface.getInterfaceSubclass() == INTERFACE_SUBCLASS // int 6
                        || usbInterface.getInterfaceProtocol() == INTERFACE_PROTOCOL // int 80

usbInterface.getEndpointCount() == 2

其中 端点之一必须满足以下条件:

where one of endpoint must satisfy following:

endPoint direction == 0
endPoint type = UsbConstants.USB_ENDPOINT_XFER_BULK //int 2

这篇关于USBdevice识别为存储设备并找到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆