繁体   English   中英

如何以编程方式检测Android设备是否与USB OTG连接

[英]How To Detect that Android Device is connected with USB OTG Or Not programmatically

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

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", "Decive Connected -> " + action);
//Initilizing globel 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中,您可以调用globel变量来检查当前的USB状态:

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

GlobalClass:

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;
    }

}

manifist:

 <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>

您可以使用 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();

        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM