I am using Alt beacon library for beacon scanning.I am scanning beacon in service class.i want to keep on scan the beacon even application closed also.but if close the application service is stoped and beacon is not scanning.I tried but i am not getting.please help me and thanks
//MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, ScaningService.class);
startService(intent);//starting service
getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);//bounding service
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
//Service class
public class ScaningService extends Service implements BeaconConsumer {
public BeaconManager beaconManager;
public MainActivity activity;
private final IBinder mBinder = new SimpleServiceBinder();
public class SimpleServiceBinder extends Binder {
public ScaningService getService() {
return ScaningService.this;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
beaconManager = BeaconManager.getInstanceForApplication(getBaseContext());
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.setBackgroundMode(true);
beaconManager.setBackgroundScanPeriod(1100);//this will set how long a bluetooth should scan
beaconManager.setBackgroundBetweenScanPeriod(2000);//this will set bluetooth scanning interval
beaconManager.bind(ScaningService.this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
beacon = beacons.iterator().next();
Log.e(TAG, " UUID: " + beacon.getId1());// this is not showing and i tried toast also
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("sBeacon", null, null, null));
} catch (RemoteException e) {
Log.i(TAG, "RemoteException: " + e);
}
}
}