title: iBeacon Ranging Service Not Returning any Beacons
body: <p>I am working with the iBeacon technology, and I am trying to create a RangingService that will search for nearby iBeacons every five seconds and run in the background of my application. The code below is not working. I'm sure I'm making some dumb mistake somewhere, but I can see in my log files that Checkpoint 3 and 4 are being reached every five seconds, while Checkpoints 1 and 2 are never being reached. Thus, the nearby beacons are not being detected. I don't have much experience with Services or Beacons, so I would appreciate any help, especially from @davidgyoung.</p>
  
  <p>Please forgive me if the code below isn't indented perfectly :) Thanks so much to anyone who can help me.</p>
  
  <pre><code>import android.app.Service;
  import android.content.Intent;
  import android.os.Handler;
  import android.os.IBinder;
  import android.os.Message;
  import android.os.RemoteException;
  import android.util.Log;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import org.altbeacon.beacon.Beacon;
  import org.altbeacon.beacon.BeaconConsumer;
  import org.altbeacon.beacon.BeaconManager;
  import org.altbeacon.beacon.RangeNotifier;
  import org.altbeacon.beacon.Region;
  
  import java.util.Collection;
  
  public class RangingService extends Service implements BeaconConsumer {
  private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
  Handler handler;
  String b = "";
  
  @Override
      public IBinder onBind(Intent arg0) {
          return null;
      }
  
  @Override
  public void onStart(Intent intent, int startId) {
      // Let it continue running until it is stopped.
      Log.d("Service", "Started");
  
      handler = new Handler(){
  
          @Override
              public void handleMessage(Message msg) {
                  // TODO Auto-generated method stub
                  super.handleMessage(msg);
                  Log.d("Checkpoint", "5 seconds have passed");
              }
  
          };
  
          new Thread(new Runnable(){
              public void run() {
                  // TODO Auto-generated method stub
                  while(true)
                  {
                      try {
                          startJob();
                          Log.d("Checkpoint", "Job has started");
                          Thread.sleep(5000);
                          handler.sendEmptyMessage(0);
  
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
  
                  }
  
              }
          }).start();
  
  
  
  
          //return START_STICKY;
      }
  
  public void startJob() {
      beaconManager.bind(this);
      Log.d("The first beacon", "Starting job for realzies");
      onBeaconServiceConnect();
  }
  
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.d("Service", "Ended");
      }
  
  @Override
  public void onBeaconServiceConnect() {
      Log.d("Checkpoint3", "Checkpoint3");
      beaconManager.setRangeNotifier(new RangeNotifier() {
          @Override
          public void didRangeBeaconsInRegion(Collection&lt;Beacon&gt; beacons, Region region) {
  
             Log.d("Checkpoint1", "Checkpoint1");
              if (beacons.size() &gt; 0) {
                  Log.d("Checkpoint2", "Checkpoint2");
                  //EditText editText = (EditText)RangingActivity.this.findViewById(R.id.rangingText);
                  Beacon firstBeacon = beacons.iterator().next();
                  String a = "The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away. RSSI = " + firstBeacon.getRssi();
                  Log.d("Service", a);
                  //logToDisplay(a);
              }
          }
  
      });
  
      try {
          beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
          Log.d("Checkpoint4", "Checkpoint4");
  
      } catch (RemoteException e) {   }
   }
  }// Update code formatting
  </code></pre>
  
  <p>Main Activity:</p>
  
  <pre><code>public void didEnterRegion(Region arg0) {
      // In this example, this class sends a notification to the user whenever   a Beacon
      // matching a Region (defined above) are first seen.
      Log.d(TAG, "did enter region.");
      startService(new Intent(this, RangingService.class));
  
  }
  </code></pre>