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

AltBeacon库无法检测Beacon问题排查求助

问题

我附近有2个Beacon设备,厂商说明它们的UUID均为:fda50693-a4e2-4fb1-afcf-c6eb07647825。使用原生Android API扫描时,每秒都能获取到Beacon的MAC地址和scanRecord;但使用AltBeacon库扫描时,didEnterRegion回调从未触发,仅应用启动时didDetermineStateForRegion回调返回状态0,请问问题出在哪里?

原生扫描代码

ScanCallback leScanCallback =
    new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            // scanned result data bytes sample:onScanResult-> deviceName: R24110120, rssi: -52, 
            // deviceMacAddress: 52:0A:24:11:00:78, 
            //scanRecord: 0201061aff4c000215fda50693a4e24fb1afcfc6eb0764782500010002d80a0952323431313031323011160318520a24110078000100020603e864000000
        }
};
var bluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
ScanSettings settings = new ScanSettings.Builder().build();
ScanFilter scanFilter = new ScanFilter.Builder().build();
ArrayList<ScanFilter> scanFilters = new ArrayList<>();
scanFilters.add(scanFilter);
bluetoothLeScanner.startScan(scanFilters, settings, leScanCallback);

AltBeacon扫描代码

beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
        setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.addMonitorNotifier(new MonitorNotifier() {
    @Override
    public void didEnterRegion(Region region) {
        var regionLogStr = String.format("didEnterRegion: %s", region.toString());
        Log.i(TAG, "onScanResult - I just saw an beacon for the first time! - " + regionLogStr);
    }
    @Override
    public void didExitRegion(Region region) {
        Log.i(TAG, "onScanResult - I no longer see an beacon");
    }
    @Override
    public void didDetermineStateForRegion(int state, Region region) {
        var regionLogStr = String.format("didDetermineStateForRegion: %s, mac: %s", region.toString(), region.getBluetoothAddress());
        Log.i(TAG, "onScanResult - I have just switched from seeing/not seeing beacons: " + state + " - " + regionLogStr);
    }
});
beaconManager.startMonitoring(new Region("myMonitoringUniqueId", null, null, null));

启动时日志

onScanResult - I have just switched from seeing/not seeing beacons: 0 - didDetermineStateForRegion: id1: null id2: null id3: null, mac: null


问题诊断与解决

1. 权限与位置服务缺失(核心原因)

Android 10+ 版本对BLE扫描有严格的权限要求:

  • 必须授予ACCESS_FINE_LOCATION(或ACCESS_COARSE_LOCATION)权限
  • 设备位置服务必须处于开启状态
  • Android 12+ 额外需要BLUETOOTH_SCAN权限,后台扫描还需ACCESS_BACKGROUND_LOCATION权限

原生扫描未设置过滤可能绕过部分系统限制,但AltBeacon库严格遵循权限规则,权限不足或位置服务关闭会直接导致扫描失效,触发didDetermineStateForRegion返回状态0(未知状态)。

2. Region匹配逻辑优化

你当前使用new Region("myMonitoringUniqueId", null, null, null)匹配所有Beacon,虽然理论上可行,但显式指定UUID可避免解析误差,提高匹配准确性:

beaconManager.startMonitoring(new Region("myMonitoringUniqueId", 
    Identifier.parse("fda50693-a4e2-4fb1-afcf-c6eb07647825"), 
    null, null));

3. 扫描配置调整

AltBeacon默认扫描周期可能不适合你的场景,可手动调整参数确保持续扫描:

// 后台扫描间隔设为0,保持持续扫描
beaconManager.setBackgroundBetweenScanPeriod(0);
// 后台每次扫描时长设为1.1秒
beaconManager.setBackgroundScanPeriod(1100);

4. 调试验证

添加测距回调确认库是否解析到Beacon数据,快速定位问题:

beaconManager.addRangeNotifier((beacons, region) -> {
    for (Beacon beacon : beacons) {
        Log.i(TAG, "解析到Beacon: UUID=" + beacon.getId1() + ", Major=" + beacon.getId2() + ", Minor=" + beacon.getId3());
    }
});
beaconManager.startRangingBeaconsInRegion(new Region("ranging", null, null, null));

如果测距回调无输出,说明扫描未获取到数据,优先排查权限和位置服务问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:47:11