You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 10/11外接USB指纹设备权限申请始终返回false求助

Android 10/11 外接USB设备权限问题排查

问题场景

在Android 10、11系统中使用Futronic USB指纹设备时,点击权限弹窗的「确定」按钮后,代码intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)始终返回false,但该功能在Android 5设备上可正常运行。相关SDK核心代码如下:

public UsbDeviceDataExchangeImpl( Context ctx, Handler trg_handler )
{
    context = ctx;
    handler = trg_handler;
            
    mDevManager = (UsbManager)ctx.getSystemService(Context.USB_SERVICE);
    mPermissionIntent = PendingIntent.getBroadcast(ctx, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE);
    
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    context.registerReceiver(mUsbReceiver, filter);
}


private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action))
        {
            synchronized (mPermissionIntent)
            {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) 
                {
                    if(device != null)
                    {
                        usb_ctx = OpenDevice(device);
                    }
                    
                    handler.obtainMessage(MESSAGE_ALLOW_DEVICE).sendToTarget();
                } 
                else
                {
                    handler.obtainMessage(MESSAGE_DENY_DEVICE).sendToTarget();
                }
                                    
            }
            
        }
    }
};

Android 10/11的USB权限额外限制及解决方案

Android 10(API 29)和Android 11(API 30)对外接USB设备新增了多项权限管控,以下是针对性的排查和修复点:

  • USB设备过滤声明
    从Android 10开始,需要在AndroidManifest.xml中明确声明支持的USB设备,否则系统可能拦截权限请求。配置示例:

    <uses-feature android:name="android.hardware.usb.host" android:required="false" />
    <activity android:name="你的主活动类">
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>
        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/usb_device_filter" />
    </activity>
    

    同时在res/xml/usb_device_filter.xml中添加Futronic设备的VID/PID(需从官方文档获取对应值):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <usb-device vendor-id="设备VID" product-id="设备PID" />
    </resources>
    
  • PendingIntent标志位兼容
    代码中使用的PendingIntent.FLAG_IMMUTABLE在Android 12+是强制要求,但在Android 10/11中可能导致广播无法正确传递权限结果。建议根据系统版本动态选择标志:

    int pendingIntentFlag;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        pendingIntentFlag = PendingIntent.FLAG_IMMUTABLE;
    } else {
        pendingIntentFlag = PendingIntent.FLAG_UPDATE_CURRENT;
    }
    mPermissionIntent = PendingIntent.getBroadcast(ctx, 0, new Intent(ACTION_USB_PERMISSION), pendingIntentFlag);
    
  • 前台权限要求
    Android 10开始加强后台权限管控,USB权限请求必须在应用前台可见状态下发起,否则会被系统拦截。确保调用mDevManager.requestPermission(device, mPermissionIntent)时,应用处于前台界面。

  • 预检查权限状态
    在发起权限请求前,可先调用mDevManager.hasPermission(device)检查是否已持有权限,避免重复请求导致的异常。

额外验证步骤

  1. 检查设备系统设置中,应用的「USB权限」是否被手动禁用(部分定制ROM会有独立的USB权限开关)。
  2. 测试其他USB设备在目标系统上的权限请求流程,排除设备本身的兼容性问题。
  3. 尝试更新Futronic官方SDK到最新版本,确认是否已适配Android 10/11的权限规则。

内容的提问来源于stack exchange,提问作者Lorensius W. L. T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 04:06:25