使用Google's new Eddystone standard,他们将在Google Play服务Nearby Api中为Android提供支持。我们可以注册eddystone信标,即使应用程序没有运行,我们的应用程序也会收到意图吗?
答案 0 :(得分:5)
是的,可以使用完全支持Android Beacon Library的Eddystone完成此操作。
应用程序的后台启动机制在Eddystone上的工作方式与对库支持的其他类型信标的工作方式相同。您在自定义RegionBootstrap
类中使用Application
对象。您可以阅读有关其工作原理的详细信息here。
与Eddystone的唯一区别在于您必须设置解码Eddystone-UID框架的BeaconParser
,然后设置一个与您的Eddystone名称空间ID匹配的Region
:
public class MyApplicationName extends Application implements BootstrapNotifier {
private static final String TAG = ".MyApplicationName";
private RegionBootstrap regionBootstrap;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "App started up");
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
// Detect the main identifier (UID) frame:
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
// wake up the app when a beacon matching myEddystoneNamespaceId is seen
myEddystoneNamespaceId = Identifier.parse("0x2f234454f4911ba9ffa6");
Region region = new Region("com.example.myapp.boostrapRegion", myEddystoneNamespaceId, null, null);
regionBootstrap = new RegionBootstrap(this, region);
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
// Don't care
}
@Override
public void didEnterRegion(Region arg0) {
Log.d(TAG, "Got a didEnterRegion call");
// This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
// if you want the Activity to launch every single time beacons come into view, remove this call.
regionBootstrap.disable();
Intent intent = new Intent(this, MainActivity.class);
// IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
// created when a user launches the activity manually and it gets launched from here.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
@Override
public void didExitRegion(Region arg0) {
// Don't care
}
}
答案 1 :(得分:1)
好的,所以这非常痛苦,但我终于设法使用Nearby Messages API检测到信标。
现在您可以关注代码示例here,它应该检测您的信标
Nearby.Messages.subscribe(googleApiClient, new MessageListener() {
@Override
public void onFound(Message message) {
Log.i(TAG, "Found : " + message);
}
@Override
public void onLost(Message message) {
Log.i(TAG, "Lost : " + message);
}
}, new SubscribeOptions.Builder()
.setStrategy(Strategy.BLE_ONLY)
.build());
答案 2 :(得分:0)
自Google Play服务v8.4 SDK(2015年12月)发布以来,这已经成为可能。
有关更多信息,请参阅以下链接: http://android-developers.blogspot.com/2015/12/google-play-services-84-sdk-is-available_18.html