Vote count:
0
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 onCreate but it still does not work.
//Monitoring beacons
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");
}
});
}
asked 34 secs ago
onMonitoringListener not called Estimote SDK
Aucun commentaire:
Enregistrer un commentaire