Essayez ceci,
// 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();
} }
Ecrire Traduire Define Synonymes Favoris Personal Trainer Phrase du jour Dictionnaire personnel Visite rapide Tutoriels Paramètres Envoyer des commentaires 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();
}
}
J'espère que cela vous aidera à obtenir que pour la fonction de détection de la Beaconlist :)
Pouvez-vous s'il vous plaît montrer votre code – davidgyoung
Je viens de copier les codes de ce cours d'instruction. Https: // altbeacon .github.io/android-beacon-library/samples.html –