6
6
import android .bluetooth .BluetoothManager ;
7
7
import android .content .Context ;
8
8
import android .text .TextUtils ;
9
+ import android .util .Log ;
9
10
10
11
import java .sql .Timestamp ;
11
12
import java .util .ArrayList ;
@@ -47,10 +48,11 @@ public class BeaconHelper<T> {
47
48
private List <DifferentBeaconEntity > newKeys ;
48
49
private BeaconResultListener beaconResultListener ;
49
50
private BeaconListener beaconListener ;
50
- private Timer timer ;
51
+ private Timer timer , deleteBeaconTimer ;
51
52
private boolean isOnlyBeaconStuff ;
52
53
private List <IBeacon > iBeacons ;
53
54
private List <IBeacon > refreshIBeacons ;
55
+ private boolean isDeleteOperationGoing = false ;
54
56
55
57
/**
56
58
* Provide BeaconHelper instance.
@@ -73,8 +75,23 @@ private BeaconHelper(Activity context) {
73
75
oldKeys = new ArrayList <>();
74
76
newKeys = new ArrayList <>();
75
77
iBeacons = new ArrayList <>();
76
- timer = new Timer ();
78
+ timer = deleteBeaconTimer = new Timer ();
77
79
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 );
78
95
}
79
96
80
97
/**
@@ -112,8 +129,7 @@ public void startBeaconUpdates(List<T> data, long timeInterval,
112
129
@ Override
113
130
public void run () {
114
131
context .runOnUiThread (() -> {
115
- if (beaconRefreshResultEntities != null
116
- && !beaconRefreshResultEntities .isEmpty () &&
132
+ if (beaconRefreshResultEntities != null &&
117
133
isDifferent (oldKeys , newKeys )) {
118
134
setOldKeys ();
119
135
BeaconHelper .this .beaconResultListener .
@@ -151,8 +167,7 @@ public void startBeaconUpdates(long timeInterval, BleAdapterEntity bleAdapterEnt
151
167
timer .scheduleAtFixedRate (new TimerTask () {
152
168
@ Override
153
169
public void run () {
154
- if (refreshIBeacons != null
155
- && !refreshIBeacons .isEmpty ()) {
170
+ if (refreshIBeacons != null ) {
156
171
BeaconHelper .this .beaconListener .onResult (refreshIBeacons );
157
172
}
158
173
}
@@ -194,13 +209,58 @@ private void setMapFromList() throws BeaconKeySerializeException {
194
209
195
210
}
196
211
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
+
197
256
private BluetoothAdapter .LeScanCallback mLeScanCallback =
198
257
new BluetoothAdapter .LeScanCallback () {
199
258
@ Override
200
259
public void onLeScan (final BluetoothDevice device , final int rssi ,
201
260
final byte [] scanRecord ) {
202
261
IBeacon iBeacon = IBeacon .fromScanData (scanRecord , rssi , device );
203
- if (iBeacon != null ) {
262
+ if (iBeacon != null && !isDeleteOperationGoing ) {
263
+ Log .d ("TTAAGG" , "*************CALLED*************" );
204
264
if (isOnlyBeaconStuff ) {
205
265
getOnlyBeaconData (iBeacon );
206
266
} else {
@@ -227,42 +287,22 @@ private void getOnlyBeaconData(IBeacon iBeacon) {
227
287
if (isAdd ) {
228
288
iBeacons .add (iBeacon );
229
289
}
230
- deleteUnusedIBeacons ();
231
290
setRefreshDataForIBeacon ();
232
291
} catch (Exception e ) {
233
292
displayBeaconError (e .getMessage ());
234
293
}
235
294
}
236
295
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
- }
257
296
258
297
private void getBeaconFilteredData (IBeacon iBeacon ) {
259
298
try {
260
299
if (data == null ) {
261
300
throw new BeaconKeySerializeException ("List can not be null" );
262
301
}
263
- if (!dataMap .containsKey (iBeacon .getBleDataPayload ())) {
302
+ if (!isStringTypeData () && ! dataMap .containsKey (iBeacon .getBleDataPayload ())) {
264
303
throw new BeaconKeySerializeException ("Key(Payload) not found in data" );
265
304
}
305
+
266
306
BeaconResultEntity entity = isPresentInBeaconPagerEntities (iBeacon .
267
307
getBluetoothAddress ());
268
308
if (entity == null ) {
@@ -278,7 +318,6 @@ private void getBeaconFilteredData(IBeacon iBeacon) {
278
318
filterBeaconData (iBeacon );
279
319
entity .setResult (filteredData );
280
320
}
281
- deleteUnusedBeacons ();
282
321
sortBeaconData ();
283
322
setNewKeys ();
284
323
setRefreshData ();
@@ -287,37 +326,6 @@ private void getBeaconFilteredData(IBeacon iBeacon) {
287
326
}
288
327
}
289
328
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
- }
321
329
322
330
private BeaconResultEntity isPresentInBeaconPagerEntities (String macAddress )
323
331
throws BeaconKeySerializeException {
@@ -363,6 +371,25 @@ private void filterBeaconMapData(IBeacon iBeacon) {
363
371
}
364
372
};
365
373
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
+
366
393
private boolean isStringTypeData () {
367
394
boolean isStringType = false ;
368
395
for (int i = 0 ; i < data .size (); i ++) {
0 commit comments