2016-11-10 12 views
0

我需要的範圍使用Altbeacons庫(正常工作在前臺模式)iBeacons的背景與範圍圖書館Altbeacon

背景在我的Android顯示器標日誌從不顯示了didRangeBeaconsInRegion方法的消息。

didEnterRegion上,消息顯示正確。

我試過下面的代碼但沒有成功,任何人都可以指導我如何做到這一點?

public class INBeaconApplication extends Application implements BootstrapNotifier, RangeNotifier { 
private BeaconManager beaconManager; 
private ArrayList<INBeacon> userBeaconArrayList; 
private INBeaconUser inBeaconUser; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.d("INBeacon", "Application created"); 

    //BeaconManager and custom beaconParser layout 
    beaconManager = BeaconManager.getInstanceForApplication(this); 
    beaconManager.getBeaconParsers().clear(); 
    beaconManager.getBeaconParsers().add(new BeaconParser() 
      .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); 
    beaconManager.setRegionStatePeristenceEnabled(false); 

    //Set the BeacomManager background between scan periods 
    long period = 1000 * 30; //equal to 30 seconds 
    beaconManager.setBackgroundBetweenScanPeriod(period); 

    // wake up the app when a beacon is seen 
    Region region = new Region(Utils.REGION_ID, Identifier 
      .parse(Utils.INBEACON_UUID), null, null); 
    new RegionBootstrap(this, region); 

    new BackgroundPowerSaver(this); 
} 



boolean checkPermissionForRange(){ 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     return getApplicationContext().checkSelfPermission(
       Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED; 
    } 
    return true; 
} 

@Override 
public void didEnterRegion(Region region) { 
    Log.d("INBeacon", "didEnterRegion"); 
    Utils.sendNotification(this, "Test"); 
    if (checkPermissionForRange()) return; 
    Log.d("INBeacon", "permissions ok"); 
    try { 
     beaconManager.startRangingBeaconsInRegion(region); 
     Log.d("INBeacon","startRanging"); 
    } catch (RemoteException e) { 
     Log.e("INBeacon",e.getMessage()); 
    } 
} 

@Override 
public void didExitRegion(Region region) { 
    Log.d("INBeacon", "didExitRegion"); 
    try { 
     beaconManager.stopRangingBeaconsInRegion(region); 
    } catch (RemoteException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) { 
    Log.d("INBeacon", "didRangeBeaconsInRegion"); 
    ArrayList<Beacon> newBeacons = new ArrayList<>(collection); 
     .............code to do what i need with the beacons..... 
} 

回答

0

它看起來像代碼只需要調用:

beaconManager.setRangeNotifier(this); 
+0

大衛,我添加和它的工作了,但這種方法顯示爲過時,這樣行嗎? – ClaudioBlanco

+0

此外,這使得它像應用程序打開時一樣掃描,還有另一種方式可以使它僅在間隔中查找它們,例如當我設置beaconManager.setBackgroundBetweenScanPeriod(句點)時; – ClaudioBlanco

+1

對不起,不推薦使用的版本是beaconManager.addRangeNotifier(this);'你應該每次在你的後臺掃描設置中看到30秒的測距停止。 – davidgyoung