如何以编程方式检测Android设备是否已通过USB OTG连接 [英] How To Detect that Android Device is connected with USB OTG Or Not programmatically

查看:751
本文介绍了如何以编程方式检测Android设备是否已通过USB OTG连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with custom OTG fingerprint scanner. I want to check that OTG is connected to my Android device or not in a specific android activity.

推荐答案

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

在任何活动"中,您都可以调用Globel变量来检查当前的USB状态:

From any Activity you can call globel variable to check the current USB state:

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

GlobalClass:

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>

这篇关于如何以编程方式检测Android设备是否已通过USB OTG连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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