我正在使用RadiusNetworks的AltBeacon库开发Android应用程序。我使用导航抽屉(启动活动,而不是片段)来显示不同的部分。因此,我有一个抽屉的主要活动,通过意图启动不同的活动。
我想在应用程序的各处提供附近的iBeacons信息(ID和距离)。怎么能接近这个?
我尝试了以下内容(如果我们使用RadiusNetworks提供的引用来映射我的应用结构):
在MainActivity中,我执行以下操作:
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon : beacons) {
Log.i("TAG","Beacon detected with id1: "+beacon.getId1()+" id2:"+beacon.getId2()+" id3: "+beacon.getId3()+" distance: "+beacon.getDistance());
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
我可以在日志中看到iBeacon信息,但只能看一次(第一次检测)。但是,我希望每次看到iBeacon时都会持续获取此信息(如参考应用程序中所示),以便我可以将其传递给其他活动。
我该如何解决这个问题?我应该使用不同的应用程序结构(因为导航抽屉)还是我以错误的方式使用库?
谢谢。
更新:MainActivity和BeaconReferenceApp的代码。
BeaconReferenceApp:
public class BeaconReferenceApp extends Application implements BootstrapNotifier {
private static final String TAG = "BeaconApp";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private MainActivity mainActivity = null;
public void onCreate() {
super.onCreate();
BeaconManager beaconManager = org.altbeacon.beacon.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,d:25-25"));
Log.d(TAG, "setting up background monitoring for beacons and power saving");
// wake up the app when a beacon is seen
Region region = new Region("backgroundRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
public void didEnterRegion(Region arg0) {
Log.d(TAG, "did enter region.");
// regionBootstrap.disable();
if (!haveDetectedBeaconsSinceBoot) {
Log.d(TAG, "auto launching MainActivity");
// The very first time since boot that we detect an beacon, we launch the MainActivity
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
haveDetectedBeaconsSinceBoot = true;
} else {
sendNotification();
}
}
public void didExitRegion(Region region) {}
public void didDetermineStateForRegion(int state, Region region) {}
public void setMainActivity(MainActivity activity) {this.mainActivity = activity;}
MainActivity:
public class MainActivity extends ActionBarActivity implements BeaconConsumer {
protected FrameLayout frameLayout;
protected ListView mDrawerList;
protected String[] listArray = { "Item1", "Item2", "Item3", "Item4" };
protected static int position;
private static boolean isLaunch = true;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
verifyBluetooth();
beaconManager.bind(this);
setContentView(R.layout.navigation_drawer_base_layout);
frameLayout = (FrameLayout)findViewById(R.id.content_frame);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, listArray));
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
openActivity(position);
}
});
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the proper interactions between the sliding drawer and the action bar app icon
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.open_drawer, /* "open drawer" description for accessibility */
R.string.close_drawer) /* "close drawer" description for accessibility */
{
public void onDrawerClosed(View drawerView) {
getSupportActionBar().setTitle(listArray[position]);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerClosed(drawerView);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.app_name));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerOpened(drawerView);
}
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
};
actionBarDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
/**
* MainActivity is intended just to add navigation drawer in the app.
* Open some activity with layout on launch. Check checking if this
* MainActivity is called first time when we are
* opening our first activity.
* */
if(isLaunch){
/**
*Setting this flag false so that next time it will not open
* first activity.
* Use this flag because we are using MainActivity
* as parent activity to our other activities.
* In this case this base activity will always be call when any
* child activity will launch.
*/
isLaunch = false;
openActivity(0);
}
}
/**
* Launching activity when any list item is clicked.
*/
protected void openActivity(int position) {
mDrawerLayout.closeDrawer(mDrawerList);
MainActivity.position = position; //Setting currently selected position in this field so that it will be available in child activities.
switch (position) {
case 0:
startActivity(new Intent(this, Item1.class));
break;
case 1:
startActivity(new Intent(this, Item2.class));
break;
case 2:
startActivity(new Intent(this, Item3.class));
break;
case 3:
startActivity(new Intent(this, Item4.class));
break;
default:
break;
}
}
public void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
public void onResume() {
super.onResume();
((BeaconReferenceApp) this.getApplicationContext()).setMainActivity(this);
if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false);
}
public void onPause() {
super.onPause();
((BeaconReferenceApp) this.getApplicationContext()).setMainActivity(null);
if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(true);
}
private void verifyBluetooth() {}
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon : beacons) {
Log.i("TAG", "Beacon detected with id1: " + beacon.getId1() + " id2:" + beacon.getId2() + " id3: " + beacon.getId3() + " distance: " + beacon.getDistance());
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
}
答案 0 :(得分:2)
最简单的方法是在自定义android.app.Application
课程中进行测距。每个Android应用程序都只有其中一个,它是将您希望全局代码放入整个应用程序的好地方。要定义自己的类,可以在清单中通过向类似物添加属性来声明它:
<application
...
android:name="MyApplication">
然后你可以定义一个具有相同名称的类,如下所示:
public class MyApplication extends Application implements BeaconConsumer {
private BeaconManager mBeaconManager;
public void onCreate() {
super.onCreate();
mbeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
mBeaconManager.bind(this);
}
public void onBeaconServiceConnect() {
mBeaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon : beacons) {
Log.i("TAG","Beacon detected with id1: "+beacon.getId1()+" id2:"+beacon.getId2()+" id3: "+beacon.getId3()+" distance: "+beacon.getDistance());
}
}
});
try {
mBeaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
如果您可以执行Application
课程中的所有逻辑,那么您就需要这样做。但是,您可能需要将测距数据转发到单个活动或片段。如果是这样,最简单的方法是从片段或活动中获取对中心Application
类的引用,如下所示:
MyApplication myApplication = ((MyApplication)this.getApplication());
然后,您可以访问Application
课程中集中存储的数据,或者告诉Application
班级将didRangeBeaconsInRegion
回调转发到您的活动或片段。
答案 1 :(得分:1)
我已经解决了修改Beaconing类的问题(我只显示更改):
public class Beaconing extends Application implements BootstrapNotifier, BeaconConsumer {
....
private BeaconManager beaconManager = null;
....
public void onCreate() {
super.onCreate();
beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
....
}
....
public void onBeaconServiceConnect() {
beaconManager.setBackgroundMode(true);
}
所以我实现了BeaconConsumer,将beaconManager设置为null并在onCreate()中获取它,并且我添加了onBeaconServiceConnect()。
这种方式适用于我,我实际上可以连续测量。
然而,我不知道之前没有用过的原因。如果有人知道并解释,我会很高兴。
感谢@davidgyoung(https://stackoverflow.com/users/1461050/davidgyoung)的帮助! :)