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

Android端基于ProximityKit框架的Beacon多状态测距适配咨询

Hey there! Let's break down how to get ProximityKit working reliably across all three Android app states—foreground, background, and even when the app is swiped away and killed. I’ve dealt with this exact scenario with RadiusNetworks beacons before, so here’s what you need to know:

Android ProximityKit Beacon Detection: Full Coverage for All App States

1. Foreground State (App Active & Visible)

This is the easiest scenario—ProximityKit should work out of the box if you’ve set things up correctly. Here’s the key checklist:

  • Initialize ProximityKit early in your app’s lifecycle (like in your Application class or main Activity’s onCreate):
    ProximityKit.initialize(context, YOUR_PROXIMITY_KIT_ID);
    
  • Register your target beacon region and attach a ranging listener:
    Region beaconRegion = new Region("radius-networks-beacons", null, null, null);
    ProximityKit.getInstance().startRangingBeaconsInRegion(beaconRegion);
    ProximityKit.getInstance().addRangingListener(this);
    
  • Make sure your component (Activity/Fragment) implements ProximityKitRangingListener and properly logs results in didRangeBeaconsInRegion:
    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        for (Beacon beacon : beacons) {
            Log.d("BeaconLog", String.format("Found beacon: UUID=%s, RSSI=%d", 
                beacon.getUuid(), beacon.getRssi()));
        }
    }
    
  • Since the app is in the foreground, Android won’t throttle Bluetooth scans, so you’ll get consistent, frequent callbacks.

2. Background State (App Running but Not Visible)

Android restricts background Bluetooth scans to save battery, so you’ll need to tweak your setup to maintain detection:

  • Enable ProximityKit’s background scan configuration to adjust scan intervals (tune these based on your battery vs. detection needs):
    ProximityKitConfiguration config = new ProximityKitConfiguration(YOUR_PROXIMITY_KIT_ID);
    config.setBackgroundScanPeriod(10000); // Scan for 10 seconds at a time
    config.setBackgroundBetweenScanPeriod(30000); // Wait 30 seconds between scans
    ProximityKit.initialize(context, config);
    
  • Critical Permissions: Ensure you’ve requested and been granted ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION (required for Android 10+ background scanning). For Android 12+, add BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions too.
  • For more reliable background detection, wrap your scanning logic in a Foreground Service. This tells Android your app is doing important work, so it won’t throttle scans as aggressively. You’ll need to show a persistent notification (per Android rules):
    public class BeaconScanService extends Service implements ProximityKitRangingListener {
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // Create foreground notification
            Notification scanNotification = new NotificationCompat.Builder(this, "BEACON_SCAN_CHANNEL")
                    .setContentTitle("Scanning for Beacons")
                    .setContentText("Monitoring RadiusNetworks devices")
                    .setSmallIcon(R.drawable.ic_beacon)
                    .build();
            startForeground(1, scanNotification);
    
            // Initialize scanning
            ProximityKit.initialize(this, YOUR_PROXIMITY_KIT_ID);
            Region region = new Region("background-scan-region", null, null, null);
            ProximityKit.getInstance().startRangingBeaconsInRegion(region);
            ProximityKit.getInstance().addRangingListener(this);
    
            return START_STICKY;
        }
    
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            // Log background detections here
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) { return null; }
    }
    
    Don’t forget to register the service in your AndroidManifest.xml:
    <service android:name=".BeaconScanService" android:foregroundServiceType="location"/>
    

3. App Killed (Process Terminated by Task Manager)

This is the trickiest scenario—once the process is gone, your normal scanning stops. Here’s how to bring it back:

  • Use WorkManager to schedule periodic scan tasks. WorkManager respects Android’s battery optimization rules and will restart scans even after the app is killed:
    // Schedule a repeating work request to restart scanning every 15 minutes
    PeriodicWorkRequest scanWork = new PeriodicWorkRequest.Builder(BeaconScanWorker.class, 15, TimeUnit.MINUTES)
            .setConstraints(new Constraints.Builder()
                    .setRequiredNetworkType(NetworkType.NOT_REQUIRED)
                    .build())
            .build();
    WorkManager.getInstance(context).enqueueUniquePeriodicWork("BeaconScanWork", ExistingPeriodicWorkPolicy.REPLACE, scanWork);
    
    Your BeaconScanWorker would then start the foreground service to resume scanning:
    public class BeaconScanWorker extends Worker {
        public BeaconScanWorker(@NonNull Context context, @NonNull WorkerParameters params) {
            super(context, params);
        }
    
        @NonNull
        @Override
        public Result doWork() {
            Intent serviceIntent = new Intent(getApplicationContext(), BeaconScanService.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                getApplicationContext().startForegroundService(serviceIntent);
            } else {
                getApplicationContext().startService(serviceIntent);
            }
            return Result.success();
        }
    }
    
  • Enable ProximityKit’s built-in restart feature (if available in your version):
    ProximityKitConfiguration config = new ProximityKitConfiguration(YOUR_PROXIMITY_KIT_ID);
    config.setAutoRestartOnKill(true); // Tells ProximityKit to try restarting scans after process death
    
  • Battery Optimization Whitelist: Guide users to add your app to the system’s battery optimization whitelist. Without this, Android may block your WorkManager or alarm triggers in low-power modes.

Quick Troubleshooting Tips

  • Always test process death by swiping the app away from the task manager—stopping it via Android Studio doesn’t replicate real-world behavior.
  • Use didEnterRegion and didExitRegion alongside didRangeBeaconsInRegion for more reliable presence detection, especially in the background.
  • Ensure you’re using the latest version of ProximityKit—older versions may lack fixes for Android 12+ background restrictions.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:33:56