如何解析代码中的信标ID? (Android)

时间:2019-04-01 05:10:33

标签: android beacon

我想在代码中解析信标的ID,但是我不知道该怎么做。

这是我的代码:

.onEnter(new Function1<ProximityZoneContext, Unit>() {
                @Override
                public Unit invoke(ProximityZoneContext proximityContext) {
                    beacon_data.onNext(proximityContext.getDeviceId());
                    //beacon_data.onComplete();
                    Log.d("BeaconOnEnter",proximityContext.getDeviceId());
                    return null;
                }
            })
            .onExit(new Function1<ProximityZoneContext, Unit>() {
                @Override
                public Unit invoke(ProximityZoneContext proximityContext) {
                    Log.d("BeaconOnExit",proximityContext.getDeviceId());
                    return null;
                }
            })
            .onContextChange(new Function1<Set<? extends ProximityZoneContext>, Unit>() {
                @Override
                public Unit invoke(Set<? extends ProximityZoneContext> proximityZoneContexts) {
                    /*Iterator<? extends ProximityZoneContext> iter = proximityZoneContexts.iterator();
                    while(iter.hasNext()){
                        Log.d("BeaconOnContext",iter.toString());
                    }*/
                    Log.d("BeaconOnContext",proximityZoneContexts.toString());
                    return null;
                }
            })

这是ProximityZoneContext接口。

interface ProximityZoneContext {
val deviceId: String
val tag: String
val attachments: Map<String, String>}

这是EstimoteProximityZoneContext。

internal data class EstimoteProximityZoneContext(
    override val deviceId: String,
    override val tag: String,
    override val attachments: Map<String, String>) : ProximityZoneContext

这就是结果。

  

D / BeaconOnEnter:96116cc225803b78ad43e6461dbd4125

     

D / BeaconOnContext:   [EstimoteProximityZoneContext(deviceId = 96116cc225803b78ad43e6461dbd4125,   tag = monitoringexample-8mi,附件= {}),   EstimoteProximityZoneContext(deviceId = 45ccdc54e8f2c30f692f594c8ca7eb06,   tag = monitoringexample-8mi,附件= {}),   EstimoteProximityZoneContext(deviceId = 16791f300532ddf9032c92e11a9d1d38,   tag = monitoringexample-8mi,附件= {})]

     

D / BeaconOnExit:45ccdc54e8f2c30f692f594c8ca7eb06

我需要deviceId。我尝试了很多方法,但没有成功。

onEnter,onExit函数可以使用getDeveiceId()函数打印ID。

但是,它不适用于onContextChange。

如何解析?

1 个答案:

答案 0 :(得分:0)

我解决了。我不知道如何使用set。

ProximityZoneContext[] contextsArray = 
    proximityZoneContexts.toArray(new ProximityZoneContext[0]);
String beacon1ID = contextsArray[0].getDeviceId();
String beacon2ID = contextsArray[1].getDeviceId();
String beacon3ID = contextsArray[2].getDeviceId();

成功了!