Android AltBeacon 测距

Android AltBeacon ranging

我想扫描我周围的任何一种信标。基本上,如果我理解正确的话,您需要在扫描之前知道他们的 ID?你会如何扫描你周围的任何信标并提取它们的 UUID/Factory Id/etc.?

这是我当前的代码:

public class TestActivity extends Activity implements BeaconConsumer {

private BeaconManager beaconManager;

public static final String TAG = "TestActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startBeaconScan();
}

public void startBeaconScan() {
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.setDebug(true);
    beaconManager.bind(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(final Region region) {
            Log.d(TAG, "ENTERED REGION: "+region);
        }

        @Override
        public void didExitRegion(Region region) {}

        @Override
        public void didDetermineStateForRegion(int i, Region region) {}
    });
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
            Log.d(TAG, "BEACONS: "+collection.size());
        }
    });
}

@Override
public void onBeaconServiceConnect() {
    try {
        beaconManager.startMonitoringBeaconsInRegion(new Region("myRegion", null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

}

我在输出中得到了这些消息,但没有别的消息:

beacon detected : id1: f7826da5-4fa1-4e48-8024-bc5b70e0891a id2: 49089 id3: 34388

所以问题是,如何在事先不知道 ID 的情况下检测到这些信标?

两点:

  1. 创建一个包含所有空标识符的 Region 定义本质上是一个匹配所有信标标识符的通配符,因此您无需预先知道它们。显示的代码已经有这个。

  2. 要获取检测到的信标列表,您需要开始测距。显示的代码有一个狂暴的回调,但它没有开始测距。

这一行之后:

beaconManager.startRangingBeaconsInRegion(new Region("myRegion", null, null, null));

添加这个:

beaconManager.startMonitoringBeaconsInRegion(new Region("myRegion", null, null, null));