如何在 didRangeBeaconsInRegion 中使用测距
How to use ranging in didRangeBeaconsInRegion
--------编辑 2--------
仍在使用 Davidgyoung 的 post 和这些评论,现在我有一个 FatalException:
E/AndroidRuntime﹕ FATAL EXCEPTION:
IntentService[BeaconIntentProcessor]
Process: databerries.beaconapp, PID: 19180
java.lang.NullPointerException
at databerries.beaconapp.MyApplicationName.didEnterRegion(MyApplicationName.java:76)
at org.altbeacon.beacon.BeaconIntentProcessor.onHandleIntent(BeaconIntentProcessor.java:83)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
这个错误是因为调用了setRangeNotifier()?
--------编辑--------
在 Davidgyoung 的 post 和这些评论之后,我尝试了这个方法,但仍然没有用:
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
的异常
这不是我的主要 class,我的主要 class :
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"。
如何知道区域内的信标?
最后一个问题,可以在后台进行吗?
谢谢
您可以在此处的 "Ranging Sample Code" 部分查看信标测距示例: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());
}
}
一旦您开始测距,它将在后台继续这样做,前提是您不退出开始测距的 activity。在库的某些使用下,测距在后台变慢,但这仅在使用 BackgroundPowerSaver
class 时才会发生。如果您不希望测距在后台变慢,只需不要启用库的后台节能。
--------编辑 2--------
仍在使用 Davidgyoung 的 post 和这些评论,现在我有一个 FatalException:
E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[BeaconIntentProcessor] Process: databerries.beaconapp, PID: 19180 java.lang.NullPointerException at databerries.beaconapp.MyApplicationName.didEnterRegion(MyApplicationName.java:76) at org.altbeacon.beacon.BeaconIntentProcessor.onHandleIntent(BeaconIntentProcessor.java:83) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.os.HandlerThread.run(HandlerThread.java:61)
这个错误是因为调用了setRangeNotifier()?
--------编辑--------
在 Davidgyoung 的 post 和这些评论之后,我尝试了这个方法,但仍然没有用:
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
这不是我的主要 class,我的主要 class :
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"。
如何知道区域内的信标?
最后一个问题,可以在后台进行吗?
谢谢
您可以在此处的 "Ranging Sample Code" 部分查看信标测距示例: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());
}
}
一旦您开始测距,它将在后台继续这样做,前提是您不退出开始测距的 activity。在库的某些使用下,测距在后台变慢,但这仅在使用 BackgroundPowerSaver
class 时才会发生。如果您不希望测距在后台变慢,只需不要启用库的后台节能。