我有一台android 5.1.1设备,一根OTG线和一台ACS CardReader。我想要检测CardReader何时通过OTG线插入。我可以检测到DETACH,但在ATTACH上没有检测到。
这就是我所拥有的:
清单文件:
<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>XML筛选器文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--usb-device vendor-id="1234" product-id="5678" class="255" subclass="66" protocol="1" /-->
<usb-device />
</resources>MainActivity:
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");
}我还没有请求许可,当检测到的时候我会问的。无论如何,我看不到预期的日志。
我已经检查了鼠标和U盘,也检测不到它们。Android设备支持OTG和CardReader;与其他应用程序一起检查。我猜他们有计时器定期检查是否连接了USB设备。
发布于 2016-10-11 16:53:33
尝试以可编程的方式创建意图过滤器:
1)删除
<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" />清单文件中的行(也可以删除XML筛选器文件)
2)在MainActivity中创建IntentFilter (如下所示):
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);
}3)使用中的权限
UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)凯斯。
https://stackoverflow.com/questions/39897843
复制相似问题