Skip to content

Commit 8198648

Browse files
committed
#New Delete Algo
1 parent aa90f66 commit 8198648

File tree

2 files changed

+89
-61
lines changed

2 files changed

+89
-61
lines changed

androidblebeaconwrapperlib/src/main/java/com/androidblebeaconwrapperlib/beacon/BeaconHelper.java

Lines changed: 88 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.bluetooth.BluetoothManager;
77
import android.content.Context;
88
import android.text.TextUtils;
9+
import android.util.Log;
910

1011
import java.sql.Timestamp;
1112
import java.util.ArrayList;
@@ -47,10 +48,11 @@ public class BeaconHelper<T> {
4748
private List<DifferentBeaconEntity> newKeys;
4849
private BeaconResultListener beaconResultListener;
4950
private BeaconListener beaconListener;
50-
private Timer timer;
51+
private Timer timer, deleteBeaconTimer;
5152
private boolean isOnlyBeaconStuff;
5253
private List<IBeacon> iBeacons;
5354
private List<IBeacon> refreshIBeacons;
55+
private boolean isDeleteOperationGoing = false;
5456

5557
/**
5658
* Provide BeaconHelper instance.
@@ -73,8 +75,23 @@ private BeaconHelper(Activity context) {
7375
oldKeys = new ArrayList<>();
7476
newKeys = new ArrayList<>();
7577
iBeacons = new ArrayList<>();
76-
timer = new Timer();
78+
timer = deleteBeaconTimer = new Timer();
7779
isOnlyBeaconStuff = false;
80+
startDeleteBeaconTimer();
81+
}
82+
83+
private void startDeleteBeaconTimer() {
84+
deleteBeaconTimer.scheduleAtFixedRate(new TimerTask() {
85+
@Override
86+
public void run() {
87+
Log.d("TTAAGG", "++++++++++++DELETE START++++++++++++");
88+
isDeleteOperationGoing = true;
89+
deleteUnusedBeacons();
90+
deleteUnusedIBeacons();
91+
isDeleteOperationGoing = false;
92+
Log.d("TTAAGG", "++++++++++++DELETE END++++++++++++");
93+
}
94+
}, 0, 5000);
7895
}
7996

8097
/**
@@ -112,8 +129,7 @@ public void startBeaconUpdates(List<T> data, long timeInterval,
112129
@Override
113130
public void run() {
114131
context.runOnUiThread(() -> {
115-
if (beaconRefreshResultEntities != null
116-
&& !beaconRefreshResultEntities.isEmpty() &&
132+
if (beaconRefreshResultEntities != null &&
117133
isDifferent(oldKeys, newKeys)) {
118134
setOldKeys();
119135
BeaconHelper.this.beaconResultListener.
@@ -151,8 +167,7 @@ public void startBeaconUpdates(long timeInterval, BleAdapterEntity bleAdapterEnt
151167
timer.scheduleAtFixedRate(new TimerTask() {
152168
@Override
153169
public void run() {
154-
if (refreshIBeacons != null
155-
&& !refreshIBeacons.isEmpty()) {
170+
if (refreshIBeacons != null) {
156171
BeaconHelper.this.beaconListener.onResult(refreshIBeacons);
157172
}
158173
}
@@ -194,13 +209,58 @@ private void setMapFromList() throws BeaconKeySerializeException {
194209

195210
}
196211

212+
private void deleteUnusedBeacons() {
213+
if (beaconResultEntities != null && !beaconResultEntities.isEmpty()) {
214+
List<BeaconResultEntity> tempResult = new ArrayList<>(beaconResultEntities);
215+
Timestamp currentTimestamp = new Timestamp
216+
(new Date().getTime());
217+
for (int i = 0; i < tempResult.size(); i++) {
218+
Timestamp beaconLastUpdatedTimestamp = new Timestamp
219+
(tempResult.get(i).getBeaconDetail().getTimeStamp());
220+
long milliseconds = currentTimestamp.getTime() -
221+
beaconLastUpdatedTimestamp.getTime();
222+
int seconds = (int) milliseconds / 1000;
223+
seconds = (seconds % 3600) % 60;
224+
Log.d("TTAAGG", "Seconds of( " + tempResult.get(i).getKey() + " ): " + seconds);
225+
if (seconds >= 10) {
226+
beaconResultEntities.remove(tempResult.get(i));
227+
}
228+
}
229+
if (beaconResultEntities.isEmpty()) {
230+
setNewKeys();
231+
setRefreshData();
232+
}
233+
}
234+
}
235+
236+
private void deleteUnusedIBeacons() {
237+
List<IBeacon> tempResult = new ArrayList<>(iBeacons);
238+
Timestamp currentTimestamp = new Timestamp
239+
(new Date().getTime());
240+
for (int i = 0; i < tempResult.size(); i++) {
241+
Timestamp beaconLastUpdatedTimestamp = new Timestamp
242+
(tempResult.get(i).getTimeStamp());
243+
long milliseconds = currentTimestamp.getTime() -
244+
beaconLastUpdatedTimestamp.getTime();
245+
int seconds = (int) milliseconds / 1000;
246+
seconds = (seconds % 3600) % 60;
247+
if (seconds >= 10) {
248+
iBeacons.remove(tempResult.get(i));
249+
}
250+
}
251+
if (iBeacons.isEmpty()) {
252+
setRefreshDataForIBeacon();
253+
}
254+
}
255+
197256
private BluetoothAdapter.LeScanCallback mLeScanCallback =
198257
new BluetoothAdapter.LeScanCallback() {
199258
@Override
200259
public void onLeScan(final BluetoothDevice device, final int rssi,
201260
final byte[] scanRecord) {
202261
IBeacon iBeacon = IBeacon.fromScanData(scanRecord, rssi, device);
203-
if (iBeacon != null) {
262+
if (iBeacon != null && !isDeleteOperationGoing) {
263+
Log.d("TTAAGG", "*************CALLED*************");
204264
if (isOnlyBeaconStuff) {
205265
getOnlyBeaconData(iBeacon);
206266
} else {
@@ -227,42 +287,22 @@ private void getOnlyBeaconData(IBeacon iBeacon) {
227287
if (isAdd) {
228288
iBeacons.add(iBeacon);
229289
}
230-
deleteUnusedIBeacons();
231290
setRefreshDataForIBeacon();
232291
} catch (Exception e) {
233292
displayBeaconError(e.getMessage());
234293
}
235294
}
236295

237-
private void deleteUnusedIBeacons() {
238-
List<IBeacon> tempResult = new ArrayList<>(iBeacons);
239-
Timestamp currentTimestamp = new Timestamp
240-
(new Date().getTime());
241-
for (int i = 0; i < tempResult.size(); i++) {
242-
Timestamp beaconLastUpdatedTimestamp = new Timestamp
243-
(tempResult.get(i).getTimeStamp());
244-
long milliseconds = currentTimestamp.getTime() -
245-
beaconLastUpdatedTimestamp.getTime();
246-
int seconds = (int) milliseconds / 1000;
247-
seconds = (seconds % 3600) % 60;
248-
if (seconds >= 10) {
249-
iBeacons.remove(tempResult.get(i));
250-
}
251-
}
252-
}
253-
254-
private void setRefreshDataForIBeacon() {
255-
refreshIBeacons = new ArrayList<>(iBeacons);
256-
}
257296

258297
private void getBeaconFilteredData(IBeacon iBeacon) {
259298
try {
260299
if (data == null) {
261300
throw new BeaconKeySerializeException("List can not be null");
262301
}
263-
if (!dataMap.containsKey(iBeacon.getBleDataPayload())) {
302+
if (!isStringTypeData() && !dataMap.containsKey(iBeacon.getBleDataPayload())) {
264303
throw new BeaconKeySerializeException("Key(Payload) not found in data");
265304
}
305+
266306
BeaconResultEntity entity = isPresentInBeaconPagerEntities(iBeacon.
267307
getBluetoothAddress());
268308
if (entity == null) {
@@ -278,7 +318,6 @@ private void getBeaconFilteredData(IBeacon iBeacon) {
278318
filterBeaconData(iBeacon);
279319
entity.setResult(filteredData);
280320
}
281-
deleteUnusedBeacons();
282321
sortBeaconData();
283322
setNewKeys();
284323
setRefreshData();
@@ -287,37 +326,6 @@ private void getBeaconFilteredData(IBeacon iBeacon) {
287326
}
288327
}
289328

290-
private void deleteUnusedBeacons() {
291-
List<BeaconResultEntity> tempResult = new ArrayList<>(beaconResultEntities);
292-
Timestamp currentTimestamp = new Timestamp
293-
(new Date().getTime());
294-
for (int i = 0; i < tempResult.size(); i++) {
295-
Timestamp beaconLastUpdatedTimestamp = new Timestamp
296-
(tempResult.get(i).getBeaconDetail().getTimeStamp());
297-
long milliseconds = currentTimestamp.getTime() -
298-
beaconLastUpdatedTimestamp.getTime();
299-
int seconds = (int) milliseconds / 1000;
300-
seconds = (seconds % 3600) % 60;
301-
if (seconds >= 10) {
302-
beaconResultEntities.remove(tempResult.get(i));
303-
}
304-
}
305-
}
306-
307-
private void setRefreshData() {
308-
beaconRefreshResultEntities = new ArrayList<>(beaconResultEntities);
309-
}
310-
311-
private void setNewKeys() {
312-
newKeys.clear();
313-
for (int i = 0; i < beaconResultEntities.size(); i++) {
314-
String bluetoothAddress = beaconResultEntities.get(i).getBeaconDetail().
315-
getBluetoothAddress();
316-
String name = beaconResultEntities.get(i).getBeaconDetail().
317-
getBleDataPayload();
318-
newKeys.add(new DifferentBeaconEntity(bluetoothAddress, name));
319-
}
320-
}
321329

322330
private BeaconResultEntity isPresentInBeaconPagerEntities(String macAddress)
323331
throws BeaconKeySerializeException {
@@ -363,6 +371,25 @@ private void filterBeaconMapData(IBeacon iBeacon) {
363371
}
364372
};
365373

374+
private void setRefreshDataForIBeacon() {
375+
refreshIBeacons = new ArrayList<>(iBeacons);
376+
}
377+
378+
private void setRefreshData() {
379+
beaconRefreshResultEntities = new ArrayList<>(beaconResultEntities);
380+
}
381+
382+
private void setNewKeys() {
383+
newKeys.clear();
384+
for (int i = 0; i < beaconResultEntities.size(); i++) {
385+
String bluetoothAddress = beaconResultEntities.get(i).getBeaconDetail().
386+
getBluetoothAddress();
387+
String name = beaconResultEntities.get(i).getBeaconDetail().
388+
getBleDataPayload();
389+
newKeys.add(new DifferentBeaconEntity(bluetoothAddress, name));
390+
}
391+
}
392+
366393
private boolean isStringTypeData() {
367394
boolean isStringType = false;
368395
for (int i = 0; i < data.size(); i++) {

androidblebeaconwrapperlib/src/main/java/com/androidblebeaconwrapperlib/beacon/IBeacon.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ public static IBeacon fromScanData(byte[] scanData, int rssi, BluetoothDevice de
368368
iBeacon.txPower = (int) scanData[startByte + 24]; // this one is signed
369369
iBeacon.rssi = rssi;
370370
iBeacon.bleDataPayload = getPayloadData(scanData);
371+
iBeacon.timeStamp = new Date().getTime();
371372
iBeacon.timezoneString = getDateCurrentTimeZone();
372373

373374
// AirLocate:

0 commit comments

Comments
 (0)