我正在使用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謝謝! –