-------编辑2--------
仍然使用Davidgyoung的帖子和这些评论,现在我有一个致命的例外:
E/AndroidRuntime﹕致命异常:IntentService[信标IntentProcessor]流程:databerries.beaconapp,PID:19180java.lang.NullPointerException在databerrys.beaconapp.MyApplicationName.didEnterRegion(MyApplicationName.java:76)网址:org.altbeacons.beacon.BeaconsIntentProcessor.onHandleIntent(BeaconsIntetProcessor.java:83)在android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:136)在android.os.HandlerThread.run(HandlerThread.java:61)
此错误是由于调用了setRangeNotificationr()?
-------编辑-------
在Davidgyoung的帖子和这些评论之后,我尝试了这种方法,但仍然不起作用:
public class MyApplicationName extends Application implements BootstrapNotifier {
private static final String TAG = ".MyApplicationName";
private RegionBootstrap regionBootstrap;
private BeaconManager beaconManager;
List region_list = new ArrayList();
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "App started up");
// wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
List region_list = myRegionList();
regionBootstrap = new RegionBootstrap(this, region_list);
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"));
beaconManager.setBackgroundScanPeriod(3000l);
beaconManager.setBackgroundBetweenScanPeriod(5000l);
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
// Don't care
}
@Override
public void didEnterRegion(Region region) {
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, MyActivity.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);
String zone = region.toString();
Log.d(TAG, "Enter in region");
String text = "Enter in " + zone;
Log.d(TAG, text);
String uuid = "UUID : " + region.getId1();
Log.d(TAG, uuid);
//This part is not working
beaconManager.setRangeNotifier(this);
beaconManager.startRangingBeaconsInRegion(region);
}
@Override
public void didExitRegion(Region arg0) {
// Don't care
}
错误是关于setRangeNotifier
中的输入和startRangingBeaconsInRegion
的异常
这不是我的主要课程,我的主要班级:
public class MyActivity extends Activity{
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("myActivity","onCreate");
}
}
我想要该地区所有信标的ID。使用这种方法,如果我理解的话,当她检测到一个区域时,应用程序会在后台唤醒,通常"startRangingBeaconsInRegion"可以给我一个信标列表,用这个我可以获取Id。
-------原件-------
我想知道我周围的所有信标。我知道这个信标的UUID,我可以用"region.toString();"获得它。但是,我需要信标的其他id。而且,我在didRangeBeaconsInRegion上没有"Beacon"。
如何知道该地区的信标?
最后一个问题,是否有可能在后台进行?
感谢
您可以在此处的"测距示例代码"部分中看到信标测距的示例:http://altbeacon.github.io/android-beacon-library/samples.html
这将允许您通过查看回调中Collection<Beacon> beacons
中返回的每个Beacon
对象来读取所有标识符。像这样:
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon: beacons) {
Log.i(TAG, "This beacon has identifiers:"+beacon.getId1()+", "+beacon.getId2()+", "+beacon.getId3());
}
}
一旦您开始测距,它将在后台继续这样做,前提是您不退出启动测距的活动。在使用库的某些情况下,测距在后台会减慢,但只有在使用BackgroundPowerSaver
类时才会发生这种情况。如果您不想在后台降低测距速度,请不要使用库启用后台节能。