2015-08-26 24 views
2

OK。我最近在使用AltBeacon圖書館進行室內地理定位應用程序。 除了從後臺過渡到前臺需要隨機的時間間隔,並且速度不夠快,一切似乎都正常。 在我的代碼中,當應用程序在後臺運行時,當檢測到信標時,它不會立即切換到前景以顯示某些事件。 我需要知道這種延遲的原因以及隨機性。如果可能的話,如何解決它。從背景到背景模式的過渡在AltBeacon中是隨機的

主要活動:

public class MainActivity extends Application implements BootstrapNotifier, BeaconConsumer, RangeNotifier { 
    private RegionBootstrap regionBootstrap; 
    BeaconManager beaconManager; 
    BackgroundPowerSaver backgroundPowerSaver; 
    boolean haveDetectedBeaconsSinceBoot = false; 
    Region region; 


    ///////////////////////////////////////////////////////////////////////////////////////////////////// 
    @Override 
    public void onCreate() { 

     super.onCreate(); 
     backgroundPowerSaver = new BackgroundPowerSaver(this); 
     beaconManager = BeaconManager.getInstanceForApplication(this); 
     beaconManager.getBeaconParsers().clear(); 
     beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24")); 
     region = new Region("com.example.backgroundRegion", 
       Identifier.parse("3d4f13b4-d1fd-4049-80e5-d3edcc840b6a"), null, null); // list of 3 identifiers 
     regionBootstrap = new RegionBootstrap(this, region); 
     beaconManager.setRangeNotifier(this); 
     // set the duration of the scan to be 1.1 seconds 
     beaconManager.setBackgroundScanPeriod(2000l); 
     //beaconManager.setBackgroundMode(true);// because setBackgroundScanPeriods calls background mode automatically 
     try { 
      beaconManager.startRangingBeaconsInRegion(region); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 


     beaconManager.bind(this); 

    } 

    @Override 
    public void didEnterRegion(Region arg0) { 

     // if (haveDetectedBeaconsSinceBoot) { 

     Intent intent = new Intent(this, Test.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     this.startActivity(intent); 

     // } 
     // haveDetectedBeaconsSinceBoot = true; 

    } 


    @Override 
    public void didDetermineStateForRegion(int arg0, Region arg1) { 
     // TODO Auto-generated method stub 

    } 


    @Override 
    public void didExitRegion(Region arg0) { 
     // TODO Auto-generated method stub 
     Log.i("tag","I no longer see a beacon in the "+region.getUniqueId()); 
    } 


    @Override 
    public void onTerminate() { 
     super.onTerminate(); 
     // release whatever is needed 
     beaconManager.unbind(this); 
     beaconManager = null; 
    } 


    @Override 
    public void onBeaconServiceConnect() { 
     // TODO Auto-generated method stub 
     try { 
      //Scan lasts for SCAN_PERIOD time 
      beaconManager.setForegroundScanPeriod(3000l); 
      //Wait every SCAN_PERIOD_INBETWEEN time 
      beaconManager.setForegroundBetweenScanPeriod(0l); 
      //Update default time with the new one 
      beaconManager.updateScanPeriods(); 
     }catch (RemoteException e){ 
      e.printStackTrace(); 
     } 

    } 

    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
     // TODO Auto-generated method stub 
     for(Beacon b: beacons) 
     { 
      Log.d("tag","beacon name "+b.getBluetoothName()); 
      Log.d("tag","beacon rssi "+b.getRssi()); 
      Log.d("tag","beacon uuid "+Integer.toString(b.getServiceUuid())); 
      Log.d("tag","beacon address "+b.getBluetoothAddress()); 
      Log.d("tag","beacon distance "+b.getDistance()); 

     } 

    } 

} 

清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.androidbeacon" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="16" 
     android:targetSdkVersion="22" /> 

    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.BATTERY_STATS" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 

    <uses-feature 
     android:name="android.hardware.bluetooth" 
     android:required="false" /> 
    <uses-feature 
     android:name="android.hardware.bluetooth_le" 
     android:required="false" /> 

    <application 
     android:name="MainActivity" 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".Test" 
      android:label="@string/app_name" 
      android:launchMode="singleInstance" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

,並且測試類..

public class Test extends Activity{ 
    TextView txt1; 
    TextView txt2; 
    TextView txt3; 
    @Override 
    protected 
    void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 
     txt1=(TextView) findViewById(R.id.textView1); 
     txt2=(TextView) findViewById(R.id.textView2); 
     txt3=(TextView) findViewById(R.id.textView3); 
    } 

} 

回答

0

onCreate()不叫beaconManager.setBackgroundBetweenScanPeriod(),所以BeaconManager是使用5分鐘的默認設置。如果您希望背景到前景的轉換更具可預測性,您需要將掃描間掃描週期設置得更低 - 但這也意味着電池壽命會更短。

+0

即使我添加它我仍然有同樣的問題:/ – Hana