可見標我使用這個頁面(http://altbeacon.github.io/android-beacon-library/samples.html)在啓動一個應用程序在背景部分的示例代碼和我有一個工作程序。如何獲得從RegionBootstrap AltBeacon方法
它會檢測到信標在範圍內,即使在背景上。
的問題是我需要知道哪個信標是它(UUID,主要,次要),以便然後匹配它我的本地數據庫,並拋出一個通知,應用程序仍然在後臺。
的didEnterRegion(地區區域)的功能只有一個matchesBeacon方法,我試着做以下,以確定該信標被看到,但它拋出一個NullPointerException:
public class SightSeeing extends Activity implements BootstrapNotifier, RangeNotifier {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Region region = new Region("sightRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(
new BeaconParser(). setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")
);
BeaconManager.getInstanceForApplication(this).setRangeNotifier(this);
}
@Override
public void didEnterRegion(Region region) {
regionBootstrap.disable();
BeaconManager.getInstanceForApplication(this).setRangeNotifier(this);
try {
BeaconManager.getInstanceForApplication(this).startRangingBeaconsInRegion(region);
}
catch (RemoteException e) {
Log.e(TAG, "Can't start ranging");
}
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Iterator<Beacon> beaconIterator = beacons.iterator();
while (beaconIterator.hasNext()) {
Beacon beacon = beaconIterator.next();
//check if beacon exists in our DB and throw notification
}
}
}
我錯過這個庫很明顯或者不可能這麼做?
編輯:
我已經更新了代碼示例給你們一個更廣闊的想法,我已經試過落實FOliveira的建議沒有任何成功。
EDIT2:
更新代碼,以反映davidgyoung的建議。仍然沒有運氣。我在didRangeBeaconsInRegion()函數的第一行有一個Log.d(),並且它沒有被調用。
我已經嘗試添加BeaconManager.getInstanceForApplication(本).setRangeNotifier(本);在try/catch塊之前,結果是一樣的。
我是否實施了錯誤的建議或是否有任何其他方式使其發揮作用?
你知道didEnterRegion被調用嗎?嘗試在頂部添加一條調試線。很明顯,如果它沒有被調用,你將無法成功開始測量這個設置。 – davidgyoung 2014-09-11 06:21:36
你解決了這個問題嗎?我面臨同樣的問題。 – 2015-11-02 04:08:52