Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从背景模式到前景模式的转换在AltBeacon中是随机的_Android_Altbeacon - Fatal编程技术网

Android 从背景模式到前景模式的转换在AltBeacon中是随机的

Android 从背景模式到前景模式的转换在AltBeacon中是随机的,android,altbeacon,Android,Altbeacon,嗯。我最近一直在使用library开发一个室内地理定位应用程序。 除了从背景到前景的过渡需要随机的时间间隔,而且速度不够快之外,一切似乎都正常。 在我的代码中,当应用程序在后台运行时,当检测到信标时,它不会立即切换到前台显示一些事件。 我需要知道这种延迟和随机性的原因。如果可能的话,如何修复它 主要活动: public class MainActivity extends Application implements BootstrapNotifier, BeaconConsumer, Ran

嗯。我最近一直在使用library开发一个室内地理定位应用程序。 除了从背景到前景的过渡需要随机的时间间隔,而且速度不够快之外,一切似乎都正常。 在我的代码中,当应用程序在后台运行时,当检测到信标时,它不会立即切换到前台显示一些事件。 我需要知道这种延迟和随机性的原因。如果可能的话,如何修复它

主要活动:

 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());

        }

    }

 }

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

即使我添加了它,我仍然存在相同的问题:/
<?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);
    }

}