我正在使用 RadiusNetworks Android iBeacon 库,我想在后台进程中完成 iBeacon 测距,然后它将信标信息广播到我的应用程序。我尝试将 iBeaconManager 绑定到在单独进程上运行的服务,但它似乎不起作用,因为该服务从未进入 onIBeaconServiceConnect() 回调。
单独进程上的 TestService:
public class TestService extends Service implements IBeaconConsumer{
public static final String START_SERVICE = "com.example.intent.action.START";
public static final String SEND_PROXID = "com.example.intent.action.PROX";
private String proxID;
private static final String TAG = TestService.class.getSimpleName();
private IBeaconManager iBeaconManager;
private Region currentRegion;
private boolean checkedRecently = false;
private Timer timer;
private TimerTask updateTask = new TimerTask() {
@Override
public void run() {
checkedRecently = false;
Log.i(TAG, "Resetting checked recently");
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId){
int ret;
ret = super.onStartCommand(intent, flags, startId);
Log.i(TAG, "Got to onStartCommand");
if(intent.getAction().equals(START_SERVICE)){
Log.i(TAG, "Service received start intent");
}
else if(intent.getAction().equals(SEND_PROXID)){
Log.i(TAG, "Service received PROXID intent");
proxID = intent.getStringExtra("prox");
System.out.println(proxID);
iBeaconManager = IBeaconManager.getInstanceForApplication(this);
iBeaconManager.bind(this);
}
return ret;
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBIND called");
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service creating");
timer = new Timer("TestTimer");
timer.schedule(updateTask, 1000L, 20 * 1000L);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroying");
timer.cancel();
timer = null;
iBeaconManager.unBind(this);
iBeaconManager = null;
}
@Override
public void onIBeaconServiceConnect() {
Log.i(TAG, "iBeacon service connect");
iBeaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
if (iBeacons.size() > 0 && !checkedRecently) {
checkedRecently = true;
Log.i(TAG, "Service found beacon, sending data");
Intent i = new Intent();
i.setAction(ServiceReceiver.DID_ENTER);
i.putExtra("maj", Integer.toString(iBeacons.iterator().next().getMajor()));
i.putExtra("min", Integer.toString(iBeacons.iterator().next().getMinor()));
sendBroadcast(i);
}
}
});
currentRegion = new Region("myRangingUniqueId", proxID, null, null);
try {
iBeaconManager.startMonitoringBeaconsInRegion(currentRegion);
} catch (RemoteException e) { }
}
}
AndroidManifest.xml
<service
android:name=".TestService"
android:exported="false"
android:process=":remote" >
<intent-filter>
<action android:name="com.example.intent.action.PROX" />
<action android:name="com.example.intent.action.START" />
</intent-filter>
</service>
<receiver android:name="ServiceReceiver" >
<intent-filter>
<action android:name="com.example.intent.action.DIDENTER" >
</action>
</intent-filter>
</receiver>
我从我的服务器获取相关的 proxID,并将其发送到服务,此时我认为我应该能够绑定到它并开始对与 prox 匹配的区域进行测距。但我似乎无法让它成功绑定到服务。感谢您的帮助,如果我需要更新发布的代码,请告诉我。