【问题标题】:How To Detect that Android Device is connected with USB OTG Or Not programmatically如何以编程方式检测 Android 设备是否与 USB OTG 连接
【发布时间】:2017-01-27 17:35:09
【问题描述】:

我正在使用自定义 OTG 指纹扫描仪。我想检查 OTG 是否已连接到我的 Android 设备或未在特定的 android 活动中。

【问题讨论】:

  • 谁能解决这个问题?
  • 你试过USB host吗?也许android.hardware.usb.action.USB_DEVICE_ATTACHED 接收器会有所帮助。如果你需要前向通信HERE 你有很好的例子(对于 Arduino,但主要是关于 Android 端)。但如果它是“特定的 android 设备”,那么可能有一个自定义 rom,或者您有该设备的“特定”文档
  • 是的@snachmsm 我正在使用相同的技术。每次连接设备时,我都会检查供应商 ID 和设备 ID,然后相应地设置设备状态。
  • 所以我应该把我的评论作为答案? :-)
  • 在你回答之前我已经知道了:D

标签: android usb-otg


【解决方案1】:
public class BootUpReceiver extends BroadcastReceiver {
    
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    String TAG = "OTG   ";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
//        Log.e("USB", "Device Connected -> " + action);
//Initialising global class to access USB ATTACH and DETACH state
        final GlobalClass globalVariable = (GlobalClass) context.getApplicationContext();

        if (action.equalsIgnoreCase("android.hardware.usb.action.USB_DEVICE_ATTACHED")) {

            UsbDevice device = (UsbDevice) intent
                    .getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if(device != null) {
                int vendorID = device.getVendorId();
                int productID = device.getProductId();
                if(String.valueOf(productID).equalsIgnoreCase(context.getString(R.string.productID/*product id of your specific device*/))&& (String.valueOf(vendorID).equalsIgnoreCase(context.getString(R.string.vendorID/*vendor id of your specific device*/)))){
    //If Product and Vendor Id match then set boolean "true" in global variable 
                    globalVariable.setIs_OTG(true);
                }else{
                    globalVariable.setIs_OTG(false);
                }
            }
        } else if (action.equalsIgnoreCase("android.hardware.usb.action.USB_DEVICE_DETACHED")) {
      //When ever device Detach set your global variable to "false"
            globalVariable.setIs_OTG(false);
        }  }   

您可以从任何 Activity 调用全局变量来检查当前 USB 状态:

 final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
                        if (globalVariable.is_OTG()) {
                            //Perform your functionality
                        } 

全局类:

public class GlobalClass extends Application {

    private boolean is_OTG = false;

    public boolean is_OTG() {
        return is_OTG;
    }

    public void setIs_OTG(boolean is_OTG) {
        this.is_OTG = is_OTG;
    }

}

清单:

 <application
        android:name="com.GlobalClass"

接收者:

 <receiver
            android:name=".BootUpReceiver"
            android:enabled="true" >
          <!--  <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.HOME" />
            </intent-filter>-->

            <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" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            </intent-filter>
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />

        </receiver>

【讨论】:

  • 这个来源是否适用于连接 USB OTG Pendrive ?如果我能成功连接,我怎样才能获得总可用空间?以及如何在连接后获取设备供应商 ID 和产品 ID?实际上我需要连接 USB OTG pendrive 并从连接的设备获取可用大小。任何帮助将不胜感激。
  • 使用上面的代码,当附加OTG时,它会老虎广播,你也可以获取用户ID和供应商ID。
  • 获取用户 ID 和供应商 ID 没问题。但我的另一个要求是获取所有可用文件夹并获取连接的 USB 设备的总可用空间。我该怎么做?
  • 你找到上述解决方案了吗?
  • @JayRathodRJ 您是否找到了满足您上述要求的解决方案?如果实现了,您可以在这里分享解决方案吗?我们在同一轨道上这样做。提前致谢!
【解决方案2】:

您可以使用 UsbManager 来检测附加的 otg 设备。

    UsbManager usbManager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
    Intent intent = new Intent("android.hardware.usb.action.USB_DEVICE_ATTACHED");
    intent.addCategory("android.hardware.usb.action.USB_DEVICE_DETACHED");
    Map<String, UsbDevice> usbDeviceList = usbManager.getDeviceList();
    Toast.makeText(this, "Deivce List Size: " + usbDeviceList.size(), Toast.LENGTH_SHORT).show();

    if (usbDeviceList.size() > 0) {

        //vid= vendor id .... pid= product id
            Toast.makeText(this, "device pid: " + device.getProductId() + " Device vid: " + device.getVendorId(), Toast.LENGTH_SHORT).show();

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多