我正在使用iBeacon技术,我正在尝试创建一个RangingService,它将每隔五秒钟搜索附近的iBeacons并在我的应用程序的后台运行。以下代码无效。我确定我在某个地方犯了一些愚蠢的错误,但我可以在我的日志文件中看到每5秒就会检查一次Checkpoint 3和4,而从未到达Checkpoints 1和2。因此,没有检测到附近的信标。我对服务或信标没有太多经验,所以我很感激任何帮助,特别是来自@davidgyoung。
如果下面的代码没有完美缩进,请原谅我:)非常感谢能帮助我的人。
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 :(得分:0)
一些提示:
请勿手动拨打onBeaconServiceConnect();
。这是一个回调方法,当信标扫描服务准备好通过调用beaconManager.bind(this);
时,由Android Beacon Library调用。如果你自己调用它,你就会失败它的目的,并会引起问题。
您不应该每隔五秒钟调用beaconManager.bind(this);
- 只需在服务启动时调用一次。反复调用会导致问题。
确保为您正在使用的信标类型配置Android Beacon Library。默认情况下,它只会检测开源的AltBeacon数据包。如果您使用的是专有信标类型,则需要创建BeaconParser
并添加配置库。您可以在Google上搜索“BeaconParser”和您的专有信标类型,以获取正确的代码行来配置库。