2016-12-07 50 views
0

我使用範圍和Altbeacon問題和測距的Android

監測
class Appclass extends Application implements BootstrapNotifier { 
private RegionBootstrap regionBootstrap; 
private BackgroundPowerSaver backgroundPowerSaver; 
private boolean haveDetectedBeaconsSinceBoot = false; 
public static Region region1; 
private static boolean activityVisible; 
public static Appclass instances; 
public static BeaconManager beaconManager; 
public void onCreate() { 
    super.onCreate(); 
    instances = this;beaconManager=org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this); 

     beaconManager.getBeaconParsers().add(new BeaconParser() 
      .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); 
    beaconManager.setForegroundScanPeriod(1100l); 
    beaconManager.setForegroundBetweenScanPeriod(0l); 
    beaconManager.setAndroidLScanningDisabled(true); 
    beaconManager.setBackgroundBetweenScanPeriod(01); 
    beaconManager.setBackgroundScanPeriod(1100l); 
    try { 
     beaconManager.updateScanPeriods(); 

    } catch (Exception e) { 
    } 
    Log.d("", "setting up background monitoring for beacons and power     saving"); 
    // wake up the app when a beacon is seen 
    region1 = new Region("backgroundRegion", 
      null, null, null); 
    regionBootstrap = new RegionBootstrap(this, region1); 
    backgroundPowerSaver = new BackgroundPowerSaver(this); 

} 

@Override 
public void didEnterRegion(Region region) { 
    Log.d("ABC", "Enter"); 
    Log.d("ABC value", region.getId2() + " " + region.getId3()); 
    try { Intent i = new Intent(getApplicationContext(), AbcService.class); 

      startService(i); 
    } catch (Exception e) { 
    } 
} 
@Override 
public void didExitRegion(Region region) { 
    Log.d("ABC", "exit"); 
} 

@Override 
public void didDetermineStateForRegion(int i, Region region) { 

} 

,然後在我的AbcService類代碼的代碼是:

class AbcService extends Service implements BeaconConsumer, MonitorNotifier, RangeNotifier { 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onCreate() { 
    Appclass.beaconManager.bind(this); 
    super.onCreate(); 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    try { 
    } catch (Exception e) { 

    } 


    return START_STICKY; 
} 

@Override 
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) { 
    Log.d("ABC", "range"); 
} 

@Override 
public void onBeaconServiceConnect() { 
    Appclass.beaconManager.setMonitorNotifier(this); 
    Appclass.beaconManager.setRangeNotifier(this); 

} 

@Override 
public void didEnterRegion(Region region) { 
    try { 
     Log.d("ABC", "didEnterRegion"); 
     Log.d("ABC", "" + region.getId2() + region.getId3()); 
     Appclass.beaconManager.startRangingBeaconsInRegion(Appclass.region1); 

    } catch (RemoteException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void didExitRegion(Region region) { 
    try { 
     Log.d("ABC", "didExitRegion"); 
     Appclass.beaconManager.stopRangingBeaconsInRegion(Appclass.region1); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void didDetermineStateForRegion(int i, Region region) { 

} 

}

現在問題是我們每次進入beacon範圍時都會調用Application類的didEnterRegion方法而AbcService類的didEnterRegion方法沒有調用一段時間,也沒有開始測距。我的代碼中有什麼問題?

+0

在服務類中是否調用了'didDetermineStateForRegion'? 'onBeaconServiceConnect()'是否被調用? – davidgyoung

+0

它正在調用一段時間...所以我不復制是什麼問題。 – aj0822ArpitJoshi

+0

對不起,我可能需要更多信息纔能有所幫助。我建議你嘗試重複測試,也許10次,看看你的服務類中的哪些方法,onCreate,onBeaconServiceConnect和didDetermineState被每次測試運行調用。如果你做了這些測試,請注意你注意到哪些方法被調用的模式與被測試的條件。 – davidgyoung

回答

0

一些提示:

  • 啓動範圍的同時,你開始監測。沒有理由等待輸入的區域回調或在退出區域時停止測距。

  • 將記錄添加到didDetermineStateFirRegion。當應用程序啓動後出現信標時,即使設備已經處於區域中,但從之前運行時開始,該方法也會被調用。 didEnterRegion只有在匹配該區域的信標實際上消失15秒以上然後重新出現時纔會被調用。

+0

完成。謝謝davidgyoung。我開始從'didDetermineStateFirRegion'這個方法重新測距,它的工作正常。 – aj0822ArpitJoshi