update: 2016/07/20
reference:
1. Beacon Tech Overview - Estimote Developer
Part 3: Ranging beacons
A. Start ranging
1. 開啟 MainActivity.java 修改如下:
package myhome.airport;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.SystemRequirementsChecker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private BeaconManager beaconManager;
private Region region;
private static final Map<String, List<String>> PLACES_BY_BEACONS;
// TODO: replace "<major>:<minor>" strings to match your own beacons.
static {
Map<String, List<String>> placesByBeacons = new HashMap<>();
placesByBeacons.put("0:5", new ArrayList<String>() {{
add("Heavenly Sandwiches");
// read as: "Heavenly Sandwiches" is closest
// to the beacon with major 0 and minor 5
add("Green & Green Salads");
// "Green & Green Salads" is the next closest
add("Mini Panini");
// "Mini Panini" is the furthest away
}});
placesByBeacons.put("0:7", new ArrayList<String>() {{
add("Mini Panini");
add("Green & Green Salads");
add("Heavenly Sandwiches");
}});
PLACES_BY_BEACONS = Collections.unmodifiableMap(placesByBeacons);
}
private List<String> placesNearBeacon(Beacon beacon) {
String beaconKey = String.format("%d:%d", beacon.getMajor(), beacon.getMinor());
if (PLACES_BY_BEACONS.containsKey(beaconKey)) {
return PLACES_BY_BEACONS.get(beaconKey);
}
return Collections.emptyList();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager = new BeaconManager(this);
region = new Region("ranged region",
UUID.fromString("FDA50693-A4E2-4FB1-AFCF-C6EB07647825"), null, null);
// add Ranging listener
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, List<Beacon> list) {
if (!list.isEmpty()) {
Beacon nearestBeacon = list.get(0);
List<String> places = placesNearBeacon(nearestBeacon);
// TODO: update the UI here
Log.d("Airport", "Nearest places: " + places);
Log.d("Major: ", ""+nearestBeacon.getMajor());
Log.d("Minor: ", ""+nearestBeacon.getMinor());
Log.d("UUID: ", ""+ nearestBeacon.getProximityUUID());
Log.d("MeasuredPower: ", ""+ nearestBeacon.getMeasuredPower());
Log.d("Rssi: ", ""+ nearestBeacon.getRssi());
}
}
});
}
//@add ########################
@Override
protected void onResume() {
super.onResume();
//@add ########################
// Runtime permissions (Android 6.0)
SystemRequirementsChecker.checkWithDefaultDialogs(this);
// start Ranging
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startRanging(region);
}
});
}
//@add ########################
@Override
protected void onPause() {
// stop Ranging
beaconManager.stopRanging(region);
super.onPause();
}
}
2. 結果:
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。