Search code examples
androidserviceibeacon-androidbackground-service

How to implement BeaconConsumer with service in android


I am using BeaconConsumer with Activity and it working fine. While as I implement BeaconConsumer with service, it not working.

SimpleService extends Service implements BeaconConsumer {


protected static final String TAG = "MyBeaconService";
String UUID = "EXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
private BeaconManager mBeaconManager;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    mBeaconManager = BeaconManager.getInstanceForApplication(this);
    mBeaconManager.setDebug(true);
    mBeaconManager.bind(this);
    Log.e("", "onCreate");
}

@Override
public void onStart(Intent intent, int startId) {
    Log.i(TAG, "onStart called");
    mBeaconManager
            .getBeaconParsers()
            .add(new BeaconParser()
                    .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    mBeaconManager.bind(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStartCommand called");
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "Service destroyed ...");
    mBeaconManager.unbind(this);
}

@Override
public void onBeaconServiceConnect() {
    Log.e("", "onBeaconServiceConnect");
    mBeaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            try {
                Log.e(TAG, "didEnterRegion");
                mBeaconManager.startRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void didExitRegion(Region region) {
            try {
                Log.e(TAG, "didExitRegion");
                mBeaconManager.stopRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void didDetermineStateForRegion(int i, Region region) {

        }
    });

    mBeaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
                Region region) {
            for (Beacon oneBeacon : beacons) {

                Log.e("", "setRangeNotifier");
                Log.e("", "ID 1" + oneBeacon.getId1());
                Log.e("", "ID 2" + oneBeacon.getId2());
                Log.e("", "ID 3" + oneBeacon.getId3());
                Log.e(TAG, "distance: " + oneBeacon.getDistance());
                Log.e("",
                        "oneBeacon.getBluetoothAddress() :- "
                                + oneBeacon.getBluetoothAddress());

            }
        }
    });

    try {
        mBeaconManager.startMonitoringBeaconsInRegion(new Region(
                "myBeaons", Identifier.parse(UUID), null, null));
        mBeaconManager.startRangingBeaconsInRegion(new Region("myBeaons",
                Identifier.parse(UUID), null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }

}

This will not working. It did not gives details of beacon, instead of that its show this in logcat - Logcat Message

04-09 11:07:40.053: D/BeaconParser(8765): This is not a matching Beacon advertisement. (Was expecting be ac. The bytes I see are: 0201061aff4c000215e2c56db5dffb48d2b060d0f5a71096e000000000c5020a000816f0ff640000000011094d696e69426561636f6e5f30303933370000 04-09 11:07:40.648: D/BluetoothAdapter(8765): onScanResult() - Device=F2:75:FA:D7:6E:19 RSSI=-45

So, how may i use BeaconConsumer class with service so i get all details of beacon when app is close or not. Please help


Solution

  • Try adding the setBeaconLayout line in the onCreate method. Depending on how your start your service, the onStart method may not get called. The onCreate method always gets called.

    You may also wish to add some logging in these methods so you fully understand the startup sequence of your service during troubleshooting.