Skip to content

Commit c2ef7c9

Browse files
author
Shashank Raj
committed
Improve NearbyBeaconBroadcastReceiver
1 parent 7dd1621 commit c2ef7c9

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

examples/BeaconstacExample/app/src/main/java/com/mobstac/beaconstacdemo/NearbyBeaconBroadcastReceiver.java

+47-45
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void onError(MSException msException) {
4141

4242
}
4343
});
44+
autoStopScan(context, 10000);
4445
}
4546

4647
@Override
@@ -51,66 +52,67 @@ public void onFailure(MSException msException) {
5152
} catch (MSException e) {
5253
e.printStackTrace();
5354
}
54-
if (beaconstac != null) {
55-
Handler handler = new Handler();
56-
handler.postDelayed(new Runnable() {
55+
}
56+
57+
@Override
58+
public void onLost(Message message) {
59+
}
60+
});
61+
}
62+
}
63+
64+
private void autoStopScan(final Context context, int duration){
65+
if (beaconstac != null) {
66+
Handler handler = new Handler();
67+
handler.postDelayed(new Runnable() {
68+
@Override
69+
public void run() {
70+
beaconstac.stopScanningBeacons(new MSErrorListener() {
71+
@Override
72+
public void onError(MSException msException) {
73+
74+
}
75+
});
76+
}
77+
}, duration); // Set the duration for which the scan needs to run for.
78+
} else {
79+
// Handle reinitialization if Beaconstac's instance is null.
80+
Handler handler = new Handler();
81+
handler.postDelayed(new Runnable() {
82+
@Override
83+
public void run() {
84+
try {
85+
if (ActivityCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
86+
return;
87+
}
88+
beaconstac = Beaconstac.initialize(context.getApplicationContext(), "MY_DEVELOPER_TOKEN", new MSSyncListener() {
5789
@Override
58-
public void run() {
90+
public void onSuccess() {
5991
beaconstac.stopScanningBeacons(new MSErrorListener() {
6092
@Override
6193
public void onError(MSException msException) {
6294

6395
}
6496
});
6597
}
66-
}, 10000); // Set the duration for which the scan needs to run for.
67-
} else {
6898

69-
// Handle reinitialization if Beaconstac's instance is null.
70-
Handler handler = new Handler();
71-
handler.postDelayed(new Runnable() {
7299
@Override
73-
public void run() {
74-
try {
75-
if (ActivityCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
76-
return;
77-
}
78-
beaconstac = Beaconstac.initialize(context.getApplicationContext(), "MY_DEVELOPER_TOKEN", new MSSyncListener() {
79-
@Override
80-
public void onSuccess() {
81-
beaconstac.stopScanningBeacons(new MSErrorListener() {
82-
@Override
83-
public void onError(MSException msException) {
84-
85-
}
86-
});
87-
}
88-
89-
@Override
90-
public void onFailure(MSException msException) {
91-
// Stop scan even if initialization failed
92-
beaconstac.stopScanningBeacons(new MSErrorListener() {
93-
@Override
94-
public void onError(MSException msException) {
100+
public void onFailure(MSException msException) {
101+
// Stop scan even if initialization failed
102+
beaconstac.stopScanningBeacons(new MSErrorListener() {
103+
@Override
104+
public void onError(MSException msException) {
95105

96-
}
97-
});
98-
}
99-
});
100-
} catch (MSException e) {
101-
e.printStackTrace();
102-
}
106+
}
107+
});
103108
}
104-
}, 10000); // Set the duration for which the scan needs to run for.
105-
106-
109+
});
110+
} catch (MSException e) {
111+
e.printStackTrace();
107112
}
108113
}
114+
}, duration); // Set the duration for which the scan needs to run for.
109115

110-
@Override
111-
public void onLost(Message message) {
112-
}
113-
});
114116
}
115117
}
116118
}

0 commit comments

Comments
 (0)