每次检测到信标时,我都能看到我的安慰日志
D / BeaconService:检测到信标:id1:xxxxx-xxx-xxxxx-xxx-xxxxx id2:xxx id3:xx
但我无法弄清楚如何捕捉这个功能。 获得此功能需要哪个界面。
由于
答案 0 :(得分:0)
试试这个,
// Y positions are relative to height of bg_distance image.
Private static final double RELATIVE_START_POS = 320.0 / 1110.0;
Private static final double RELATIVE_STOP_POS = 885.0 / 1110.0;
Private BeaconManager beaconManager;
Private Beacon beacon;
Private Region region;
Private View distance view, dotView;
Private int startY = -1;
Private int segmentLength = -1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.distance_view);
dotView = findViewById(R.id.distance_iv_dot);
beacon = getIntent().getParcelableExtra(MainActivity.EXTRAS_BEACON);
region = new Region("regionid", beacon.getProximityUUID(), beacon.getMajor(), beacon.getMinor());
if (beacon == null) {
Toast.makeText(this, "Beacon not found in intent extras", Toast.LENGTH_LONG).show();
finish();
return;
}
beaconManager = new BeaconManager(this);
distanceview = findViewById(R.id.distance_background_view);
distanceview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
distanceview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
startY = (int) (RELATIVE_START_POS * distanceview.getMeasuredHeight());
int stopY = (int) (RELATIVE_STOP_POS * distanceview.getMeasuredHeight());
segmentLength = stopY - startY;
dotView.setVisibility(View.VISIBLE);
dotView.setTranslationY(computeDotPosY(beacon));
}
});
}
private void updateDistanceView(Beacon foundBeacon) {
if (segmentLength == -1) {
return ;
}
dotView.animate().translationY(computeDotPosY(foundBeacon)).start();
}
private int computeDotPosY(Beacon beacon) {
// Let's put dot at the end of the scale when it's further than 6m.
double distance = Math.min(Utils.computeAccuracy(beacon), 6.0);
return startY + (int) (segmentLength * (distance / 6.0));
}
@Override
protected void onStart() {
super.onStart();
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> rangedBeacons) {
// Note that results are not delivered on UI thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
// Just in case if there are multiple beacons with the same uuid, major, minor.
Beacon foundBeacon = null;
for (Beacon rangedBeacon : rangedBeacons) {
if (rangedBeacon.getMacAddress().equals(beacon.getMacAddress())) {
foundBeacon = rangedBeacon;
}
}
if (foundBeacon != null) {
updateDistanceView(foundBeacon);
}
}
});
}
});
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startRanging(region);
}
});
}
@Override
protected void onStop() {
beaconManager.stopRanging(region);
beaconManager.disconnect();
super.onStop();
} }
写入 翻译 限定 同义词 最爱 私人教练 每日短语 个人词典 快速游览 教程 设置 发送反馈 GingerFREE
Public class DistanceBeaconActivity extends AppCompatActivity {
Private static final String TAG = DistanceBeaconActivity. class. getSimpleName ();
// Y positions are relative to height of bg_distance image.
Private static final double RELATIVE_START_POS = 320.0 / 1110.0;
Private static final double RELATIVE_STOP_POS = 885.0 / 1110.0;
Private BeaconManager beaconManager;
Private Beacon beacon;
Private Region region;
Private View distance view, dotView;
Private int startY = -1;
Private int segmentLength = -1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.distance_view);
dotView = findViewById(R.id.distance_iv_dot);
beacon = getIntent().getParcelableExtra(MainActivity.EXTRAS_BEACON);
region = new Region("regionid", beacon.getProximityUUID(), beacon.getMajor(), beacon.getMinor());
if (beacon == null) {
Toast.makeText(this, "Beacon not found in intent extras", Toast.LENGTH_LONG).show();
finish();
return;
}
beaconManager = new BeaconManager(this);
distanceview = findViewById(R.id.distance_background_view);
distanceview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
distanceview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
startY = (int) (RELATIVE_START_POS * distanceview.getMeasuredHeight());
int stopY = (int) (RELATIVE_STOP_POS * distanceview.getMeasuredHeight());
segmentLength = stopY - startY;
dotView.setVisibility(View.VISIBLE);
dotView.setTranslationY(computeDotPosY(beacon));
}
});
}
private void updateDistanceView(Beacon foundBeacon) {
if (segmentLength == -1) {
return ;
}
dotView.animate().translationY(computeDotPosY(foundBeacon)).start();
}
private int computeDotPosY(Beacon beacon) {
// Let's put dot at the end of the scale when it's further than 6m.
double distance = Math.min(Utils.computeAccuracy(beacon), 6.0);
return startY + (int) (segmentLength * (distance / 6.0));
}
@Override
protected void onStart() {
super.onStart();
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> rangedBeacons) {
// Note that results are not delivered on UI thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
// Just in case if there are multiple beacons with the same uuid, major, minor.
Beacon foundBeacon = null;
for (Beacon rangedBeacon : rangedBeacons) {
if (rangedBeacon.getMacAddress().equals(beacon.getMacAddress())) {
foundBeacon = rangedBeacon;
}
}
if (foundBeacon != null) {
updateDistanceView(foundBeacon);
}
}
});
}
});
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startRanging(region);
}
});
}
@Override
protected void onStop() {
beaconManager.stopRanging(region);
beaconManager.disconnect();
super.onStop();
}
}
我希望它能帮助你获得检测Beaconlist的功能。:)