1

我正在使用iBeacon技术,并且我正在尝试创建一个RangingService,它将每隔5秒搜索附近的iBeacons并在我的应用程序的后台运行。下面的代码不起作用。我确信我在某个地方犯了一些愚蠢的错误,但我可以在日志文件中看到Checkpoint 3和4每5秒钟到达一次,而检查点1和2永远不会到达。因此,附近的信标没有被检测到。我没有太多的服务或信标经验,所以我会很感激任何帮助,特别是@davidgyoung。iBeacon测距服务不返回任何信标

如果下面的代码没有完全缩进,请原谅我:)非常感谢任何能够帮助我的人。

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<Beacon> beacons, Region region) { 

      Log.d("Checkpoint1", "Checkpoint1"); 
      if (beacons.size() > 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 

主要活动:

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)); 

} 

回答

0

一些提示:

  1. 不要手动调用onBeaconServiceConnect();。这是一个回叫方法,当信标扫描服务随时可以通过拨打beaconManager.bind(this);时,由Android Beacon库调用。如果您自己调用它,则会导致其失败并导致问题。

  2. 您不应该每五秒钟拨打一次beaconManager.bind(this); - 只需在服务启动时调用一次即可。反复调用会导致问题。

  3. 确保Android信标库配置了您正在使用的信标类型。默认情况下,它只会检测开源的AltBeacon数据包。如果您使用的是专有信标类型,则需要创建一个BeaconParser并添加对库进行配置。您可以执行谷歌搜索“BeaconParser”和专有信标类型以获取适当的代码行来配置库。

+0

非常感谢。我做了所有这三项修改,现在我的代码已经到达了检查点1和2,但是它只检测一次信标。我希望它每五秒钟收到一次信标(距离)的位置。我怎样才能实现这个?再次感谢 - 我真的很感激它。 –

+0

你能否请创建一个新的问题,并从这里链接到它?你遇到的问题已经发生了很大的变化,代码也基于你描述的变化。 – davidgyoung

+0

http://stackoverflow.com/questions/35702632/ibeacon-ranging-service-not-returning-any-beacons-part-2谢谢! –