gpt4 book ai didi

android - 无法在 Android 上检测到 USB 连接

转载 作者:太空狗 更新时间:2023-10-29 14:46:16 24 4
gpt4 key购买 nike

我有一个 android 5.1.1 设备、一个 OTG 数据线和一个 ACS 读卡器。我想检测 CardReader 何时通过 OTG 电缆插入。我可以检测到 DETACH,但没有检测到 ATTACH

这是我的:

list 文件:

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

主要 Activity :

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

我还没申请权限,等检测到再问。无论如何我都看不到预期的日志。

好吧,我已经检查了鼠标和 USB 内存棒;也无法检测到它们。Android 设备支持 OTG 和 CardReader;检查与其他应用程序。我猜他们有定时器定期检查是否连接了 USB 设备。

最佳答案

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

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

list 文件中的行(您也可以删除 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)

案例。

关于android - 无法在 Android 上检测到 USB 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39897843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com