setForegroundScanPeriod not working in marshmallow

I tried setting the value foreground scan period using beaconManager.setForegroundScanPeriod(1L, 0);
Its working fine on Android Kitkat but on Marshmallow it doesnt seem to affect the scan period. I’m using the latest sdk,

If a phone supports batch scanning (not all Marshmallow devices support this feature), it is automatically enabled and phone scans all the time (in low power mode). Hardware decides when to end scan cycle (using timeout period provided by the user or buffer is full) . User can set batch scan period using setForegroundScanPeriod, but I noticed this value is treated more like an advice rather than strict requirement.
Other problem might be that calling beaconManager.setForegroundScanPeriod(1L, 0); will actually set scan period to 1 milisecond. This might be not long enough to detect any packets and low value might be ignored by Android internals.

If a phone supports batch scanning (not all Marshmallow devices support this feature), it is automatically enabled and phone scans all the time (in low power mode). Hardware decides when to end scan cycle (using timeout period provided by the user or buffer is full) . User can set batch scan period using setForegroundScanPeriod, but I noticed this value is treated more like an advice rather than strict requirement.

So might be the reason why I have the same code (your android tutorial code) having onBeaconsDiscovered() called once per second on a Nexus 5 running Android 5.1 and called every 1.5 to 2 seconds on same device running Android 6.0.1?

Yes, this is the reason. Note that batch scanning is supported by particular phone model, not Android version. Nexus 5 does not support it in Lollipop and Marshmallow versions.
I tested batch mode on Nexus 6 and it seems it is not possible to have batch callback invoked more frequently than once in 1500ms.