android usb弹窗权限r,Android USB权限对话框永远不会出现

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

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 deviceList = mUsbManager.getDeviceList();

for (HashMap.Entry 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;

}

}

}

这是清单的相关部分:

package="com.demo.printerDemo"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="14"

android:targetSdkVersion="17" />

我已经配置了设备,以便在连接打印机时启动我的应用程序,并在枚举期间找到设备(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中添加以下内容:

标签:host,android,printing,usb

来源: https://codeday.me/bug/20191008/1874180.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值