我已经将https://github.com/AltBeacon/android-beacon-library的2.0-beta6版本集成到我的安卓应用程序中。在打开调试的情况下,我看到我的Roximity iBeacons被看到了,但被信标解析器拒绝了,因为字节是意外的。
在参考应用程序中有以下注意事项:
// By default the AndroidBeaconLibrary will only find AltBeacons. If you wish to make it
// find a different type of beacon, you must specify the byte layout for that beacon's
// advertisement with a line like below. The example shows how to find a beacon with the
// same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb
//
// beaconManager.getBeaconParsers().add(new BeaconParser().
// setBeaconLayout("m:2-3=aabb,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
//
// In order to find out the proper BeaconLayout definition for other kinds of beacons, do
// a Google search for "setBeaconLayout" (including the quotes in your search.)我创建了我认为是正确的格式化字符串(至少对我的测试应用程序足够好),并尝试使用以下防御性很强的代码来设置它:
List<BeaconParser> beaconParsers = beaconManager.getBeaconParsers();
if (beaconParsers != null) {
// We can add a new parser
String roximityBeaconParser = this.getRoximityBeaconParserString();
BeaconParser beaconParser = new BeaconParser();
Log.e("iBeacon", "About to set BeaconLayout with " + roximityBeaconParser);
try {
beaconParser.setBeaconLayout(roximityBeaconParser);
beaconParsers.add(beaconParser);
} catch (BeaconLayoutException e) {
e.printStackTrace();
}
}我自己的字符串导致了一个错误,所以我更改了我的函数,以获得解析器字符串,以简单地复制注释中的示例:
private String getRoximityBeaconParserString() {
String result = "m:2-3:beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";
return result;
}尝试设置解析器字符串的代码行将导致BeaconLayoutException:
beaconParser.setBeaconLayout(roximityBeaconParser);异常堆栈跟踪没有有用的信息来说明字符串的错误所在。
我做错了什么?
发布于 2014-09-26 09:09:14
好吧我想通了。我从解析器函数的注释中复制了示例,它有一个冒号,而不是一个相等的字符-讽刺的是,解析器函数的注释是错误的。因此
m:2-3:beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25是错误的,而
m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25是对的。区别在于"m:2-3“字符后的"=”字符。
作为参考,位于The Parsing Source Code的解析代码具有以下模式:
private static final Pattern I_PATTERN = Pattern.compile("i\\:(\\d+)\\-(\\d+)(l?)");
private static final Pattern M_PATTERN = Pattern.compile("m\\:(\\d+)-(\\d+)\\=([0-9A-F-a-f]+)");
private static final Pattern D_PATTERN = Pattern.compile("d\\:(\\d+)\\-(\\d+)([bl]?)");
private static final Pattern P_PATTERN = Pattern.compile("p\\:(\\d+)\\-(\\d+)");这就是对字符串进行解析的依据。
从文档中看不清楚的是,解析函数需要一个(单个)幂("p")元素,需要一个(单个)匹配("m")元素,以及至少一个标识符("i")元素有效。否则将抛出异常。
因为我们不能看到/debug到解析器方法中,所以我最终将顶级方法复制到我的类中(以及它所依赖的初始化变量),然后调用函数的本地副本来找出它出错的原因。最大的问题是,如果有一个术语(逗号分隔值之一)与模式不完全匹配,那么通用的“无法解析这个术语”异常就会被抛出,这就不容易调试了。因此,我复制了函数并调用它,以查看抛出了什么以及抛出的原因。
https://stackoverflow.com/questions/26047415
复制相似问题