Estimote / Android-Fleet-Management-SDK

Estimote Fleet Management SDK for Android
https://developer.estimote.com
MIT License
836 stars 451 forks source link

setMonitoringListener not getting called. #105

Closed nikhilkashid closed 9 years ago

nikhilkashid commented 9 years ago

Hi,

I am creating an activity to monitor beacon entering a region, however setMonitoringListener is not getting triggered and i do not get any notification for the beacons entering the region. I am newbie to BLE so I am not sure if I am doing something wrong. Below is the snipped of code I am using. I have tried calling the MonitoringListener in onCreat but it still does not work

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.navigation_main);

    //Beacon monitoring

    coinRegion = new Region("mumble","EE4B5D02-1FD4-4B8D-BD71-97F4DABECCC2" , null, null);
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    beaconManager = new BeaconManager(this);
    beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(2), TimeUnit.SECONDS.toMillis(8));

}

@Override protected void onResume() { super.onResume();

    notificationManager.cancel(NOTIFICATION_ID);

    if (!beaconManager.hasBluetooth()) {
        Toast.makeText(this, "Device does not have Bluetooth Low Energy",
                Toast.LENGTH_LONG).show();
        return;
    }

    // If Bluetooth is not enabled, let user enable it.
    if (!beaconManager.isBluetoothEnabled()) {
        Intent enableBtIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    } else {
        connectToService();
    }
}

@Override
protected void onDestroy() {
    notificationManager.cancel(NOTIFICATION_ID);
    beaconManager.disconnect();
    super.onDestroy();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_ENABLE_BT) {
        if (resultCode == Activity.RESULT_OK) {
            connectToService();
        } else {
            Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_LONG)
                    .show();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

private void postNotification(String msg) {
    Intent notifyIntent = new Intent(NavigationMain.this, NavigationMain.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(NavigationMain.this,0,new Intent[]{notifyIntent},PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(NavigationMain.this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Coin Notification")
            .setContentText(msg)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notificationManager.notify(NOTIFICATION_ID, notification);

}

private void connectToService(){

    beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
        @Override
        public void onServiceReady() {
            try {
                beaconManager.startMonitoring(coinRegion);
                Log.d(TAG,"Start Monitoring");
            } catch (RemoteException e) {
                Log.d(TAG, "Error while starting monitoring");
            }
        }
    });

    beaconManager.setMonitoringListener(new MonitoringListener() {

        @Override
        public void onEnteredRegion(Region region, List<Beacon> beacons) {
            postNotification("Entered region");
            Log.d(TAG,"Monitoring Listener");
        }

        @Override
        public void onExitedRegion(Region region) {
            postNotification("Exited region");
        }
    });

}
wiktor commented 9 years ago

First. Have you tried paying with examples? Did they work for you?

nikhilkashid commented 9 years ago

Yes the Estimote demo works for me

wiktor commented 9 years ago

Please do follow example implementation.

On Fri, Mar 27, 2015 at 4:53 PM, nikhilkashid notifications@github.com wrote:

Reopened #105.

Reply to this email directly or view it on GitHub: https://github.com/Estimote/Android-SDK/issues/105#event-267004002

nikhilkashid commented 9 years ago

I have tried that but still I am not able to get the answer. Can you suggest what I maybe missing