このページ ( http://altbeacon.github.io/android-beacon-library/samples.html ) のサンプル コードを「バックグラウンドでのアプリの起動」セクションで使用しており、アプリが動作しています。
バックグラウンドでもビーコンが範囲内にあるときはいつでも検出します。
問題は、どのビーコン (UUID、メジャー、マイナー) であるかを知り、それをローカル データベースと照合して、アプリがまだバックグラウンドである状態で通知をスローする必要があることです。
didEnterRegion(Region region) 関数には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(this).setRangeNotifier(this); を追加してみました。try/catch ブロックの前と結果は同じです。
提案を間違って実装しましたか、これを機能させる他の方法はありますか?