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

Android API 30+下Altbeacon扫描功能报错问题咨询

解决Targeting S+时Altbeacon扫描的PendingIntent标记错误

问题原因

Android 12(API 31及以上)强制要求创建PendingIntent时必须指定FLAG_IMMUTABLE或FLAG_MUTABLE标记,而你使用的android-beacon-library-2.17.1版本未适配该系统要求,导致扫描功能触发报错。

解决方案

方案一:升级Altbeacon库(推荐)

将android-beacon-library版本升级至2.19.0及以上,官方已在该版本修复了Android 12的PendingIntent标记适配问题,升级后无需额外修改代码即可正常使用扫描功能。

方案二:自定义扫描器类(临时适配)

如果暂时无法升级库,可通过自定义CycledLeScanner子类,手动添加FLAG_IMMUTABLE标记:

  1. 创建自定义扫描器类:
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import org.altbeacon.beacon.service.scanner.CycledLeScanner;

public class CustomCycledLeScanner extends CycledLeScanner {
    public CustomCycledLeScanner(Context context, long scanPeriod, long betweenScanPeriod, boolean backgroundFlag) {
        super(context, scanPeriod, betweenScanPeriod, backgroundFlag);
    }

    @Override
    protected PendingIntent getWakeUpOperation() {
        Intent intent = new Intent("org.altbeacon.beacon.wakeup");
        intent.setPackage(mContext.getPackageName());
        // 添加FLAG_IMMUTABLE标记,符合Android 12+要求
        return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    }
}
  1. 在初始化BeaconManager时设置自定义扫描器:
BeaconManager.getInstanceForApplication(this).setScannerClass(CustomCycledLeScanner.class);

注意事项

  • 优先选择升级库,自定义子类可能会在后续库版本更新时出现兼容性问题。
  • 对于无需修改PendingIntent内容的场景,使用FLAG_IMMUTABLE是官方推荐的安全选项。

内容的提问来源于stack exchange,提问作者HYunsu CHoi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 04:50:21