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