Exception when trying to add a BeaconParser to AltBeacon lib

我怕爱的太早我们不能终老 提交于 2019-12-10 09:46:57

问题


I have been trying to modify the reference app from AltBeacon in order to detect iBeacons.

In RangingActivity, I replaced

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ranging);
    beaconManager.bind(this);
}

with

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ranging);
    BeaconParser bp = new BeaconParser();
    bp.setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24");
    List<BeaconParser> lbp = beaconManager.getBeaconParsers();
    lbp.add(bp);

    beaconManager.bind(this);
}

In order to apply the modifications to detect an iBeacon which I found here.

However this throws as lbp is a java.util.Collections$UnmodifiableRandomAccessList. I am new to Java but I guess this is a list to which I cannot add items and something must have changed in the library between the solution in the second link and now.

Do anybody know what would be the correct way to add a new BeaconParser or what I am doing wrong here ?


回答1:


I already answered your question on GitHub but I felt like I should answer in here as well to help other people who faces this problem.

I have checked the source code (v2.1.3), most probably you are adding new parser after binding beacon service (which forbids future custom layout adding).

public List<BeaconParser> getBeaconParsers() {
    if (isAnyConsumerBound()) {
        return Collections.unmodifiableList(beaconParsers);
    }
    return beaconParsers;
}

In order to add any layout parser afterwards you need to unbind all service connections which also includes all RegionBootsraps. If you defined one, you need to disable it since it also establishes a connection with BeaconService.

However, I believe you don't need to add any custom layout later time. If you are storing your layout in remote, you should first fetch them then bind BeaconManager.

BTW, If you believe this ability should be added to library, you can create a request on library's GitHub page: AltBeacon GitHub



来源:https://stackoverflow.com/questions/28595468/exception-when-trying-to-add-a-beaconparser-to-altbeacon-lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!