我想在我的应用中添加最简单的程序。程序应该只在单击一个对话框选项后获取信标信息,我多次尝试编写代码,但没有得到开始测距所需的最小步骤。
BaseClass
任何人都可以解释onBeaconServiceConnect是如何运行的?我是否需要在开始测量监控过程之前,检查是否有信标以及是否有信标可以开始测距,或者只是为了让程序只进行测距?
我尝试按Android Beacon Library示例代码部分所述进行操作,但无法正常工作
祝你好运
修改
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.");
}
}
});
}
}
为代码添加了这些权限,但仍无法正常工作
我做的另一件事是检查是否使用here中描述的功能启用了ACCESS_COARSE_LOCATION(第二个最高投票答案)
答案 0 :(得分:1)
单独测量很好。
onBeaconServiceConnect是在您调用beaconManager.bind(this);之后,当信标扫描服务准备就绪时进行的回调。回调表示您已准备好开始测距或监控。确保你得到这个回调。
如果您在Android 6+上进行测试,请确保您已获得位置许可,否则将阻止检测。
http://altbeacon.github.io/android-beacon-library/requesting_permission.html
还要确保蓝牙已开启,手机已启用位置,并且您可以通过现场信号检测来检测相同的信标,例如定位:https://play.google.com/store/apps/details?id=com.radiusnetworks.locate
答案 1 :(得分:0)
问题是:在示例代码中我认为它已经为iBeacons指定了,所以我离开了它......改为
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static String super_reduced_string(String s){
StringBuilder sb = new StringBuilder(s);
for(int i = 0; i<sb.length(); i++){
if(sb.charAt(i)==sb.charAt(i+1)){
sb.delete(i,i+2);
//System.out.println(sb.length());
i =- 1;
}
}
String resultString = sb.toString();
return resultString;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String result = super_reduced_string(s);
System.out.println(result);
}
}
为我正在使用的灯塔工作