4

I am facing one issue here. I am trying to create > 1 Region for Beacon Ranging. but it's not working. It never get called even onServiceReady(...). When i do the same for single Region then it's fine. It's worked.

Code:

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.BeaconManager.MonitoringListener;
import com.estimote.sdk.Utils;

public class EstimoteManager {

private static final Logger logger = LoggerFactory.getLogger("c*.h*.b*.EstimoteMan*");
private static final int NOTIFICATION_ID = 123;
private static BeaconManager beaconManager;
private static NotificationManager notificationManager;
public static final String EXTRAS_BEACON = "extrasBeacon";
private static Context currentContext;
private static List<MyBeacon> _beaconslist;
private static List<UUID> ESTIMOTE_PROXIMITY_UUIDS;
private static List<Region> ALL_ESTIMOTE_BEACONS;
private static int count=0;

// Create everything we need to monitor the beacons
public static void Create(NotificationManager notificationMngr, Context context, final Intent i) {
    try {
        notificationManager = notificationMngr;
        currentContext = Happlication.getInstance().getContext();

        // Create a beacon manager
        beaconManager = new BeaconManager(currentContext);

        //get becons from DB (> 5)
       _beaconslist=Happlication.getInstance().getDBAdapter().getAllBeacons();

        for(MyBeacon myBeacon: _beaconslist){
            count++;
            logger.debug("myBeacon: " + myBeacon.getUUID().toUpperCase());
            ESTIMOTE_PROXIMITY_UUIDS.add(UUID.fromString(myBeacon.getUUID().toUpperCase()));
            ALL_ESTIMOTE_BEACONS.add(new Region("regionId"+String.valueOf(count),UUID.fromString(myBeacon.getUUID().toUpperCase()), Integer.parseInt(myBeacon.getMajor()), Integer.parseInt(myBeacon.getMinor())));
        }

        // We want the beacons heartbeat to be set at one second.
        beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(10), 0);

        beaconManager.setRangingListener(new BeaconManager.RangingListener() {

            @Override
            public void onBeaconsDiscovered(Region paramRegion, List<Beacon> paramList) {
                if (paramList != null && !paramList.isEmpty()) {
                    Beacon beacon = paramList.get(0);
                    Utils.Proximity proximity = Utils.computeProximity(beacon);
                    if (proximity == Utils.Proximity.IMMEDIATE) {
                        logger.debug("entered in region " + paramRegion.getProximityUUID());
                        postNotificationIntent("Estimote testing", "I have found an estimote !!!", i);
                    } else if (proximity == Utils.Proximity.FAR) {
                        logger.debug("exiting in region " + paramRegion.getProximityUUID());
                        postNotificationIntent("Estimote testing", "I have lost my estimote !!!", i);
                    }
                }
            }

        });

        // Connect to the beacon manager...
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                try {

                    logger.debug("onServiceReady connected");
                    for (Region region : ALL_ESTIMOTE_BEACONS) {
                        beaconManager.startRanging(region);
                    }

                } catch (Exception e) {
                }
            }
        });
    } catch (Exception e) {
    }
}

// Pops a notification in the task bar
public static void postNotificationIntent(String title, String msg, Intent i) {
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(
            currentContext, 0, new Intent[]{i},
            PendingIntent.FLAG_UPDATE_CURRENT);

    Bitmap largeIcon = BitmapFactory.decodeResource(currentContext.getResources(), R.drawable.ic_launcher);

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(currentContext)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(msg)
                    .setTicker(title)
                    .setLargeIcon(largeIcon)
                    .setWhen(System.currentTimeMillis())
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setAutoCancel(true)
                    .setContentText(msg);

    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

// Stop beacons monitoring, and closes the service
public static void stop() {
    try {

        for(Region region: ALL_ESTIMOTE_BEACONS){
            // ... and start the monitoring
            beaconManager.stopRanging(region);
        }

        beaconManager.disconnect();
    } catch (Exception e) {
    }
}

Any help would be appreciated. How do i work with > 1 region ? I am working with Estimote Beacons.

M D
  • 47,665
  • 9
  • 93
  • 114

0 Answers0