我正在使用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));
}
非常感谢。我做了所有这三项修改,现在我的代码已经到达了检查点1和2,但是它只检测一次信标。我希望它每五秒钟收到一次信标(距离)的位置。我怎样才能实现这个?再次感谢 - 我真的很感激它。 –
你能否请创建一个新的问题,并从这里链接到它?你遇到的问题已经发生了很大的变化,代码也基于你描述的变化。 – davidgyoung
http://stackoverflow.com/questions/35702632/ibeacon-ranging-service-not-returning-any-beacons-part-2谢谢! –