问题
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 RegionBootsrap
s. 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