Skip to content

Commit

Permalink
Merge pull request #160 from neXenio/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Steppschuh authored Aug 15, 2019
2 parents 0ce40a7 + 203c6f8 commit f94577f
Show file tree
Hide file tree
Showing 31 changed files with 203 additions and 125 deletions.
66 changes: 34 additions & 32 deletions BLE Indoor Positioning/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ buildscript {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.8.1"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.8"
}
}

plugins {
id 'net.researchgate.release' version '2.4.0'
id 'com.jfrog.bintray' version '1.7.3'
id 'net.researchgate.release' version '2.6.0'
id 'com.jfrog.bintray' version '1.8.4'
}

apply plugin: 'java-library'
Expand All @@ -28,8 +28,8 @@ dependencies {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"

def releaseVersionCode = 14
def releaseVersionName = "0.3.8"
def releaseVersionCode = 16
def releaseVersionName = "0.4.0"

def projectName = 'BLE Indoor Positioning'
def projectDescription = 'Java library for indoor positioing using bluetooth beacons'
Expand Down Expand Up @@ -97,32 +97,6 @@ def pomConfig = {
}
}

publishing {
publications {
mavenJava(MavenPublication) {
from components.java
groupId projectGroupId
artifact sourcesJar
artifactId projectArtifactId
}
BintrayPublication(MavenPublication) {
from components.java
groupId projectGroupId
artifact sourcesJar
artifact javadocJar
artifactId projectArtifactId
version releaseVersionName
pom.withXml {
def root = asNode()
root.appendNode('name', projectName)
root.appendNode('description', projectDescription)
root.appendNode('url', repositoryUrl)
root.children().last() + pomConfig
}
}
}
}

task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allJava
Expand All @@ -138,6 +112,34 @@ artifacts {
archives javadocJar
}

project.afterEvaluate {
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
groupId projectGroupId
artifact sourcesJar
artifactId projectArtifactId
}
BintrayPublication(MavenPublication) {
from components.java
groupId projectGroupId
artifact sourcesJar
artifact javadocJar
artifactId projectArtifactId
version releaseVersionName
pom.withXml {
def root = asNode()
root.appendNode('name', projectName)
root.appendNode('description', projectDescription)
root.appendNode('url', repositoryUrl)
root.children().last() + pomConfig
}
}
}
}
}

// configure release plugin. See https://github.com/researchgate/gradle-release#configuration
release {
failOnUnversionedFiles = false
Expand All @@ -157,4 +159,4 @@ jacocoTestReport {
}
}

check.dependsOn jacocoTestReport
check.dependsOn jacocoTestReport
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nexenio.bleindoorpositioning.ble.beacon.Beacon;
import com.nexenio.bleindoorpositioning.ble.beacon.BeaconManager;
import com.nexenio.bleindoorpositioning.ble.beacon.BeaconUpdateListener;
import com.nexenio.bleindoorpositioning.ble.beacon.BeaconUtil;
import com.nexenio.bleindoorpositioning.ble.beacon.filter.BeaconFilter;
import com.nexenio.bleindoorpositioning.ble.beacon.filter.GenericBeaconFilter;
import com.nexenio.bleindoorpositioning.location.Location;
Expand All @@ -15,6 +16,7 @@

import org.apache.commons.math3.exception.TooManyEvaluationsException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -51,7 +53,6 @@ public class IndoorPositioning implements LocationProvider, BeaconUpdateListener

private LocationPredictor locationPredictor = new LocationPredictor();


