ich eine kleine app gebaut, die die Android Beacon Bibliothek nutzt in der Nähe Baken zu finden, wenn der Benutzer nach diesem Tutorial die App startet: http://www.software7.com/blog/creating-a-beacon-app-for-android-in-less-than-10-minutes-from-scratch/Starten und Stoppen eines Beacon Scanner App mit Android Beacon Bibliothek
Wenn die App gestartet ist und findet Beacons gehört zu der bestimmten Region, es druckt Informationen in der Konsole und auch auf dem Text-Label in der Benutzeroberfläche. in meinen OnCreate und OnDestroy Methoden
Ich beginne und die Beacon-Manager zu schließen, die den Suchprozess verwaltet. Die App funktioniert gut, wenn ich sie starte erstmals. Aber wenn ich es mit meinem Pfeil zurück Knopf am Telefon schließe und es wieder öffnen, wird es keine Konsolenmeldungen noch Updates auf dem Textlabel sein. Dies passiert auch, wenn der Bildschirm in den Ruhezustand wechselt und ich ihn wieder entsperren muss - keine Konsolenaktualisierungen, keine UI-Aktualisierungen.
Nach den Konsolenmeldungen, die OnBeaconServiceConnect arbeitet die App auf die Wiederaufnahme, aber ich weiß nicht eine Log-Nachricht von didEnterRegion noch setRangeNotifier wieder bekommen.
Ich habe auch versucht, die Bindung und löst Onpause und OnResume, die auch nicht funktioniert. Wie kann ich die Suche wieder aufnehmen & Prozess wieder richtig finden? Vielen Dank für jede Hilfe :)
Hier ist mein kommentierten Code, falls Sie einen kurzen Blick haben wollen:
package de.mediatoni.beacontut01;
import android.graphics.Region;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import java.util.Collection;
public class BeaconActivity extends ActionBarActivity implements BeaconConsumer{
public static final String TAG = "BeaconsEverywhere";
// Beacon Manager Variable
private BeaconManager beaconManager;
//GUI Text Label
TextView rangeElement;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beacon);
// Vars for XML Elements
rangeElement = (TextView) findViewById(R.id.range);
// Instantiate a Beacon Manager via factory method
beaconManager = BeaconManager.getInstanceForApplication(this);
// Tell Library how to decode the signal
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
// Start the Beacon Manager
beaconManager.bind(this);
}
// When the Beacon Service starts, search for Beacons with the following Identifier
@Override
public void onBeaconServiceConnect() {
final org.altbeacon.beacon.Region region = new org.altbeacon.beacon.Region("myBeacons", Identifier.parse("73676723-7400-0000-ffff-0000ffff0005"), null, null);
beaconManager.setMonitorNotifier(new MonitorNotifier() {
// If the phone enters a Beacon region
@Override
public void didEnterRegion(org.altbeacon.beacon.Region region) {
try {
Log.d(TAG, "did Enter Region");
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
// If the phone leaves a Beacon region
@Override
public void didExitRegion(org.altbeacon.beacon.Region region) {
try {
Log.d(TAG, "did Exit Region");
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didDetermineStateForRegion(int i, org.altbeacon.beacon.Region region) {
}
});
// If the phone finds a Beacon fitting the rules, print it in the console
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
//Log out welche beacons in der Nähe sind
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
for(final Beacon oneBeacon : beacons) {
Log.d(TAG, "distance: " + oneBeacon.getDistance() + "id: " + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
// Access UI thread and make changes to the UI
// Placing rangeElement.setText outside of the Runnable will crash the App
runOnUiThread(new Runnable() {
@Override
public void run() {
// Change the text label in the UI
rangeElement.setText(String.valueOf(oneBeacon.getDistance()));
}
});
}
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
// When the App gets closed, stop the Beacon Manager
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_beacon, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Ehrfürchtig, das hat für mich funktioniert - großartig und danke! – olop01