I’m trying to add to this code an event that notificates when a beacon is exited from range but I don’t recive any feedback.
Any suggest?
public class ProximityBeacon extends Service implements BeaconEventThrower, BeaconManager.BeaconRangingListener,
{
private String server;
private String ID;
private BeaconManager beaconManager;
private String scanId;
private boolean toDo;
private boolean toDoUscita;
private ProximityContentManager proximityContentManager;
private ArrayList<BeaconEventListener> beaconEventListener=new ArrayList<BeaconEventListener>();
public ProximityBeacon()
{
super();
}
/**
*
* @param server Indirizzo del server a cui fare l'upload della telemetria
*/
public void setServer(String server)
{
this.server=server;
}
/**
* Avvia la ricerca del beacon
* @param ID ID del beacon
* @param intent codice da aggiungere: new Intent(this, ProximityBeacon.class)
* @param context getApplicationContext()
* @throws BluetoothUnsupportedException
* @throws BluetoothDisabledException
*/
public void startBeaconDetecting(String ID, BeaconEventListener b, Intent intent, Context context) throws BluetoothDisabledException, BluetoothUnsupportedException
{
this.ID=ID;
toDo=true;
scanId=ID;
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
if(bta==null)
throw new BluetoothUnsupportedException();
if(bta.isEnabled()==false)
throw new BluetoothDisabledException();
intent.putExtra("ID", ID);
intent.putExtra("listener", b);
context.startService(intent);
}
/**
* Interrompe la ricerca del beacon
*/
public void stopBeaconDetecting()
{
stopSelf();
onDestroy();
}
public int onStartCommand(final Intent intent, int flag, int startId)
{
ID=intent.getStringExtra("ID");
beaconEventListener.add((BeaconEventListener)intent.getSerializableExtra("listener"));
beaconManager = new BeaconManager(this);
beaconManager.connect(new BeaconManager.ServiceReadyCallback()
{
public void onServiceReady()
{
beaconManager.startTelemetryDiscovery();
}
});
toDo=true;
toDoUscita=false;
beaconManager.setTelemetryListener(new BeaconManager.TelemetryListener()
{
@Override
public void onTelemetriesFound(List<EstimoteTelemetry> telemetries)
{
for (EstimoteTelemetry tlm : telemetries)
{
if (toDo && ID.equals((tlm.toString()).substring(29,61)))
{
String text = tlm.toString();
fireBeaconEventFound(new BeaconEvent(this));
Log.i(TAG,text);
if(text!="")
{
toDo = false;
toDoUscita=true;
FileOutputStream fos;
try
{
fos =openFileOutput("Telemetria.txt",Context.MODE_WORLD_READABLE);
DataOutputStream dos = new DataOutputStream(fos);
dos.write(text.getBytes());
dos.close();
//getFilesDir().getAbsolutePath(); Indirizzo file TODO spedizione al server
} catch (Exception e)
{
}
}
}
}
//TODO uscita
}
});
proximityContentManager = new ProximityContentManager(this,
Arrays.asList(ID), new EstimoteCloudBeaconDetailsFactory());
proximityContentManager.setListener(new ProximityContentManager.Listener()
{
@Override
public void onContentChanged(Object content)
{
}
});
return Service.START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
proximityContentManager.destroy();
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void addBeaconEventListener(BeaconEventListener listener)
{
beaconEventListener.add(listener);
}
@Override
public void fireBeaconEventFound(BeaconEvent e)
{
int n=beaconEventListener.size();
for(int i=0; i<n; i++)
{
(beaconEventListener.get(i)).beaconFound(e);
}
}
@Override
public void fireBeaconEventReleased(BeaconEvent e)
{
int n = beaconEventListener.size();
for (int i = 0; i < n; i++) {
(beaconEventListener.get(i)).beaconReleased(e);
}
}
}