0

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);
    }
}

}

  • use background `Service` – Mohammad Ersan Nov 11 '16 at 10:39
  • The [library's documentation](https://altbeacon.github.io/android-beacon-library/documentation.html) also talks about background operation. – Markus Kauppinen Nov 11 '16 at 10:46
  • Please [edit] your question to show [what you have tried so far](http://whathaveyoutried.com). You should include a [mcve] of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Nov 11 '16 at 10:55

1 Answers1

1

Yes, this is possible. When properly configured, the Android Beacon Library will resume scanning in the background within five minutes of your app being killed. In the ranging callbacks, you may send data to your server with the results of the scans, although you probably need to do so on a new thread (using a Handler or similar construct.)

The examples on the project website above should get you much of the way there. You will want to combine background launching and ranging in a custom Application class.

EDIT: After seeing the code posted, it is clear that the issue is that the scanning is triggered from a custom service. While this can work, it is much harder to get it to work properly so scanning restarts on the app being killed. It is much simpler to follow the examples which use RegionBootstrap to trigger background detections using a custom Application class.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204