Skip to content

Commit

Permalink
Improve NearbyBeaconBroadcastReceiver
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashank Raj committed Nov 15, 2018
1 parent 7dd1621 commit c2ef7c9
Showing 1 changed file with 47 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void onError(MSException msException) {

}
});
autoStopScan(context, 10000);
}

@Override
Expand All @@ -51,66 +52,67 @@ public void onFailure(MSException msException) {
} catch (MSException e) {
e.printStackTrace();
}
if (beaconstac != null) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
}

@Override
public void onLost(Message message) {
}
});
}
}

private void autoStopScan(final Context context, int duration){
if (beaconstac != null) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
beaconstac.stopScanningBeacons(new MSErrorListener() {
@Override
public void onError(MSException msException) {

}
});
}
}, duration); // Set the duration for which the scan needs to run for.
} else {
// Handle reinitialization if Beaconstac's instance is null.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
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) {
return;
}
beaconstac = Beaconstac.initialize(context.getApplicationContext(), "MY_DEVELOPER_TOKEN", new MSSyncListener() {
@Override
public void run() {
public void onSuccess() {
beaconstac.stopScanningBeacons(new MSErrorListener() {
@Override
public void onError(MSException msException) {

}
});
}
}, 10000); // Set the duration for which the scan needs to run for.
} else {

// Handle reinitialization if Beaconstac's instance is null.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
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) {
return;
}
beaconstac = Beaconstac.initialize(context.getApplicationContext(), "MY_DEVELOPER_TOKEN", new MSSyncListener() {
@Override
public void onSuccess() {
beaconstac.stopScanningBeacons(new MSErrorListener() {
@Override
public void onError(MSException msException) {

}
});
}

@Override
public void onFailure(MSException msException) {
// Stop scan even if initialization failed
beaconstac.stopScanningBeacons(new MSErrorListener() {
@Override
public void onError(MSException msException) {
public void onFailure(MSException msException) {
// Stop scan even if initialization failed
beaconstac.stopScanningBeacons(new MSErrorListener() {
@Override
public void onError(MSException msException) {

}
});
}
});
} catch (MSException e) {
e.printStackTrace();
}
}
});
}
}, 10000); // Set the duration for which the scan needs to run for.


});
} catch (MSException e) {
e.printStackTrace();
}
}
}, duration); // Set the duration for which the scan needs to run for.

@Override
public void onLost(Message message) {
}
});
}
}
}

0 comments on commit c2ef7c9

Please sign in to comment.