好啊。我最近一直在使用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);
    }

}

最佳答案

您的onCreate()不调用beaconManager.setBackgroundBetweenScanPeriod(),因此beaconmanager使用默认设置5分钟。如果你想让背景到前景的转换更容易预测,你需要将两次扫描之间的间隔时间设置得更低一些,但这也意味着电池的寿命会更短。

关于android - 从背景到前景模式的转换在AltBeacon中是随机的,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32226337/

10-09 00:30