I am trying to make an Android service that will find Estimote beacons and pop up notifications to the user. I downloaded the sample code and created a new myService Activity using this code. The code works, and I see in LogCat that Android is scanning beacons. Unfortunately, no beacons were found. What is wrong with my code?
Or maybe I'm doing it wrong?
package com.osos.service;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.BeaconManager.MonitoringListener;
import com.estimote.sdk.BeaconManager.RangingListener;
import com.estimote.sdk.Region;
import com.estimote.sdk.utils.L;
import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MyService extends Service{
private static final int NOTIFICATION_ID = 123;
private BeaconManager beaconManager;
Beacon beacon;
private NotificationManager myNotificationManager;
private NotificationManager notificationManager;
private static final String TAG = "MyService";
private static final Region ALL_ESTIMOTE_BEACONS_REGION = new Region("rid", null, null, null);
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
beaconManager = new BeaconManager(this);
beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);
Toast.makeText(this, "Jus paleidote OSOS serviza", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
L.enableDebugLogging(true);
Log.d(TAG, "onStartCOmmand");
try{
connectToService();
}catch(Exception e){
System.out.println("### problem ####");
System.out.println(e);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCOmmand");
beaconManager.setMonitoringListener(new MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
switch(beacon.getMinor()){
case 11111: displayNotificationOne(1,"This is OSOS");
break;
default: displayNotificationOne(1,"This is Sparta");
}
}
@Override
public void onExitedRegion(Region region) {
displayNotificationOne(2,"Exited OSOS");
}
});
return startId;
}
protected void displayNotificationOne(int i, String msg) {
Log.d(TAG, "Display notif");
Notification.Builder mBuilder = new Notification.Builder(this);
mBuilder.setContentTitle("Simplify");
mBuilder.setContentText("Jus turite nauju uzduociu");
mBuilder.setTicker("Simplify: Jus turite nauju uzduociu");
mBuilder.setSmallIcon(R.drawable.arrow_up_float).setAutoCancel(true).build();
long[] vibrate = {0,200,500 };
mBuilder.setVibrate(vibrate);
mBuilder.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_ONE_SHOT
);
mBuilder.setContentIntent(resultPendingIntent);
myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotificationManager.notify(1, mBuilder.build());
}
private void connectToService() {
Log.e(TAG, "Connect to service");
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
} catch (RemoteException e) {
Log.e(TAG, "Cannot start ranging", e);
}
}
});
}
@Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
beaconManager.disconnect();
}
}
source
share