Monitoring does not work unless the Bluetooth adapter state changes every time I run my app

I am trying my beacons using a demo code on Android. However, the monitoring doesn't work unless I change the status of the Bluetooth (switch it off then on again) . What to do to change that.
I tried to play with the following block of code, but it didn't work:
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTIONSTATECHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRASTATE,
switch (state) {
case BluetoothAdapter.STATE
// When bluetooth is turning off, lets stop estimotes ranging
if (estimoteServiceIntent != null) {
estimoteServiceIntent = null;