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

如何通过Nearby API查找最近的Eddystone Beacon?Android应用实现咨询

Finding the Nearest Beacon (Including Eddystone) with Google Nearby API

Hey there! Great question—let’s break this down clearly. Google’s Nearby API doesn’t have a built-in method to directly return the "closest" Beacon, but it gives you all the data you need to calculate this yourself, whether you’re dealing with standard BLE Beacons or Eddystone devices.

Calculating Distance to Find the Nearest Beacon

The key here is using two values provided by every detected Beacon via the Nearby API:

  • RSSI: The received signal strength indicator (how strong the Beacon’s signal is when it reaches your device)
  • TxPower: The signal strength measured at 1 meter from the Beacon (a factory-set value for most Beacons)

You can use these to estimate the distance with this common formula:

distance = 10^((TxPower - RSSI) / (10 * n))

Where n is the environmental attenuation factor—use 2 for open spaces, 3 for typical indoor environments, and 4 for dense, obstacle-filled areas.

Step-by-Step Implementation

  1. Collect Detected Beacons: In your Nearby API beacon detection callback, gather all detected Beacons along with their RSSI and TxPower values.
  2. Calculate Distance for Each: Use the formula above to compute an estimated distance for every Beacon.
  3. Sort to Find the Closest: Iterate through your calculated distances to pick the Beacon with the smallest value.

Here’s a code snippet to illustrate this:

// Store detected Beacons and their estimated distances
private final Map<Beacon, Double> beaconDistanceMap = new HashMap<>();

// Inside your Nearby Beacon detection callback
@Override
public void onBeaconsFound(List<Beacon> beacons) {
    beaconDistanceMap.clear();

    for (Beacon beacon : beacons) {
        int rssi = beacon.getRssi();
        int txPower = beacon.getTxPower();

        // Skip if TxPower or RSSI is 0 (invalid for calculation)
        if (txPower == 0 || rssi == 0) continue;

        // Calculate estimated distance (using n=3 for indoor environments)
        double distance = Math.pow(10, (txPower - rssi) / (10.0 * 3));
        beaconDistanceMap.put(beacon, distance);
    }

    // Find the nearest Beacon
    Beacon nearestBeacon = null;
    double minDistance = Double.MAX_VALUE;

    for (Map.Entry<Beacon, Double> entry : beaconDistanceMap.entrySet()) {
        if (entry.getValue() < minDistance) {
            minDistance = entry.getValue();
            nearestBeacon = entry.getKey();
        }
    }

    // Handle the nearest Beacon (check type if needed)
    if (nearestBeacon != null) {
        if (nearestBeacon instanceof EddystoneUid) {
            EddystoneUid eddystoneUid = (EddystoneUid) nearestBeacon;
            Log.d("Nearest Beacon", "Eddystone UID: " + eddystoneUid.getId());
        } else if (nearestBeacon instanceof IBeacon) {
            IBeacon iBeacon = (IBeacon) nearestBeacon;
            Log.d("Nearest Beacon", "iBeacon UUID: " + iBeacon.getUuid());
        }
        Log.d("Nearest Beacon", "Estimated Distance: " + String.format("%.2f", minDistance) + "m");
    }
}

Handling Eddystone Beacons Specifically

Eddystone (Google’s open Beacon format) works exactly the same way here. The Nearby API returns Eddystone Beacons as EddystoneUid, EddystoneUrl, or EddystoneTlm objects—all of which inherit from the base Beacon class. This means you can still call getRssi() and getTxPower() to calculate distance just like with any other Beacon.

A quick note: Eddystone-TLM frames transmit additional telemetry (battery level, temperature), but you don’t need that for distance calculation—stick to RSSI and TxPower from the UID/URL frames for the most reliable estimates.

Important Caveats

  • RSSI is Volatile: Signal strength fluctuates due to obstacles, device orientation, and interference. For better accuracy, average RSSI values over multiple detections instead of using a single reading.
  • TxPower Accuracy: Some cheap Beacons may have incorrect TxPower values, which will throw off your distance calculations. Verify your Beacon’s TxPower setting if estimates seem off.
  • Estimates, Not Exact Measurements: This is always an approximation—use it for relative proximity (e.g., "which Beacon is closest") rather than precise meter-level tracking.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:49:42