Skip to content

Commit e79b3f8

Browse files
committed
Synchonised access to the RssiLog
1 parent e8db168 commit e79b3f8

2 files changed

Lines changed: 37 additions & 27 deletions

File tree

library/src/uk/co/alt236/bluetoothlelib/device/BluetoothLeDevice.java

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public BluetoothLeDevice(BluetoothDevice device, int rssi, byte[] scanRecord, lo
7171
mFirstTimestamp = timestamp;
7272
mRecordStore = new AdRecordStore(AdRecordUtils.parseScanRecordAsSparseArray(scanRecord));
7373
mScanRecord = scanRecord;
74-
mRssiLog = Collections.synchronizedMap(
75-
new LimitedLinkHashMap<Long, Integer>(MAX_RSSI_LOG_SIZE));
74+
mRssiLog = new LimitedLinkHashMap<Long, Integer>(MAX_RSSI_LOG_SIZE);
7675
updateRssiReading(timestamp, rssi);
7776
}
7877

@@ -108,8 +107,7 @@ protected BluetoothLeDevice(Parcel in) {
108107
mFirstRssi = b.getInt(PARCEL_EXTRA_FIRST_RSSI, 0);
109108
mFirstTimestamp = b.getLong(PARCEL_EXTRA_FIRST_TIMESTAMP, 0);
110109
mRecordStore = b.getParcelable(PARCEL_EXTRA_DEVICE_SCANRECORD_STORE);
111-
mRssiLog = Collections.synchronizedMap(
112-
(Map<Long, Integer>) b.getSerializable(PARCEL_EXTRA_DEVICE_RSSI_LOG));
110+
mRssiLog = (Map<Long, Integer>) b.getSerializable(PARCEL_EXTRA_DEVICE_RSSI_LOG);
113111
mScanRecord = b.getByteArray(PARCEL_EXTRA_DEVICE_SCANRECORD);
114112
}
115113

@@ -120,13 +118,15 @@ protected BluetoothLeDevice(Parcel in) {
120118
* @param rssiReading the rssi reading
121119
*/
122120
private void addToRssiLog(long timestamp, int rssiReading){
123-
if(timestamp - mCurrentTimestamp > LOG_INVALIDATION_THRESHOLD){
124-
mRssiLog.clear();
121+
synchronized (mRssiLog) {
122+
if(timestamp - mCurrentTimestamp > LOG_INVALIDATION_THRESHOLD){
123+
mRssiLog.clear();
124+
}
125+
126+
mCurrentRssi = rssiReading;
127+
mCurrentTimestamp = timestamp;
128+
mRssiLog.put(timestamp, rssiReading);
125129
}
126-
127-
mCurrentRssi = rssiReading;
128-
mCurrentTimestamp = timestamp;
129-
mRssiLog.put(timestamp, rssiReading);
130130
}
131131

132132
/* (non-Javadoc)
@@ -264,7 +264,9 @@ public int getRssi() {
264264
* @return the rssi log
265265
*/
266266
protected Map<Long, Integer> getRssiLog() {
267-
return mRssiLog;
267+
synchronized (mRssiLog) {
268+
return mRssiLog;
269+
}
268270
}
269271

270272
/**
@@ -275,18 +277,26 @@ protected Map<Long, Integer> getRssiLog() {
275277
public double getRunningAverageRssi(){
276278
int sum = 0;
277279
int count = 0;
278-
final Iterator<Long> it1 = mRssiLog.keySet().iterator();
279-
280-
while(it1.hasNext()){
281-
count ++;
282-
sum += mRssiLog.get(it1.next());
283-
}
284-
285-
if(count > 0){
286-
return sum/count;
287-
} else {
288-
return 0;
289-
}
280+
281+
synchronized (mRssiLog) {
282+
final Iterator<Long> it1 = mRssiLog.keySet().iterator();
283+
284+
while(it1.hasNext()){
285+
count ++;
286+
sum += mRssiLog.get(it1.next());
287+
}
288+
}
289+
// for(final Map.Entry<Long,Integer> e : mRssiLog.entrySet()){
290+
// count ++;
291+
// sum += e.getValue();
292+
// }
293+
294+
if(count > 0){
295+
return sum/count;
296+
} else {
297+
return 0;
298+
}
299+
290300
}
291301

292302
/**
@@ -339,7 +349,7 @@ public String toString() {
339349
* @param timestamp the timestamp
340350
* @param rssiReading the rssi reading
341351
*/
342-
public synchronized void updateRssiReading(long timestamp, int rssiReading){
352+
public void updateRssiReading(long timestamp, int rssiReading){
343353
addToRssiLog(timestamp, rssiReading);
344354
}
345355

library/src/uk/co/alt236/bluetoothlelib/util/LimitedLinkHashMap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ public class LimitedLinkHashMap<K, V> extends LinkedHashMap<K, V>{
77
private static final long serialVersionUID = -5375660288461724925L;
88

99
private final int mMaxSize;
10+
1011
public LimitedLinkHashMap(int maxSize){
11-
super();
12+
super(maxSize + 1, 1, false);
1213
mMaxSize = maxSize;
1314
}
1415

1516
@Override
16-
protected boolean removeEldestEntry(Map.Entry<K, V> eldest)
17-
{
17+
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
1818
return this.size() > mMaxSize;
1919
}
2020
}

0 commit comments

Comments
 (0)