private void sendBeaconChangeMessage(Beacon beacon) {
    String uuid = beacon.getId1().toString();
    // Payload
    SaneJSONObject payload = new SaneJSONObject();
    payload.putOrIgnore("platform", "android");
    payload.putDoubleOrIgnore("version", VERSION);

    // Beacon
    SaneJSONObject beaconJSON = new SaneJSONObject();
    beaconJSON.putOrIgnore("uuid", uuid);
    payload.putJSONOrIgnore("beacon", beaconJSON);

    // Proximity
    payload.putJSONOrIgnore("proximity", getProximity(beacon));

    // Message
    SaneJSONObject message = new SaneJSONObject();
    JSONArray devices = new JSONArray();
    devices.put("*");
    message.putArrayOrIgnore("devices", devices);
    message.putJSONOrIgnore("payload", payload);
    message.putOrIgnore("topic", "location_update");

    // Send
    BeaconInfo beaconInfo = getBeaconInfo(this.beaconInfo, uuid);
    Double distance = beacon.getDistance();
    if (beaconInfo.hasChangedDistance(distance)) {
      meshblu.message(message);
      emitter.emit(EVENTS.LOCATION_UPDATE, payload, beaconInfo);
    }
    beaconInfo.setLastDistance(distance);
  }
 /**
  * 每个扫描周期结束,根据20秒内各beacon的RSSI平均值计算它的距离,该回调获取这些beacon的距离值 Called once per second (实际上是每扫描周期)
  * to give an estimate of the mDistance to visible beacons
  */
 @Override
 public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
   LogManager.d(TAG, "didRangeBeaconsInRegion(),beacons=" + beacons.size());
   for (Beacon beacon : beacons) {
     LogManager.d(TAG, beacon.getId2() + ":" + beacon.getId3() + "," + beacon.getDistance());
   }
   Beacon beacon = mNearestBeacon.getNearestBeacon(mGetBeaconType, beacons);
   mOnNearestBeaconListener.getNearestBeacon(mGetBeaconType, beacon);
 }
 public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
   if (beacons.size() > 0) {
     EditText editText = (EditText) RangingActivity.this.findViewById(R.id.rangingText);
     for (Beacon beacon : beacons) {
       logToDisplay(
           "Beacon "
               + beacon.toString()
               + " is about "
               + beacon.getDistance()
               + " meters away, with Rssi: "
               + beacon.getRssi());
     }
   }
 }
 private void beaconRangeChange(Collection<Beacon> beacons, Region region) {
   for (Beacon beacon : beacons) {
     String uuid = beacon.getId1().toString();
     DecimalFormat df = new DecimalFormat("#.000");
     String distance = df.format(beacon.getDistance());
     Log.d(TAG, "Beacon (" + uuid.substring(0, 8) + ") is about " + distance + " meters away.");
     Boolean enabled = isBeaconEnabled(uuid);
     if (enabled != null) {
       if (enabled) {
         sendBeaconChangeMessage(beacon);
       }
     } else {
       emitter.emit(EVENTS.DISCOVERED_BEACON, beacon);
     }
   }
 }
 private SaneJSONObject getProximity(Beacon beacon) {
   Double distance = beacon.getDistance();
   String proximity = "Unknown";
   Integer code = 0;
   if (distance < 2) {
     code = 1;
     proximity = "Immediate";
   } else if (distance >= 2 && distance < 5) {
     code = 2;
     proximity = "Near";
   } else if (distance >= 5) {
     code = 3;
     proximity = "Far";
   }
   SaneJSONObject proximityJSON = new SaneJSONObject();
   proximityJSON.putOrIgnore("message", proximity);
   proximityJSON.putIntOrIgnore("code", code);
   proximityJSON.putDoubleOrIgnore("distance", distance);
   proximityJSON.putIntOrIgnore("rssi", beacon.getRssi());
   Long time = new Date().getTime();
   proximityJSON.putOrIgnore("timestamp", new Timestamp(time).toString());
   return proximityJSON;
 }