Synchonised access to the RssiLog
This commit is contained in:
@@ -71,8 +71,7 @@ public class BluetoothLeDevice implements Parcelable{
|
|||||||
mFirstTimestamp = timestamp;
|
mFirstTimestamp = timestamp;
|
||||||
mRecordStore = new AdRecordStore(AdRecordUtils.parseScanRecordAsSparseArray(scanRecord));
|
mRecordStore = new AdRecordStore(AdRecordUtils.parseScanRecordAsSparseArray(scanRecord));
|
||||||
mScanRecord = scanRecord;
|
mScanRecord = scanRecord;
|
||||||
mRssiLog = Collections.synchronizedMap(
|
mRssiLog = new LimitedLinkHashMap<Long, Integer>(MAX_RSSI_LOG_SIZE);
|
||||||
new LimitedLinkHashMap<Long, Integer>(MAX_RSSI_LOG_SIZE));
|
|
||||||
updateRssiReading(timestamp, rssi);
|
updateRssiReading(timestamp, rssi);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,8 +107,7 @@ public class BluetoothLeDevice implements Parcelable{
|
|||||||
mFirstRssi = b.getInt(PARCEL_EXTRA_FIRST_RSSI, 0);
|
mFirstRssi = b.getInt(PARCEL_EXTRA_FIRST_RSSI, 0);
|
||||||
mFirstTimestamp = b.getLong(PARCEL_EXTRA_FIRST_TIMESTAMP, 0);
|
mFirstTimestamp = b.getLong(PARCEL_EXTRA_FIRST_TIMESTAMP, 0);
|
||||||
mRecordStore = b.getParcelable(PARCEL_EXTRA_DEVICE_SCANRECORD_STORE);
|
mRecordStore = b.getParcelable(PARCEL_EXTRA_DEVICE_SCANRECORD_STORE);
|
||||||
mRssiLog = Collections.synchronizedMap(
|
mRssiLog = (Map<Long, Integer>) b.getSerializable(PARCEL_EXTRA_DEVICE_RSSI_LOG);
|
||||||
(Map<Long, Integer>) b.getSerializable(PARCEL_EXTRA_DEVICE_RSSI_LOG));
|
|
||||||
mScanRecord = b.getByteArray(PARCEL_EXTRA_DEVICE_SCANRECORD);
|
mScanRecord = b.getByteArray(PARCEL_EXTRA_DEVICE_SCANRECORD);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,13 +118,15 @@ public class BluetoothLeDevice implements Parcelable{
|
|||||||
* @param rssiReading the rssi reading
|
* @param rssiReading the rssi reading
|
||||||
*/
|
*/
|
||||||
private void addToRssiLog(long timestamp, int rssiReading){
|
private void addToRssiLog(long timestamp, int rssiReading){
|
||||||
if(timestamp - mCurrentTimestamp > LOG_INVALIDATION_THRESHOLD){
|
synchronized (mRssiLog) {
|
||||||
mRssiLog.clear();
|
if(timestamp - mCurrentTimestamp > LOG_INVALIDATION_THRESHOLD){
|
||||||
}
|
mRssiLog.clear();
|
||||||
|
}
|
||||||
|
|
||||||
mCurrentRssi = rssiReading;
|
mCurrentRssi = rssiReading;
|
||||||
mCurrentTimestamp = timestamp;
|
mCurrentTimestamp = timestamp;
|
||||||
mRssiLog.put(timestamp, rssiReading);
|
mRssiLog.put(timestamp, rssiReading);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@@ -264,7 +264,9 @@ public class BluetoothLeDevice implements Parcelable{
|
|||||||
* @return the rssi log
|
* @return the rssi log
|
||||||
*/
|
*/
|
||||||
protected Map<Long, Integer> getRssiLog() {
|
protected Map<Long, Integer> getRssiLog() {
|
||||||
return mRssiLog;
|
synchronized (mRssiLog) {
|
||||||
|
return mRssiLog;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -275,18 +277,26 @@ public class BluetoothLeDevice implements Parcelable{
|
|||||||
public double getRunningAverageRssi(){
|
public double getRunningAverageRssi(){
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
final Iterator<Long> it1 = mRssiLog.keySet().iterator();
|
|
||||||
|
|
||||||
while(it1.hasNext()){
|
synchronized (mRssiLog) {
|
||||||
count ++;
|
final Iterator<Long> it1 = mRssiLog.keySet().iterator();
|
||||||
sum += mRssiLog.get(it1.next());
|
|
||||||
}
|
while(it1.hasNext()){
|
||||||
|
count ++;
|
||||||
|
sum += mRssiLog.get(it1.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// for(final Map.Entry<Long,Integer> e : mRssiLog.entrySet()){
|
||||||
|
// count ++;
|
||||||
|
// sum += e.getValue();
|
||||||
|
// }
|
||||||
|
|
||||||
|
if(count > 0){
|
||||||
|
return sum/count;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if(count > 0){
|
|
||||||
return sum/count;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -339,7 +349,7 @@ public class BluetoothLeDevice implements Parcelable{
|
|||||||
* @param timestamp the timestamp
|
* @param timestamp the timestamp
|
||||||
* @param rssiReading the rssi reading
|
* @param rssiReading the rssi reading
|
||||||
*/
|
*/
|
||||||
public synchronized void updateRssiReading(long timestamp, int rssiReading){
|
public void updateRssiReading(long timestamp, int rssiReading){
|
||||||
addToRssiLog(timestamp, rssiReading);
|
addToRssiLog(timestamp, rssiReading);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ public class LimitedLinkHashMap<K, V> extends LinkedHashMap<K, V>{
|
|||||||
private static final long serialVersionUID = -5375660288461724925L;
|
private static final long serialVersionUID = -5375660288461724925L;
|
||||||
|
|
||||||
private final int mMaxSize;
|
private final int mMaxSize;
|
||||||
|
|
||||||
public LimitedLinkHashMap(int maxSize){
|
public LimitedLinkHashMap(int maxSize){
|
||||||
super();
|
super(maxSize + 1, 1, false);
|
||||||
mMaxSize = maxSize;
|
mMaxSize = maxSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest)
|
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
||||||
{
|
|
||||||
return this.size() > mMaxSize;
|
return this.size() > mMaxSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user