I wanted to get the notification if just the bluetooth is on .Is that possible in android using some background service.
Hi @Vamc,
the monitoring technology is made for you ^^
It notifies you when you enter or exit a region you defined.
It works on background.
It needs the Bluetooth and the Location permissions (BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_COARSE_LOCATION).
See:
// Instanciate the BeaconManager class.
BeaconManager manager = new BeaconManager(this);
// Instanciate the region you want to monitor.
BeaconRegion region = new BeaconRegion(/* Your region */);
// Set a listener to handle the events.
manager.setMonitoringListener(new BeaconManager.BeaconMonitoringLitener {
@Override
public void onEnteredRegion(BeaconRegion region, List<Beacon> beacons) {
// Handle here the event when you enter the region.
}
@Override
public void onExitedRegion(BeaconRegion region) {
// Handle here the event when you exit the region.
}
});
// Connect to the service (mandatory) before monitoring.
manager.connect(new BeaconService.OnServiceReadyCallback {
@Override
public void onServiceReady() {
manager.startMonitoring(region);
}
});
/* After the monitoring. */
// Stop the monitoring.
manager.stopMonitoring(region.getIdentifier());
// Disconnect from service.
manager.disconnect();
THIS IS MY CODE FOR MONITORING PART…
I AM GETTING NOTIFICATION ONLY WHEN THE APPLICATION IS ACTIVE AND IN FOREGROUND…WHEN THE APP IS NOT OPEN i AM NOT GETTING ANY NOTIFICATION EVEN IF I AM IN THE RANGE OF BEACON…
Can someone help me out with this thing??
package com.example.airport;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import java.util.List;
import java.util.UUID;
public class MyApplication extends Application {
private BeaconManager beaconManager;
@Override
public void onCreate() {
super.onCreate();
System.out.println(“i am here”);
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> list) {
showNotification(
"Your gate closes in 47 minutes.",
"Current security wait time is 15 minutes, "
+ "and it's a 5 minute walk from security to the gate. "
+ "Looks like you've got plenty of time!");
}
@Override
public void onExitedRegion(Region region) {
// could add an "exit" notification too if you want (-:
}
});
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startMonitoring(new Region("monitored region",
UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), 3303, 12553));
}
});
}
public void showNotification(String title, String message) {
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0,
new Intent[]{notifyIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
}
Sorry, but I don’t master the notification and Android background app’ features… But your Estimote code seems to be correct to me.