public WMBeaconManager(
     MonitorNotifier monitorNotifier, RangeNotifier rangeNotifier, Context context) {
   this.context = context;
   mBeaconManager = BeaconManager.getInstanceForApplication(this.context);
   // Detect the main Eddystone-UID frame:
   mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(EDDYSTONE_LAYOUT));
   mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(IBEACON_LAYOUT));
   mBeaconManager.setRangeNotifier(rangeNotifier);
   mBeaconManager.setMonitorNotifier(monitorNotifier);
 }
 @Override
 public void didEnterRegion(Region region) {
   Log.d(TAG, "I just saw a beacon named " + region.getUniqueId() + " for the first time!");
   try {
     Log.d(TAG, "entered region.  starting ranging");
     mBeaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);
     mBeaconManager.setRangeNotifier(this);
   } catch (RemoteException e) {
     Log.e(TAG, "Cannot start ranging");
   }
 }
 @Override
 public void didEnterRegion(Region arg0) {
   // In this example, this class sends a notification to the user whenever a Beacon
   // matching a Region (defined above) are first seen.
   Log.d(TAG, "did enter region.");
   try {
     // start ranging for beacons.  This will provide an update once per second with the estimated
     // distance to the beacon in the didRAngeBeaconsInRegion method.
     mBeaconManager.startRangingBeaconsInRegion(arg0);
     mBeaconManager.setRangeNotifier(this);
   } catch (RemoteException e) {
   }
 }
  public BeaconSearcher(Context context) {

    this.mContext = context.getApplicationContext();

    this.mBeaconManager = BeaconManager.getInstanceForApplication(mContext);

    // 经过测试,天津的Beacon应该是Apple的Beacon,beaconTypeCode=0215
    // 其传输帧的字节序列按照以下顺序传输,但是网络上查到2013年后的Estimote beacons也是下列的字节顺序,ok
    // mBeaconManager.getBeaconParsers().add(new
    // BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

    // 也可能是AltBeacon(即Radius)的Beacon,ok
    mBeaconManager
        .getBeaconParsers()
        .add(new AltBeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

    // 设置距离计算模型
    DefaultDistanceCalcuator defaultDistanceCalcuator = new DefaultDistanceCalcuator(mContext);
    // 使用asserts/model-distance-calculations.json
    defaultDistanceCalcuator.setDefaultDistanceCalcuator(null);
    // 使用url
    // defaultDistanceCalcuator.setDefaultDistanceCalcuator("http://data.altbeacon.org/android-distance.json");

    // 设置发现beacon监听回调,看到beacon,看不到beacon; 进入,离开,临界状态
    // Specifies a class that should be called each time the BeaconService
    // sees or stops seeing a Region of beacons.
    // IMPORTANT: Only one MonitorNotifier may be active for a given
    // application. If two different activities or services set different
    // MonitorNotifier instances, the last one set will receive all the
    // notifications.
    mBeaconManager.setMonitorNotifier(mMonitorNotifier);

    // 设置测距修正回调,每个扫描周期结束,根据20秒内各beacon的RSSI平均值计算它的距离,该回调获取这些beacon的距离值
    // Specifies a class that should be called each time the BeaconService
    // gets ranging data, which is nominally once per
    // second(实际上每个扫描周期,计算一次距离) when beacons are detected.
    // IMPORTANT: Only one RangeNotifier may be active for a given
    // application. If two different activities or services set different
    // RangeNotifier instances,
    // the last one set will receive all the notifications.
    mBeaconManager.setRangeNotifier(mRangeNotifier);

    // 当程序切换到后台,BeaconService自动切换到后台模式,为了省电,蓝牙扫描频率降低;程序恢复到前台,BeaconService也跟随恢复至前台
    // simply constructing this class and holding a reference to it in your
    // custom Application
    // class will automatically cause the BeaconLibrary to save battery
    // whenever the application
    // is not visible. This reduces bluetooth power usage by about 60%
    Context appContext = this.mContext.getApplicationContext();
    mBackgroundPowerSaver = new BackgroundPowerSaver(appContext);
  }
  @Override
  public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(
        new RangeNotifier() {
          @Override
          public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            beaconRangeChange(beacons, region);
          }
        });

    try {
      beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {
    }
  }
Example #6
0
  @Override
  public void onBeaconServiceConnect() {
    if (solicitarParada && statusSolicitacao) {
      if (!onCounting) {
        logToDisplay("Starting timer");
        timer();
      }
    } else {
      beaconManager.setRangeNotifier(
          new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
              if (beacons.size() > 0) {
                firstBeacon = beacons.iterator().next();
                if (!solicitarParada) {
                  if (firstBeacon.getDistance() < 3) {
                    aux++;
                    if (aux > 4) {
                      id = firstBeacon.getId1().toString();

                      logToDisplay("Sending request to server");
                      logToDisplay(
                          WebService.acesso(
                              "http://200.188.161.248:8080/WSH2/recurso/abrir_parada/"
                                  + id.substring(id.length() - 1, id.length())));

                      aux = 0;

                      solicitarParada = true;
                      statusSolicitacao = true;
                    }
                  }
                } else if (aux == 0) {
                  if (id != null) {
                    String resposta =
                        WebService.acesso(
                            "http://200.188.161.248:8080/WSH2/recurso/abrir_parada/"
                                + id.substring(id.length() - 1, id.length()));
                    if (resposta.equalsIgnoreCase("0")) {
                      solicitarParada = true;
                      statusSolicitacao = true;
                    } else {
                      solicitarParada = false;
                      WebService.acesso(
                          "http://200.188.161.248:8080/WSH2/recurso/fecharSol_parada/"
                              + id.substring(id.length() - 1, id.length()));
                      statusSolicitacao = true;
                    }
                  }
                }
              }
            }
          });
    }

    try {
      beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {

    }
  }