【发布时间】:2017-01-20 17:06:21
【问题描述】:
免责声明:android 编程新手。
大家好, 我正在尝试设计一个应用程序来使用 altbeacon 库跟踪智能手机的位置。我正在使用的信标设置为每 100 毫秒做一次广告(不可连接的广告),因此由于默认情况下库扫描 1.1 秒,因此预计会收到多个广告数据包。相反,我在日志中每次扫描只接收一个数据包,因此 RSSI 的刷新非常慢(每秒一个)。使用默认的 android 库(设置 SCAN_MODE_LOW_LATENCY),我可以连续访问 ble 回调,在一秒钟内多次更新 RSSI。
是否可以对 Altbeacon 库做同样的事情?我的示例应用程序如下所示:
public class MainActivity extends AppCompatActivity implements BeaconConsumer{
protected static final String TAG = "info";
private BeaconManager beaconManager;
private HashSet<Beacon> beaconsSeen = new HashSet<Beacon>();
Region region1 = new Region("region1", "xx:xx:xx:xx:xx:xx");
Region region2 = new Region("region2", "xx:xx:xx:xx:xx:xx");
Region region3 = new Region("region3", "xx:xx:xx:xx:xx:xx");
Region region4 = new Region("region4", "xx:xx:xx:xx:xx:xx");
Region region5 = new Region("region5", "xx:xx:xx:xx:xx:xx");
Region region6 = new Region("region6", "xx:xx:xx:xx:xx:xx");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RangedBeacon.setSampleExpirationMilliseconds(5000); //collects signal measurements over 5 seconds, throws out the top 10% and bottom 10% of
// measurements, and averages the rest
beaconManager = BeaconManager.getInstanceForApplication(this);
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon : beacons) {
Log.i(TAG, "I see a beacon "+beacon.getBluetoothAddress()+" with rssi: "+beacon.getRssi()+" dBm about "+beacons.iterator().next().getDistance()+" meters away.");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(region1);
beaconManager.startRangingBeaconsInRegion(region2);
beaconManager.startRangingBeaconsInRegion(region3);
beaconManager.startRangingBeaconsInRegion(region4);
beaconManager.startRangingBeaconsInRegion(region5);
beaconManager.startRangingBeaconsInRegion(region6);
} catch (RemoteException e) { }
}
我的手机是运行 Android 5.0.2 的小米红米 Note 2。
谢谢。
【问题讨论】:
标签: android bluetooth-lowenergy ibeacon altbeacon