Пример #1
0
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

          Log.i(Utils.LOG_TAG, "BLE packet received");

          IBeacon newBeacon = parseAdvertisementData(scanRecord);
          if (newBeacon == null) return;

          newBeacon.setMacAddress(device.getAddress());

          // If already discovered, then just refresh the RSSI of the existing instance and return
          if (_arrOrderedIBeacons.contains(newBeacon)) {

            double newDistance = calculateDistance(newBeacon.getPowerValue(), rssi);
            IBeacon previousIBeaconInfo = findIfExists(newBeacon);
            double oldDistance = previousIBeaconInfo.getProximity();

            if (newDistance < oldDistance) {
              Log.i(Utils.LOG_TAG, "Updating distance");
              previousIBeaconInfo.setProximity(newDistance);
              // Sort again
              Collections.sort(_arrOrderedIBeacons, new IBeaconProximityComparator());
            }
            return;
          }

          newBeacon.setEasiBeacon(false);

          if (device.getName() != null) {

            Log.e("device.getName()", String.valueOf(device.getName()));
            if (device.getName().startsWith(EASIBEACON_IDPREFIX)) {

              newBeacon.setEasiBeacon(true);
              String version = device.getName().substring(EASIBEACON_IDPREFIX.length());
              newBeacon.setVersionModel(version);

              if (newBeacon.getVersion() == 1) {
                // Version 1 is always connectable
                newBeacon.setConnectable(true);

              } else if (newBeacon.getVersion() == 2) {

                newBeacon.setConnectable(getConnectable(scanRecord));
                if (!newBeacon.isConnectable())
                  newBeacon.setEasiBeacon(false); // If not connectable we will report it as unknown
              }
            }
          }
          // Review this
          Log.i(
              Utils.LOG_TAG,
              device.getName()
                  + " "
                  + device.getAddress()
                  + " "
                  + newBeacon.getPowerValue()
                  + " "
                  + rssi
                  + " Connectable: "
                  + newBeacon.isConnectable());
          newBeacon.setProximity(calculateDistance(newBeacon.getPowerValue(), rssi));

          if (!_arrOrderedIBeacons.contains(newBeacon)) {

            _arrOrderedIBeacons.add(newBeacon);
            Collections.sort(_arrOrderedIBeacons, new IBeaconProximityComparator());
            _listener.beaconFound(newBeacon);

            // Every time a new beacon is found, reset the timeout
            _timeoutHandler.removeCallbacks(timeoutTask);
            _timeoutHandler.postDelayed(timeoutTask, IBeaconProtocol.SCANNING_PERIOD);
          }
        }