异常设置(示例)带Android信标库的iBeacon解析器布局
我已经将的2.0-beta6版本集成到我的Android应用程序中。当debug打开时,我看到我的Roximity iBeacons被看到,但由于字节是意外的,所以被信标解析器拒绝 在参考应用程序中,有以下注释:异常设置(示例)带Android信标库的iBeacon解析器布局,android,ibeacon-android,Android,Ibeacon Android,我已经将的2.0-beta6版本集成到我的Android应用程序中。当debug打开时,我看到我的Roximity iBeacons被看到,但由于字节是意外的,所以被信标解析器拒绝 在参考应用程序中,有以下注释: // By default the AndroidBeaconLibrary will only find AltBeacons. If you wish to make it // find a different type of beacon, you must
// 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();
}
}
尝试设置解析器字符串的代码行会导致BeaconLayoutException:
beaconParser.setBeaconLayout(roximityBeaconParser);
异常堆栈跟踪没有有用的信息来说明字符串的错误
我做错了什么?好吧,我知道了。我从解析器函数的注释中复制了这个示例,它有一个冒号而不是一个相等的字符——讽刺的是,解析器函数的注释是错误的。因此
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”字符后的“=”字符
作为参考,在中可以找到解析代码,它具有以下模式:
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”)元素才有效。否则将引发异常
由于无法在解析器方法中查看/调试,我最终将顶级方法复制到类中(以及它所依赖的初始化变量),然后调用函数的本地副本以找出它抱怨的原因。最大的问题是,如果有一个术语(逗号分隔的值之一)与模式不完全匹配,则会抛出一个通用的“无法解析此术语”异常,这不容易调试。因此,我复制了该函数并调用它以查看抛出的内容和原因。感谢您对此进行的疑难解答,并对出现的问题表示抱歉。介意在GitHub中针对开源库打开一个问题吗?@davidgyoung当然-我会在这个周末尝试完成。如果我没有,那就唠叨我,以防我忘了。在一条异常消息(复制/粘贴错误)中也有一个错误,该消息中缺少一个术语-有两条消息“youneedanm term”。我会把这些都写进我的缺陷报告里。伟大的库,目前主要关注的是为什么现在解析器识别出我的iBeacon后我没有收到通知。知道源代码在github上的位置使这变得非常容易。
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+)");