我有3个Estimote Beacons(Div Kit)。在我的Android应用程序中,我试图更改3个TextView以显示每个信标的主要和次要。我已经将信标设置更改为每隔1ms通告一次信号,这是最短的时间。
问题是我的BeaconManager
对象只检测到一个信标,需要一段时间才能完成。
这是我的代码
App.java
public class App extends Application {
public static BeaconManager mBeaconManager;
@Override
public void onCreate() {
super.onCreate();
mBeaconManager = new BeaconManager(getApplicationContext());
mBeaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);
mBeaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
mBeaconManager.startMonitoring(new Region(
"monitored region",
UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"),
null, null));
}
});
}
}
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.tv);
final TextView tv2 = (TextView) findViewById(R.id.tv2);
final TextView tv3 = (TextView) findViewById(R.id.tv3);
App.mBeaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> list) {
tv.setText(list.get(0).getMajor() + " : " + list.get(0).getMinor());
if (list.size() > 1) {
tv2.setText(list.get(1).getMajor() + " : " + list.get(1).getMinor());
}
if (list.size() > 2) {
tv3.setText(list.get(2).getMajor() + " : " + list.get(2).getMinor());
}
}
@Override
public void onExitedRegion(Region region) {
tv.setText("Exit");
}
});
}
我知道代码可能是非专业的,但它仅用于测试。
答案 0 :(得分:0)
嗨,我遇到了同样的问题 但我解决了这个问题
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Log.i("test", "Detected " + beacons.size());
if (beacons.size() > 0) {
beaconList.clear();
for (Beacon beacon : beacons) {
beacon.setHardwareEqualityEnforced(true);
beaconList.add(beacon);
}
}
我插入了beacon.setHardwareEqualityEnforced(true);
答案 1 :(得分:0)
在你的方法之后试试这个。
mBeaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
mBeaconManager.startMonitoring(new Region(
"monitored region",
UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"),
null, null));
}
});
// Apply this method mBeaconManager.setRangingListener
mBeaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beaconList) {
if (!beaconList.isEmpty()) {
// You will find total list of beacon around of you.
Log.e("Length of Beacon List - ", String.valueOf(beaconList.size()));
}
}
});