Java Code Examples for com.polidea.rxandroidble2.exceptions.BleScanException#UNDOCUMENTED_SCAN_THROTTLE

The following examples show how to use com.polidea.rxandroidble2.exceptions.BleScanException#UNDOCUMENTED_SCAN_THROTTLE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScanPreconditionsVerifierApi24.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@Override
public void verify(boolean checkLocationProviderState) {
    scanPreconditionVerifierApi18.verify(checkLocationProviderState);

    /*
     * Android 7.0 (API 24) introduces an undocumented scan throttle for applications that try to scan more than 5 times during
     * a 30 second window. More on the topic: https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983
     */

    // TODO: [DS] 27.06.2017 Think if persisting this information through Application close is needed
    final int oldestCheckTimestampIndex = getOldestCheckTimestampIndex();
    final long oldestCheckTimestamp = previousChecks[oldestCheckTimestampIndex];
    final long currentCheckTimestamp = timeScheduler.now(TimeUnit.MILLISECONDS);

    if (currentCheckTimestamp - oldestCheckTimestamp < EXCESSIVE_SCANNING_PERIOD) {
        throw new BleScanException(
                BleScanException.UNDOCUMENTED_SCAN_THROTTLE,
                new Date(oldestCheckTimestamp + EXCESSIVE_SCANNING_PERIOD)
        );
    }
    previousChecks[oldestCheckTimestampIndex] = currentCheckTimestamp;
}
 
Example 2
Source File: ScanExceptionHandler.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
/**
 * Show toast with error message appropriate to exception reason.
 *
 * @param context   current Activity context
 * @param exception BleScanException to show error message for
 */
public static void handleException(final Activity context, final BleScanException exception) {
    final String text;
    final int reason = exception.getReason();

    // Special case, as there might or might not be a retry date suggestion
    if (reason == BleScanException.UNDOCUMENTED_SCAN_THROTTLE) {
        text = getUndocumentedScanThrottleErrorMessage(context, exception.getRetryDateSuggestion());
    } else {
        // Handle all other possible errors
        final Integer resId = ERROR_MESSAGES.get(reason);
        if (resId != null) {
            text = context.getString(resId);
        } else {
            // unknown error - return default message
            Log.w("Scanning", String.format("No message found for reason=%d. Consider adding one.", reason));
            text = context.getString(R.string.error_unknown_error);
        }
    }

    Log.w("Scanning", text, exception);
    Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}