如何检测其他信标Android -

时间:2016-09-22 08:14:45

标签: android altbeacon

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    try {                        
        Beacon beacon = new Beacon.Builder()
                .setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6") // UUID for beacon
                .setId2("1") // Major for beacon
                .setId3("5") // Minor for beacon
                .setManufacturer(0x004C) // Radius Networks.0x0118  Change this for other beacon layouts//0x004C for iPhone
                .setTxPower(-56) // Power in dB
                .setDataFields(Arrays.asList(new Long[]{0l})) // Remove this for beacon layouts without d: fields
                .build();
        BeaconParser beaconParser = new BeaconParser()
                .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
        BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
        beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() {
            @Override
            public void onStartFailure(int errorCode) {
                Log.e("tag", "Advertisement start failed with code: " + errorCode);
            }
            @Override
            public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                Log.i("tag", "Advertisement start succeeded.");
            }
        });
    }
    catch(Exception o)
    {
    }
}

我正在使用AltBeacon Library将我的手机转为信标。我正在进入我的adb logcat avdertisment start成功。但是,我想检测现在充当灯塔的其他手机,我该如何实现呢?

1 个答案:

答案 0 :(得分:0)

使用Android Beacon Library检测信标很简单。请参阅此页面上的“测距示例代码”部分:http://altbeacon.github.io/android-beacon-library/samples.html

正如上面链接的示例代码所示,您将获得大约每秒一次的回调,其中包含当时可见的所有信标的列表,其方式如下所示:

public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
      // The beacons collection contains all beacons detected in the past second
}