Android永远不会进入didExitRegion

Android永远不会进入didExitRegion,android,ibeacon,ibeacon-android,Android,Ibeacon,Ibeacon Android,我正在使用一个带有android beacon库的estimote beacon。我找不到迪迪斯特里翁。其他一切都很好。如果应用程序“看到”一个信标自动获取didEnterRegion,但如果我超出了信标的范围,则不会调用didExitRegion方法。我正在用android版本5.1.1测试Nexus5 这是我对库的实现。希望有人能帮助我 public class MyApplicationName extends Application implements BootstrapNotifie

我正在使用一个带有android beacon库的estimote beacon。我找不到迪迪斯特里翁。其他一切都很好。如果应用程序“看到”一个信标自动获取didEnterRegion,但如果我超出了信标的范围,则不会调用didExitRegion方法。我正在用android版本5.1.1测试Nexus5

这是我对库的实现。希望有人能帮助我

public class MyApplicationName extends Application implements BootstrapNotifier {
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private HomePageActivity homePageActivity = null;

@Override
public void onCreate() {
    super.onCreate();

    BeaconManager beaconManager = 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"));
    Log.d("MyApplicationName", "Setting up background monitoring for beacons and power saving");
    Region region = new Region("B9407F30-F5F8-466E-AFF9-25556B57FE6D", null, null, null);
    regionBootstrap = new RegionBootstrap(this, region);
    backgroundPowerSaver = new BackgroundPowerSaver(this);
}

@Override
public void didEnterRegion(Region region) {

    Log.d("MyApplicationName", "did enter region");
    if (!haveDetectedBeaconsSinceBoot) {
        Log.d("MyApplicationName", "auto launching HomePageActivity");

        Intent intent = new Intent(this, HomePageActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
        haveDetectedBeaconsSinceBoot = true;
        //showNotification("Ciao", "ciaooo");
    }else {
        if(homePageActivity != null) {
            //showNotification("", "Welcome");
        }else {
            showNotification("", "Welcome");
        }
    }

    regionBootstrap.disable();
    Intent intent = new Intent(this, HomePageActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);
}

@Override
public void didExitRegion(Region region) {
    Log.d("MyApplicationName", "did exit region");
    showNotification("", "Goobbye");
}

@Override
public void didDetermineStateForRegion(int state, Region region) {
    Log.d("MyApplicationName", "See/not see beacon " + state);
}

private void showNotification(String title, String message) {
    Intent notifyIntent = new Intent(this, HomePageActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(this, 0,
            new Intent[]{notifyIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setPriority(Notification.PRIORITY_HIGH)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setStyle(new Notification.BigTextStyle().bigText(message))
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

public void setMonitoringActivity(HomePageActivity activity) {
    this.homePageActivity = activity;
    Log.d("MyApplicationName", String.valueOf(activity));
}
}

公共类HomePageActivity扩展AppCompative实现Beaconsumer{
专用BeaconManager BeaconManager=BeaconManager.getInstanceForApplication(此应用程序);
私人收藏;
私有字符串[]BeaconPresentMajorMinor;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u主页);
//beaconManager=beaconManager.getInstanceForApplication(此应用程序);
//beaconManager.getBeaconParsers().add(新的BeaconParser().setBeaconLayout(“m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24”);
beaconManager.bind(这个);
}
@凌驾
受保护的void onResume(){
super.onResume();
((MyApplicationName)this.getApplicationContext()).setMonitoringActivity(this);
如果(beaconManager.isBound(this))beaconManager.setBackgroundMode(false);
}
@凌驾
受保护的void onPause(){
super.onPause();
((MyApplicationName)this.getApplicationContext()).setMonitoringActivity(null);
如果(beaconManager.isBound(this))beaconManager.setBackgroundMode(true);
}
@凌驾
公共空间{
super.ondestory();
beaconManager.unbind(此);
}
@凌驾
beacerviceconnect()上的公共无效{
beaconManager.addRangeNotifier(新的RangeNotifier(){
@凌驾
公共无效范围信标区域(收集信标,区域){
信标当前=信标;
如果(beacons.size()>0){
用于(信标b:信标){
System.out.println(b.getId2()+“”+b.getId3());
//getWelcomeMessage(b.getId2(),b.getId3());
}
}
}
});
试一试{
beaconManager.StarTransingBeaconsisnRegion(新区域(“B9407F30-F5F8-466E-AFF9-25556B57FE6D”,空,空,空));
}捕获(远程异常){
e、 printStackTrace();
}
}
}

androidmanifest.xml的实现

<?xml version="1.0" encoding="utf-8"?>



问题在于
dientereregion
回调中的以下行:

regionBootstrap.disable();

该行将有效地禁用区域监视,因此您将永远不会得到另一个入口或出口回调。最简单的解决办法是删除该行。

你救了我一天!
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:name=".MyApplicationName"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".HomePageActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


</application>
regionBootstrap.disable();