Skip to content

Commit bdd64f0

Browse files
authored
Merge pull request #30 from peterdeka/master
Added setRssiFilter for android in order to be able to configure the rssi filter type and options.
2 parents 3c8de89 + 9c6f36c commit bdd64f0

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ DeviceEventEmitter.addListener('beaconsDidRange', (data) => {
171171
| **setForegroundScanPeriod(period: number): void** | Sets the duration in milliseconds of each Bluetooth LE scan cycle to look for beacons (in foreground). For more info [take a look at the official docs](https://altbeacon.github.io/android-beacon-library/javadoc/index.html) |
172172
| **setBackgroundScanPeriod(period: number): void** | Sets the duration in milliseconds of each Bluetooth LE scan cycle to look for beacons (in background). For more info [take a look at the official docs](https://altbeacon.github.io/android-beacon-library/javadoc/index.html) |
173173
| **setBackgroundBetweenScanPeriod(period: number): void** | Sets the duration in milliseconds spent not scanning between each Bluetooth LE scan cycle when no ranging/monitoring clients are in the foreground. For more info [take a look at the official docs](https://altbeacon.github.io/android-beacon-library/javadoc/index.html) |
174+
| **setRssiFilter(filterType: int, avgModifier: number): void** | Sets the RSSI averaging method. The parameter `filterType` must be one of the exported constants `ARMA_RSSI_FILTER` or `RUNNING_AVG_RSSI_FILTER`. The `avgModifier` param changes the rate of the averaging function For the ARMA filter it's in the range 0.1-1.0, for the running average it's the filter window in milliseconds. For more info [take a look at the docs](https://altbeacon.github.io/android-beacon-library/distance_vs_time.html) |
174175
| **setHardwareEqualityEnforced(e: boolean): void** | Configures whether the bluetoothAddress (mac address) must be the same for two Beacons to be configured equal. This setting applies to all beacon instances in the same process. Defaults to false for backward compatibility. Useful when all the beacons you are working with have the same UUID, major and minor (they are only uniquely identifiable by their mac address), otherwise the module will detect all the beacons as if they were only one. For more info [take a look at the official docs](https://altbeacon.github.io/android-beacon-library/javadoc/index.html) |
175176
| **getRangedRegions(): promise** | Returns a promise that resolves in an array with the regions being ranged. |
176177
| **getMonitoredRegions(): promise** | Returns a promise that resolves in an array with the regions being monitored. |

android/src/main/java/com/mackentoch/beaconsandroid/BeaconsAndroidModule.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727
import org.altbeacon.beacon.MonitorNotifier;
2828
import org.altbeacon.beacon.RangeNotifier;
2929
import org.altbeacon.beacon.Region;
30+
import org.altbeacon.beacon.service.ArmaRssiFilter;
31+
import org.altbeacon.beacon.service.RunningAverageRssiFilter;
3032

3133
import java.util.Collection;
3234
import java.util.HashMap;
3335
import java.util.Map;
3436

3537
public class BeaconsAndroidModule extends ReactContextBaseJavaModule implements BeaconConsumer {
3638
private static final String LOG_TAG = "BeaconsAndroidModule";
39+
private static final int RUNNING_AVG_RSSI_FILTER = 0;
40+
private static final int ARMA_RSSI_FILTER = 1;
3741
private ReactApplicationContext mReactContext;
3842
private Context mApplicationContext;
3943
private BeaconManager mBeaconManager;
@@ -62,6 +66,8 @@ public Map<String, Object> getConstants() {
6266
constants.put("NOT_SUPPORTED_BLE", BeaconTransmitter.NOT_SUPPORTED_BLE);
6367
constants.put("NOT_SUPPORTED_CANNOT_GET_ADVERTISER_MULTIPLE_ADVERTISEMENTS", BeaconTransmitter.NOT_SUPPORTED_CANNOT_GET_ADVERTISER_MULTIPLE_ADVERTISEMENTS);
6468
constants.put("NOT_SUPPORTED_CANNOT_GET_ADVERTISER", BeaconTransmitter.NOT_SUPPORTED_CANNOT_GET_ADVERTISER);
69+
constants.put("RUNNING_AVG_RSSI_FILTER",RUNNING_AVG_RSSI_FILTER);
70+
constants.put("ARMA_RSSI_FILTER",ARMA_RSSI_FILTER);
6571
return constants;
6672
}
6773

@@ -100,6 +106,27 @@ public void setForegroundBetweenScanPeriod(int period) {
100106
mBeaconManager.setForegroundBetweenScanPeriod((long) period);
101107
}
102108

109+
@ReactMethod
110+
public void setRssiFilter(int filterType, double avgModifier) {
111+
String logMsg = "Could not set the rssi filter.";
112+
if (filterType==RUNNING_AVG_RSSI_FILTER){
113+
logMsg="Setting filter RUNNING_AVG";
114+
BeaconManager.setRssiFilterImplClass(RunningAverageRssiFilter.class);
115+
if (avgModifier>0){
116+
RunningAverageRssiFilter.setSampleExpirationMilliseconds((long) avgModifier);
117+
logMsg+=" with custom avg modifier";
118+
}
119+
} else if (filterType==ARMA_RSSI_FILTER){
120+
logMsg="Setting filter ARMA";
121+
BeaconManager.setRssiFilterImplClass(ArmaRssiFilter.class);
122+
if (avgModifier>0){
123+
ArmaRssiFilter.setDEFAULT_ARMA_SPEED(avgModifier);
124+
logMsg+=" with custom avg modifier";
125+
}
126+
}
127+
Log.d(LOG_TAG, logMsg);
128+
}
129+
103130
@ReactMethod
104131
public void checkTransmissionSupported(Callback callback) {
105132
int result = BeaconTransmitter.checkTransmissionSupported(mReactContext);

lib/module.android.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const tramissionSupport: Array<string> = [
2222
'NOT_SUPPORTED_CANNOT_GET_ADVERTISER_MULTIPLE_ADVERTISEMENTS'
2323
];
2424

25+
const ARMA_RSSI_FILTER = beaconsAndroid.ARMA_RSSI_FILTER;
26+
const RUNNING_AVG_RSSI_FILTER = beaconsAndroid.RUNNING_AVG_RSSI_FILTER;
27+
2528
function setHardwareEqualityEnforced(e: boolean): void {
2629
beaconsAndroid.setHardwareEqualityEnforced(e);
2730
}
@@ -94,6 +97,10 @@ function setForegroundScanPeriod(period: number): void {
9497
beaconsAndroid.setForegroundScanPeriod(period);
9598
}
9699

100+
function setRssiFilter(filterType: number, avgModifier: number): void {
101+
beaconsAndroid.setRssiFilter(filterType, avgModifier);
102+
}
103+
97104
function getRangedRegions(): Promise<any> {
98105
return new Promise((resolve, reject) => {
99106
beaconsAndroid.getRangedRegions(resolve);
@@ -169,7 +176,7 @@ function startRangingBeaconsInRegion(regionId: string, beaconsUUID?: string): Pr
169176
* stops monittorings for a region
170177
*
171178
* @param {BeaconRegion} region region (see BeaconRegion type)
172-
* @returns {Promise<any>} promise resolves to void or error
179+
* @returns {Promise<any>} promise resolves to void or error
173180
*/
174181
function stopMonitoringForRegion(region: BeaconRegion): Promise<any> {
175182
return new Promise(
@@ -213,11 +220,14 @@ export default {
213220
setBackgroundScanPeriod,
214221
setBackgroundBetweenScanPeriod,
215222
setForegroundScanPeriod,
223+
setRssiFilter,
216224
checkTransmissionSupported,
217225
getRangedRegions,
218226
getMonitoredRegions,
219227
startMonitoringForRegion,
220228
startRangingBeaconsInRegion,
221229
stopMonitoringForRegion,
222-
stopRangingBeaconsInRegion
230+
stopRangingBeaconsInRegion,
231+
ARMA_RSSI_FILTER,
232+
RUNNING_AVG_RSSI_FILTER
223233
};

0 commit comments

Comments
 (0)