gpt4 book ai didi

Android USB 权限对话框从不出现

转载 作者:行者123 更新时间:2023-11-29 00:33:06 29 4
gpt4 key购买 nike

我编写了一个简单的应用程序,用于向通过 USB 连接到 Android 4.0 平板电脑的 USB 打印机发送命令。出于某种原因,我无法通过获得声明接口(interface)和打开连接的权限。这是相关代码:

public class TestPrintActivity extends Activity {

private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private UsbInterface mInterface;
private UsbEndpoint mBulkIn;
private UsbEndpoint mBulkOut;
private static final String ACTION_USB_PERMISSION =
"com.demo.xprinter.USB_PERMISSION";
private PendingIntent mPermissionIntent;
private BroadcastReceiver mUsbReceiver;

@Override

private static final String ACTION_USB_PERMISSION =
"com.demo.printerDemo.USB_PERMISSION";



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_print);

mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);

mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
registerReceiver(mUsbReceiver, filter);

mUsbReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
//call method to set up device communication
openPort(device);
}
}
else {
Toast.makeText(getApplicationContext(), "Denied!", Toast.LENGTH_SHORT).show();
}
}
}
}
};

}

public void onResume() {
super.onResume();

HashMap<String,UsbDevice> deviceList = mUsbManager.getDeviceList();

for (HashMap.Entry<String,UsbDevice> entry : deviceList.entrySet()) {
UsbDevice aDevice = entry.getValue();
if ((aDevice.getProductId() == 8965) && (aDevice.getVendorId() == 1659) ){

if (mUsbManager.hasPermission(aDevice))
openPort(aDevice);
else
mUsbManager.requestPermission(aDevice, mPermissionIntent);
break;
}
}


}

这是 list 的相关部分:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.printerDemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-feature android:name="android.hardware.usb.host"/>

我已配置设备,以便在连接打印机时启动我的应用程序,并在枚举期间找到设备 (onResume()) 并调用权限请求。但是,无论出于何种原因,我从未看到“请求权限”对话框(我在网上看到过它的屏幕截图),也从未调用过 onReceive()。任何想法为什么会发生这种情况?

最佳答案

1) 为什么要声明两个成员 ACTION_USB_PERMISSION?

2) 在 onCreate() 中尝试像这样注册您的 Intent,也许有区别:

IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(ACTION_USB_PERMISSION);
context.registerReceiver(usbReceiver, filter);

3) 由于 Android (http://code.google.com/p/android/issues/detail?id=25703) 中的错误,我不确定是否调用了 ACTION_USB_ACCESSORY_ATTACHED 操作。尝试在您的 AndroidManifest.xml 中添加以下内容:

    <activity ...>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter"/>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

关于Android USB 权限对话框从不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274285/

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