我想在我的应用程序中添加最简单的程序。程序只应在单击对话框选项后才能获取信标信息,我多次尝试编写代码,但没有得到启动测距所需的最低步骤。
public class SecondClass extends Activity implements BeaconConsumer {
private BeaconManager beaconManager;
protected static final String TAG = "RangingActivity";
RegionBootstrap regionBootstrap;
Region region;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
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.bind(this);
region = new Region("backgroundRegion", null, null, null);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(101); //closes notification
//opens the alert dialog
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(this);
}
builder.setTitle("Send data")
.setMessage("Are you sure you want to send your data to the server?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// if yes was pressed, send the data
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Log.d(TAG, "Can't start ranging");
}
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// if cancel was pressed, do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
@Override
public void onBeaconServiceConnect() {
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
}
}
});
}
}有人能解释一下onBeaconServiceConnect是如何运行的吗?我是否需要在开始测距之前进行监测过程,检查是否有一个信标,是否有一个开始测距的信标,或者仅仅有一个只进行测距的程序是可以的?
我试着按照示例代码部分中的描述做,但是没有工作
诚挚的问候
编辑
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />将这些权限添加到代码中,但仍然无法工作。
发布于 2018-02-06 07:05:03
问题是:在示例代码中,我认为它已经为iBeacons指定了,所以我离开了它。变到
beaconManager.getBeaconParsers().add(new BeaconParser(). setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
为我用的信标工作
发布于 2018-02-02 13:03:34
量程本身是可以的。
onBeaconServiceConnect是在您调用beaconManager.bind(This)之后,当信标扫描服务准备就绪时进行的回调。回调指示您已准备好开始测距或监视。确保你收到了回音。
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); Eddystone有不同的布局。https://stackoverflow.com/questions/48578338
复制相似问题