这是我可以添加Observers的Singleton类:
以下链接的完整类实现:
相关代码:
public void addSensor(AbstractBeaconSensor sensor){
lock.lock();
if(sensors.isEmpty() && !isBinded) {
beaconManager.bind(this);
Log.d(TAG, "Binding+++++++++++++++++++");
isBinded = true;
}
sensors.add(sensor);
lock.unlock();
}
public void removeSensor(AbstractBeaconSensor sensor){
lock.lock();
sensors.remove(sensor);
if(sensors.isEmpty() && isBinded){
try {
beaconManager.stopRangingBeaconsInRegion(myRegion);
} catch (RemoteException e) {
e.printStackTrace();
}
beaconManager.unbind(this);
isBinded = false;
}
lock.unlock();
}
@Override
public void onBeaconServiceConnect() {
Log.d(TAG, "On beacon connect+++++++++++++++++");
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Log.d(TAG, "++ " + beacons.size());
long time = System.currentTimeMillis();
try {
//lock.lock();
for(AbstractBeaconSensor sensor : sensors) {
sensor.setData(beacons, time);
}
} finally {
//lock.unlock();
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(myRegion);
} catch (RemoteException e) {
Log.d(TAG, "Got exception" + e.getMessage());
}
}
问题是添加一个AbstractSensor后,扫描工作正常。
在调用stopRangingBeaconInRegion()和unbind(),然后再次调用bind()之后,该服务似乎只保持在后台扫描模式(我每隔30秒获得一个信标列表)。
05-12 13:34:39.969 25700-25700/interdroid.swan D/BeaconSingleton: On beacon connect+++++++++++++++++
05-12 13:34:42.497 25700-25700/interdroid.swan D/BeaconSingleton: Binding+++++++++++++++++++
05-12 13:34:42.502 25700-25700/interdroid.swan D/BeaconSingleton: On beacon connect+++++++++++++++++
05-12 13:34:43.728 25700-27614/interdroid.swan D/BeaconSingleton: ++ 5
05-12 13:34:44.848 25700-27635/interdroid.swan D/BeaconSingleton: ++ 1
05-12 13:34:46.029 25700-27655/interdroid.swan D/BeaconSingleton: ++ 3
05-12 13:34:47.161 25700-27672/interdroid.swan D/BeaconSingleton: ++ 7
05-12 13:34:54.091 25700-27778/interdroid.swan D/BeaconSingleton: ++ 6
05-12 13:34:55.237 25700-27799/interdroid.swan D/BeaconSingleton: ++ 7
05-12 13:34:56.423 25700-27817/interdroid.swan D/BeaconSingleton: ++ 4
05-12 13:34:57.566 25700-27833/interdroid.swan D/BeaconSingleton: ++ 1
05-12 13:34:58.747 25700-27850/interdroid.swan D/BeaconSingleton: ++ 3
05-12 13:34:59.894 25700-27873/interdroid.swan D/BeaconSingleton: ++ 5
05-12 13:35:01.053 25700-27891/interdroid.swan D/BeaconSingleton: ++ 7
05-12 13:35:02.199 25700-27913/interdroid.swan D/BeaconSingleton: ++ 6
05-12 13:35:03.354 25700-27930/interdroid.swan D/BeaconSingleton: ++ 3
05-12 13:35:04.504 25700-27951/interdroid.swan D/BeaconSingleton: ++ 3
05-12 13:35:05.648 25700-27968/interdroid.swan D/BeaconSingleton: ++ 2
05-12 13:35:11.405 25700-28056/interdroid.swan D/BeaconSingleton: ++ 8
05-12 13:35:12.548 25700-28071/interdroid.swan D/BeaconSingleton: ++ 4
05-12 13:35:13.703 25700-28088/interdroid.swan D/BeaconSingleton: ++ 6
05-12 13:35:38.580 25700-25700/interdroid.swan D/BeaconSingleton: Binding+++++++++++++++++++
05-12 13:35:38.585 25700-25700/interdroid.swan D/BeaconSingleton: On beacon connect+++++++++++++++++
05-12 13:35:39.789 25700-28555/interdroid.swan D/BeaconSingleton: ++ 3
05-12 13:36:24.995 25700-25700/interdroid.swan D/BeaconSingleton: Binding+++++++++++++++++++
05-12 13:36:25.022 25700-25700/interdroid.swan D/BeaconSingleton: On beacon connect+++++++++++++++++
05-12 13:36:26.224 25700-29401/interdroid.swan D/BeaconSingleton: ++ 6
正如您在logcat中看到的那样,在调用第二个绑定后,对范围通知程序的调用非常罕见。
如果有人遇到类似问题,我们非常感谢您的帮助。
答案 0 :(得分:1)
目前还不清楚应用程序的生命周期序列是什么,但是有些事情导致库在第二次bind()调用时认为它处于后台模式。您可以通过在onBeaconServiceConnect()
中添加以下行来更正此问题:
beaconManager.setBackgroundMode(false);