Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在android上检测usb连接_Android_Usb Otg - Fatal编程技术网

无法在android上检测usb连接

无法在android上检测usb连接,android,usb-otg,Android,Usb Otg,我有一个安卓5.1.1设备,一根OTG电缆和一个ACS读卡器。我想检测读卡器何时通过OTG电缆插入。我可以检测到分离,但是附加不成功 以下是我所拥有的: 清单文件: <uses-feature android:name="android.hardware.usb.host" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:labe

我有一个安卓5.1.1设备,一根OTG电缆和一个ACS读卡器。我想检测读卡器何时通过OTG电缆插入。我可以检测到
分离
,但是
附加
不成功

以下是我所拥有的:

清单文件:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

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

        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </activity>
</application>
我还没有请求许可,我会在检测到时询问。无论发生什么,我都看不到预期的日志

我检查了鼠标和U盘;也无法检测到它们。
Android设备支持OTG和读卡器;与其他应用程序一起检查。我猜他们会定时检查是否连接了USB设备。

尝试通过可编程方式创建意图过滤器:

1) 除去

3) 在中使用权限

UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)

案例。

将很快重试,但问题似乎是android版本5.1.1它是设备或android版本问题。解决方案的问题是,接收器将仅在应用程序启动时注册。这意味着,如果你先启动,android系统只会将广播传送给应用程序。但它不会在后台监听。@Radon8472同意,但它可以在
服务
中通过
启动完成
实现。这只是一种方法,而不是最终的解决方案。
private static final String TAG = MainActivity.class.getSimpleName();

private static final String ACTION_USB_PERMISSION = "com.guness.deleteme.USB_PERMISSION";
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 = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if (device != null) {
                        //call method to set up device communication
                        Log.e(TAG, "permission granted " + device);
                    }
                } else {
                    Log.e(TAG, "permission denied for device " + device);
                }
            }
        }
    }
};

private PendingIntent mPermissionIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e(TAG, "onCreate");
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);
}

@Override
protected void onResume() {
    super.onResume();
    Log.e(TAG, "onResume");
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.e(TAG, "onNewIntent");
}
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
private static final String TAG = MainActivity.class.getSimpleName();

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            Log.d(TAG, "USB device attached");

        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            Log.d(TAG, "USB device detached");
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e(TAG, "onCreate");
    setContentView(R.layout.activity_main);

    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiver, filter);
}
UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)