private IndoorPositioning() {
BeaconManager.registerBeaconUpdateListener(this);
}
Expand Down Expand Up @@ -89,7 +90,7 @@ private void updateLocation() {
if (usableBeacons.size() < MINIMUM_BEACON_COUNT) {
return;
} else if (usableBeacons.size() > MINIMUM_BEACON_COUNT) {
Collections.sort(usableBeacons, Beacon.RssiComparator);
Collections.sort(usableBeacons, BeaconUtil.DescendingRssiComparator);
int maximumBeaconIndex = Math.min(MAXIMUM_BEACON_COUNT, usableBeacons.size());
int firstRemovableBeaconIndex = maximumBeaconIndex;
for (int beaconIndex = MINIMUM_BEACON_COUNT; beaconIndex < maximumBeaconIndex; beaconIndex++) {
Expand Down Expand Up @@ -117,7 +118,11 @@ private void updateLocation() {
}

public static <B extends Beacon> List<B> getUsableBeacons(Collection<B> availableBeacons) {
return getInstance().usableIndoorPositioningBeaconFilter.getMatches(availableBeacons);
BeaconFilter beaconFilter = getInstance().usableIndoorPositioningBeaconFilter;
if (availableBeacons.isEmpty() || !beaconFilter.canMatch(availableBeacons.iterator().next())) {
return new ArrayList<>();
}
return beaconFilter.getMatches(availableBeacons);
}

private void onLocationUpdated(Location location) {
Expand Down Expand Up @@ -150,8 +155,14 @@ public static GenericBeaconFilter<? extends Beacon> createUsableIndoorPositionin

@Override
public boolean matches(Beacon beacon) {
if (getInstance().indoorPositioningBeaconFilter != null && !getInstance().indoorPositioningBeaconFilter.matches(beacon)) {
return false;
BeaconFilter beaconFilter = getInstance().indoorPositioningBeaconFilter;
if (beaconFilter != null) {
if (!beaconFilter.canMatch(beacon)) {
return false;
}
if (!beaconFilter.matches(beacon)) {
return false;
}
}
if (!beacon.hasLocation()) {
return false; // beacon has no location assigned, can't use it for multilateration
Expand Down Expand Up @@ -224,4 +235,5 @@ public int getMinimumRssiThreshold() {
public void setMinimumRssiThreshold(int minimumRssiThreshold) {
this.minimumRssiThreshold = minimumRssiThreshold;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public boolean dataEquals(AdvertisingPacket advertisingPacket) {

@Override
public String toString() {
return AdvertisingPacketUtil.toHexadecimalString(data);
return "AdvertisingPacket{" +
"data=" + Arrays.hashCode(data) +
", rssi=" + rssi +
", timestamp=" + timestamp +
'}';
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -26,7 +25,6 @@ public abstract class Beacon<P extends AdvertisingPacket> {

public static final long MAXIMUM_PACKET_AGE = TimeUnit.SECONDS.toMillis(60);

protected UUID uuid;
protected String macAddress;
protected int rssi; // in dBm
protected int calibratedRssi; // in dBm
Expand Down Expand Up @@ -76,7 +74,7 @@ public static List<Location> getLocations(List<? extends Beacon> beacons) {
public abstract BeaconLocationProvider<? extends Beacon> createLocationProvider();

public boolean hasAnyAdvertisingPacket() {
return advertisingPackets != null && !advertisingPackets.isEmpty();
return !advertisingPackets.isEmpty();
}

public P getOldestAdvertisingPacket() {
Expand Down Expand Up @@ -123,9 +121,16 @@ public ArrayList<P> getAdvertisingPacketsBefore(long timestamp) {
public void addAdvertisingPacket(P advertisingPacket) {
synchronized (advertisingPackets) {
rssi = advertisingPacket.getRssi();
if (!hasAnyAdvertisingPacket() || !advertisingPacket.dataEquals(getLatestAdvertisingPacket())) {

P latestAdvertisingPacket = getLatestAdvertisingPacket();
if (latestAdvertisingPacket == null || !advertisingPacket.dataEquals(latestAdvertisingPacket)) {
applyPropertiesFromAdvertisingPacket(advertisingPacket);
}

if (latestAdvertisingPacket != null && latestAdvertisingPacket.getTimestamp() > advertisingPacket.getTimestamp()) {
return;
}

advertisingPackets.add(advertisingPacket);
trimAdvertisingPackets();
invalidateDistance();
Expand Down Expand Up @@ -219,26 +224,37 @@ public WindowFilter createSuggestedWindowFilter() {
return new KalmanFilter(getLatestTimestamp());
}

/**
* This function and its reverse are implemented with indicative naming in BeaconUtil.
*
* @deprecated use {@link BeaconUtil#AscendingRssiComparator} instead
*/
@Deprecated
public static Comparator<Beacon> RssiComparator = new Comparator<Beacon>() {

public int compare(Beacon firstBeacon, Beacon secondBeacon) {
return firstBeacon.rssi - secondBeacon.rssi;
if (firstBeacon.equals(secondBeacon)) {
return 0;
}
return Integer.compare(firstBeacon.rssi, secondBeacon.rssi);
}

};

@Override
public String toString() {
return "Beacon{" +
", macAddress='" + macAddress + '\'' +
", rssi=" + rssi +
", calibratedRssi=" + calibratedRssi +
", calibratedDistance=" + calibratedDistance +
", transmissionPower=" + transmissionPower +
", advertisingPackets=" + advertisingPackets +
'}';
}

/*
Getter & Setter
*/

public UUID getUuid() {
return uuid;
}

public void setUuid(UUID uuid) {
this.uuid = uuid;
}

public String getMacAddress() {
return macAddress;
}
Expand Down Expand Up @@ -293,4 +309,5 @@ public LocationProvider getLocationProvider() {
public void setLocationProvider(BeaconLocationProvider<? extends Beacon> locationProvider) {
this.locationProvider = locationProvider;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.nexenio.bleindoorpositioning.ble.beacon.signal.WindowFilter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -106,8 +107,8 @@ private void notifyBeaconUpdateListeners(Beacon beacon) {
for (BeaconUpdateListener beaconUpdateListener : beaconUpdateListeners) {
try {
beaconUpdateListener.onBeaconUpdated(beacon);
} catch (Exception e) {
e.printStackTrace();
} catch (ClassCastException e) {
// meh
}
}
}
Expand All @@ -126,23 +127,11 @@ public static boolean unregisterBeaconUpdateListener(BeaconUpdateListener beacon
}

public static String getBeaconKey(String macAddress, AdvertisingPacket advertisingPacket) {
return getBeaconKey(macAddress, BeaconUtil.getReadableBeaconType(advertisingPacket));
return macAddress + "-" + Arrays.hashCode(advertisingPacket.getData());
}

private static String getBeaconKey(String macAddress, String beaconType) {
return macAddress + "-" + beaconType;
}

public static IBeacon getIBeacon(String macAddress) {
return (IBeacon) getBeacon(macAddress, IBeacon.class);
}

public static Eddystone getEddystone(String macAddress) {
return (Eddystone) getBeacon(macAddress, Eddystone.class);
}

public static Beacon getBeacon(String macAddress, Class<? extends Beacon> beaconClass) {
String key = getBeaconKey(macAddress, BeaconUtil.getReadableBeaconType(beaconClass));
public static Beacon getBeacon(String macAddress, AdvertisingPacket advertisingPacket) {
String key = getBeaconKey(macAddress, advertisingPacket);
return getInstance().beaconMap.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.nexenio.bleindoorpositioning.ble.beacon.signal.WindowFilter;
import com.nexenio.bleindoorpositioning.location.distance.BeaconDistanceCalculator;

import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -19,7 +20,7 @@ public abstract class BeaconUtil {
* @param transmissionPower the tx power (in dBm) of the beacon
* @return estimated range in meters
* @see <a href="https://support.kontakt.io/hc/en-gb/articles/201621521-Transmission-power-Range-and-RSSI">Kontakt.io
* Knowledge Base</a>
* Knowledge Base</a>
*/
public static float getAdvertisingRange(int transmissionPower) {
if (transmissionPower < -30) {
Expand Down Expand Up @@ -155,4 +156,28 @@ public static String getReadableBeaconType(Class<? extends Beacon> beaconClass)
return beaconClass.getSimpleName();
}

/**
* Used to sort beacons from highest rssi to lowest rssi.
*/
public static Comparator<Beacon> DescendingRssiComparator = new Comparator<Beacon>() {
public int compare(Beacon firstBeacon, Beacon secondBeacon) {
if (firstBeacon.equals(secondBeacon)) {
return 0;
}
return Integer.compare(secondBeacon.rssi, firstBeacon.rssi);
}
};

/**
* Used to sort beacons from lowest rssi to highest rssi.
*/
public static Comparator<Beacon> AscendingRssiComparator = new Comparator<Beacon>() {
public int compare(Beacon firstBeacon, Beacon secondBeacon) {
if (firstBeacon.equals(secondBeacon)) {
return 0;
}
return Integer.compare(firstBeacon.rssi, secondBeacon.rssi);
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public void applyPropertiesFromAdvertisingPacket(P advertisingPacket) {
setCalibratedRssi(advertisingPacket.getMeasuredPowerByte());
}

@Override
public String toString() {
return "IBeacon{" +
"proximityUuid=" + proximityUuid +
", major=" + major +
", minor=" + minor +
", macAddress='" + macAddress + '\'' +
", rssi=" + rssi +
", calibratedRssi=" + calibratedRssi +
", transmissionPower=" + transmissionPower +
", advertisingPackets=" + advertisingPackets +
'}';
}

/*
Getter & Setter
*/
Expand Down
Loading

0 comments on commit f94577f

Please sign in to comment.