package edu.ucla.iBeaconNav; import java.util.List; import java.util.LinkedList; import java.util.Collection; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; import android.os.IBinder; import android.os.Looper; import android.os.Messenger; import android.os.RemoteException; import android.preference.PreferenceManager; import com.radiusnetworks.ibeacon.*; import com.radiusnetworks.ibeacon.client.*; import com.radiusnetworks.ibeacon.service.*; public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier { /* Private data */ private Task task; private IBeaconManager ibeacon; /* Public methods */ public Beacon(Task task) { this.task = task; IBeaconManager.LOG_DEBUG = true; } public void connect() { Util.debug("Task: handle - connect"); this.task.notify("Connected", android.R.drawable.presence_online); this.ibeacon = IBeaconManager.getInstanceForApplication(this.task); this.ibeacon.bind(this); //this.ibeacon.setBackgroundMode(this, false); } public void disconnect() { Util.debug("Task: handle - register"); this.ibeacon.unBind(this); //this.ibeacon.setBackgroundMode(this, true); } /* IBeaconConsumer Methods * Pass most of these off to Task, * I don't even know why they're here */ @Override public boolean bindService(Intent intent, ServiceConnection connection, int mode) { return this.task.bindService(intent, connection, mode); } @Override public Context getApplicationContext() { return this.task.getApplicationContext(); } @Override public void unbindService(ServiceConnection connection) { this.task.unbindService(connection); } @Override public void onIBeaconServiceConnect() { Util.debug("Task: onIBeaconServiceConnect"); this.ibeacon.setRangeNotifier(this); this.ibeacon.setMonitorNotifier(this); try { Region region = new Region("iBeaconNavMonitoringId", null, null, null); this.ibeacon.startRangingBeaconsInRegion(region); this.ibeacon.startMonitoringBeaconsInRegion(region); } catch (RemoteException e) { Util.debug("Task: onIBeaconServiceConnect - error"); } } /* RangeNotifier Methods */ @Override public void didRangeBeaconsInRegion(Collection iBeacons, Region region) { Util.debug("Task: didRangeBeaconsInRegion"); } /* MonitorNotifier Methods */ @Override public void didEnterRegion(Region region) { Util.debug("Task: didEnterRegion"); } @Override public void didExitRegion(Region region) { Util.debug("Task: didExitRegion"); } @Override public void didDetermineStateForRegion(int state, Region region) { Util.debug("Task: didDetermineStateForRegion"); } }