我正在开发一个提供背景信标监控的应用程序。当用户从定义的区域输入信标时,我想开始测距。当应用程序处于后台并且它一直在监视并且用户进入我定义的区域时,我想开始测距并获得InstanceID或Major,Minor值来确定信标是什么,连接到服务器并向用户发送通知。如果我可以在后台与服务器进行通信,那将是最好的。我使用此示例来实现后台监控:https://altbeacon.github.io/android-beacon-library/samples.html。我还从这里下载了示例项目:https://github.com/AltBeacon/android-beacon-library-reference以它为基础。
不幸的是,在此示例中,当用户进入区域时,Activity已启动...我不希望这种情况发生。所以我的问题是:是否有可能在后台设置信标?
在我的情况下也发生了奇怪的事情,因为当我将我的应用程序放在后台方法“didRangeBeaconsInRegion(集合信标,区域区域)”仍然从MainActivity调用但没有找到信标。此外,方法的调用次数较少,因为beaconManager处于后台模式。当我启动未发生的示例项目时。也许是因为我没有monitoringActivity。我的MainActivity在推出时会立即进行测距。当然,我尝试设置与BeaconReferenceApplication示例完全相同的所有内容。
BTW我正在使用Android 6.0.1在Nexus 5上测试我的应用程序
提前感谢您提供任何解决方案!
答案 0 :(得分:10)
我终于明白了如何做到这一点!实际上我很简单,我从一开始就这样做,但是由于我错误地使用了旧版本的Altbeacon库而导致我所有的问题... Ehh
没关系。这是我的代码。 Meybe有人可以使用它;) 我通过创建集中的Application类来实现它,该类在进入定义的Region时实现BootstrapNotifier用于后台通知。我的类还实现了BeaconConsumer,RangeNotifier接口,它们是进行信标测距的必要条件。
package com.smartmachi.smartmachi_android;
import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.powersave.BackgroundPowerSaver;
import org.altbeacon.beacon.startup.BootstrapNotifier;
import org.altbeacon.beacon.startup.RegionBootstrap;
import java.util.Collection;
public class BeaconReferenceApplication extends Application implements BootstrapNotifier, BeaconConsumer, RangeNotifier {
private static final String TAG = "BeaconReferenceApp";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private MainActivity rangingActivity = null;
BeaconManager beaconManager;
public void onCreate() {
super.onCreate();
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
Region region = new Region("backgroundRegion", Identifier.parse("0xf7826da6bc5b71e0893e"), null, null);
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
beaconManager.setBackgroundBetweenScanPeriod(30000l);
beaconManager.setForegroundBetweenScanPeriod(2000l);
beaconManager.bind(this);
}
@Override
public void didEnterRegion(Region region) {
Log.d(TAG, "did enter region.");
try {
beaconManager.startRangingBeaconsInRegion(region);
}
catch (RemoteException e) {
if (BuildConfig.DEBUG) Log.d(TAG, "Can't start ranging");
}
}
@Override
public void didExitRegion(Region region) {
try {
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.d(TAG,"I have just switched from seeing/not seeing beacons: " + state);
}
private void sendNotification(String text) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Beacon Reference Application")
.setContentText(text)
.setSmallIcon(R.drawable.ic_launcher);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
for (Beacon b : beacons) {
if(b.getId2().toString().equals("0x6d767674636e")) {
Log.e(TAG, "Beacon with my Instance ID found!");
sendNotification("Beacon with my Instance ID found!");
}
}
}
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(this);
}
}