如何通过库android-beacon-library将android设备用于Beacon?

时间:2016-12-28 08:59:15

标签: android altbeacon

BeaconParser beaconParser = new BeaconParser()
    .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
Beacon beacon = new Beacon.Builder()
    .setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")
    .setId2("1")
    .setId3("2")
    .setManufacturer(0x0118)
    .setTxPower(-59)
    .setDataFields(Arrays.asList(new Long[]{0l}))
    .build();
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.");
    }
 });

运行此命令,“广告开始成功”。显示,是成功。 但是,mBeaconManager.startMonitoringBeaconsInRegion(区域);找不到altBeacon。

2 个答案:

答案 0 :(得分:0)

您无法在传输数据包的同一设备上检测到信标。这不是Android Beacon Library特有的限制,而是蓝牙LE的工作原理。无线电硬件系统通常设计为不接收自己的传输。

答案 1 :(得分:0)

非常感谢您的回复!

public class MainActivity extends Activity implements BeaconConsumer, RangeNotifier {

    private BeaconManager mBeaconManager;
public void onResume() {
        super.onResume();
        mBeaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
        // Detect the main Eddystone-UID frame:
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout(BeaconParser.ALTBEACON_LAYOUT));
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
        // Detect the telemetry Eddystone-TLM frame:
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
        mBeaconManager.bind(this);
}

public void onBeaconServiceConnect() {
        Log.d("youfu","onBeaconServiceConnect.");
        Region region = new Region("all-beacons-region", null, null, null);
        try {
            mBeaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        mBeaconManager.addRangeNotifier(this);
    }

@Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        Log.d("youfu","didRangeBeaconsInRegion region=== "+region);
        for (Beacon beacon: beacons) {
            if (beacon.getServiceUuid() == 0xfeaa && beacon.getBeaconTypeCode() == 0x00) {
                // This is a Eddystone-UID frame
                Identifier namespaceId = beacon.getId1();
Identifier instanceId = beacon.getId2();
                Log.d("youfu", "I see a beacon transmitting namespace id: "+namespaceId+
                        " and instance id: "+instanceId+
                        " approximately "+beacon.getDistance()+" meters away.");

                // Do we have telemetry data?
                if (beacon.getExtraDataFields().size() > 0) {
long telemetryVersion = beacon.getExtraDataFields().get(0);
                    long batteryMilliVolts = beacon.getExtraDataFields().get(1);
                    long pduCount = beacon.getExtraDataFields().get(3);
                    long uptime = beacon.getExtraDataFields().get(4);
Log.d("youfu", "The above beacon is sending telemetry version "+telemetryVersion+
                            ", has been up for : "+uptime+" seconds"+
                            ", has a battery level of "+batteryMilliVolts+" mV"+
                            ", and has transmitted "+pduCount+" advertisements.");
}
            }
        }
    }

@Override
    public void onPause() {
        super.onPause();
        mBeaconManager.unbind(this);
    }
}

The log like this:
12-30 10:52:57.232 16674-16674/? D/youfu: onBeaconServiceConnect.
12-30 10:52:58.483 16674-16870/? D/youfu: didRangeBeaconsInRegion region=== id1: null id2: null id3: null
12-30 10:52:59.591 16674-16925/? D/youfu: didRangeBeaconsInRegion region=== id1: null id2: null id3: null
12-30 10:53:00.699 16674-16945/? D/youfu: didRangeBeaconsInRegion region=== id1: null id2: null id3: null
region is null, so please help me to check the code, Thanks.