2014-09-25 50 views
0

我已將https://github.com/AltBeacon/android-beacon-library的2.0-beta6版本集成到我的Android應用程序中。打開調試後,我看到我的Roximity iBeacons被看到,但由於字節意外而被信標解析器拒絕。例外設置(示例)使用Android Beacon Library的iBeacon解析器佈局

在參考應用有以下注意事項:

// 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); 

的異常堆棧跟蹤沒有有用的信息,說什麼是錯的字符串。

我在做什麼錯?

回答

2

好吧,算了一下。我從解析器函數的註釋中複製了這個例子,它有一個冒號而不是一個平等的字符 - 具有諷刺意味的是,解析器函數的註釋是錯誤的。因此

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 「)元素有效。否則會引發異常。

由於我們無法看到/調試到解析器方法,我最終將頂級方法複製到我的類(以及它所依賴的初始化變量),然後調用函數的本地副本來找出爲什麼它在抱怨。最大的問題是,如果有一個術語(逗號分隔值之一)與模式不完全匹配,那麼會拋出一個通用的「無法解析這個術語」的異常,這並不容易調試。因此,我複製了該函數並調用它來查看拋出的內容和原因。

+0

感謝您排除故障,併爲此感到遺憾。在GitHub中打開一個針對開源庫的問題? – davidgyoung 2014-09-26 17:47:43

+0

@davidgyoung當然 - 我會盡力在這個週末完成。如果我不那麼嘮叨我,萬一我忘了。在缺少術語時出現的異常消息(複製/粘貼錯誤)中也存在錯誤 - 有兩個「您需要一個術語」消息。我會把所有這些放在我的缺陷報告中。偉大的圖書館,目前專注於爲什麼我現在沒有收到通知解析器識別我的iBeacon。知道源代碼在github上的位置使得這一點變得如此簡單。 – Colin 2014-09-26 17:54:39