I'm using RadiusNetworks Android iBeacon Library and I'd like to have my iBeacon ranging done in a background process, then it will broadcast beacon info to my application. I try to bind the iBeaconManager to the service running on the separate process but it doesn't seem to work as the service never makes it to the onIBeaconServiceConnect() callback.
TestService on separate process:
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>
I fetch the relevant proxID from my server, and send it to the service at which time I figured I ought to be able to bind to it and start ranging for regions that match the prox. But I can't seem to get it to bind to the service successfully. Thanks for help, and let me know if I need to update posted code.