我正在将AltBeacon API集成到现有应用中。当用户进入或离开"蓝牙区域时,应用程序将在后台中启动操作。
此功能应该是可选的,用户应该从设置开始和停止扫描。
我尝试绑定/取消绑定BeaconConsumer ,但我看到,在后台 BluetoothLeScanner 正在扫描!如何阻止BluetoothLeScanner扫描?这是正确的方法吗?
以下是代码:
@Override
public void onCreate() {
super.onCreate();
LogManager.setVerboseLoggingEnabled(true);
log.debug("onCreate");
mAllBeaconsRegion = new Region(ALLBEACONS, null, null, null);
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBackgroundPowerSaver = new BackgroundPowerSaver(this);
mBeaconManager.setBackgroundBetweenScanPeriod(60000L);
mBeaconManager.setBackgroundScanPeriod(2100L);
// wake up the app when a beacon is seen
mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion);
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
dsGlobal = new DbGlobalsHelper(getApplicationContext());
boolean isBeaconScan = Utils.isBoolean(dsGlobal.getCursorGlobalsByKey(Constants.DB_KEY_BEACON_SCAN));
if(isBeaconScan){
mBeaconManager.bind(this);
}else{
mBeaconManager.unbind(this);
}
}
// Set Regions to monitor at boot/startup
private void setRegionsAtBoot(){
Log.i(TAG, "setRegionsAtBoot");
log.info("setRegionsAtBoot");
SimpleBeaconStore beaconStore = new SimpleBeaconStore(this);
List<SimpleBeacon> beacons = beaconStore.getBeacons();
for (Iterator<SimpleBeacon> iterator = beacons.iterator(); iterator.hasNext();) {
SimpleBeacon simpleBeacon = iterator.next();
List<Identifier> listB = new ArrayList<Identifier>();
StringTokenizer st = new StringTokenizer(simpleBeacon.getBeaconUuid(), "#");
while (st.hasMoreTokens()) {
listB.add(new Identifier(Identifier.parse(st.nextToken())));
}
try {
Log.i(TAG, "setRegionsAtBoot " + simpleBeacon.getId());
log.info("setRegionsAtBoot " + simpleBeacon.getId());
Region region = new Region(simpleBeacon.getId(), listB);
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
// Start actions
if (region.getUniqueId().equals(ALLBEACONS)){
return;
}
if (beacons.size() > 0) {
for (Beacon beacon: beacons) {
Log.d(TAG, "Beacon "+beacon.toString()+" is about "+beacon.getDistance()+" meters away, with Rssi: "+beacon.getRssi());
log.debug("Beacon " + beacon.toString() + " is about " + beacon.getDistance() + " meters away, with Rssi: " + beacon.getRssi());
datasource = new DbZoneHelper(getApplicationContext());
ZoneEntity ze = datasource.getCursorZoneByName(region.getUniqueId());
if (beacon.getDistance() < ze.getRadius() && !ze.isStatus()) {
//
Log.i(TAG, "Beacon " + beacon.toString() + " Just became less than " + ze.getRadius() + " meters away.");
log.info("*** Beacon " + beacon.toString() + " Just became less than " + ze.getRadius() + " meters away. ***");
String transitionType = getTransitionString(1);
NotificationUtil.sendNotification(getApplicationContext(), transitionType, region.getUniqueId());
worker = new Worker(getApplicationContext());
worker.handleTransition(1, region.getUniqueId(), Constants.BEACON);
}
if (beacon.getDistance() > (ze.getRadius() * EXITMULTIPLICATOR) && ze.isStatus()) {
//
Log.i(TAG, "Beacon "+ beacon.toString()+" Just became over " + ze.getRadius() * EXITMULTIPLICATOR + " meters away.");
log.info("*** Beacon " + beacon.toString() + " Just became over " + ze.getRadius() * EXITMULTIPLICATOR + " meters away. ***");
String transitionType = getTransitionString(2);
NotificationUtil.sendNotification(getApplicationContext(), transitionType, region.getUniqueId());
worker = new Worker(getApplicationContext());
//
worker.handleTransition(2, region.getUniqueId(), Constants.BEACON);
}
}
}
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
}
@Override
public void didEnterRegion(Region region) {
}
@Override
public void didExitRegion(Region region) {
if (region.getUniqueId().equals(ALLBEACONS)){
return;
}
datasource = new DbZoneHelper(getApplicationContext());
ZoneEntity ze = datasource.getCursorZoneByName(region.getUniqueId());
if (ze.isStatus()) {
//
Log.i(TAG, "Beacon " + region.getUniqueId() + " Just exited region.");
log.info("Beacon " + region.getUniqueId() + " Just exited region.");
String transitionType = getTransitionString(2);
NotificationUtil.sendNotification(getApplicationContext(), transitionType, region.getUniqueId());
worker = new Worker(getApplicationContext());
worker.handleTransition(2, region.getUniqueId(), Constants.BEACON);
}
}
@Override
public void onBeaconServiceConnect() {
Log.d(TAG, "onBeaconServiceConnect.");
log.debug("onBeaconServiceConnect");
setRegionsAtBoot();
mBeaconManager.setRangeNotifier(this);
}
public void bind(){
mBeaconManager.bind(this);
}
public void unbind(){
mBeaconManager.unbind(this);
}
我也从我的设置中调用bind / unbind。
答案 0 :(得分:1)
显示的代码使用<ul id="dynamic">
</ul>
类在后台持续扫描信标。此类主要用于永不停止扫描信标。 RegionBootstrap
在构建时与扫描服务绑定,实际上从不解除绑定。因此显示手动绑定和解除绑定的代码无效。
如果您想在不同时间开始和停止扫描,请不要使用RegionBootstrap
课程,而应使RegionBootstrap
或Application
实施{{1} }和绑定/取消绑定,如监控示例代码中所述:http://altbeacon.github.io/android-beacon-library/samples.html