1

This is my first try to make an app with beacons. Im using the AltBeacon Library. All i want now is to be able to receive the UUID from a beacon. For this purpose i wanted to follow the Altbeacon example.

https://altbeacon.github.io/android-beacon-library/samples.html

So I did and this is my code.

public class MenuActivity extends AppCompatActivity implements View.OnClickListener, BeaconConsumer {

    protected final String TAG = "BeaconSearch";
    private BeaconManager beaconManager;
    private Region region = new Region("myUniqueRegion", null,null,null);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("m:2-3=02150215,i:4-19,i:20-21,i:22-23,p:24-24"));
        beaconManager.bind(this);


        Button scanButton = (Button) findViewById(R.id.scan_btn);
        scanButton.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.scan_btn:
                onBeaconServiceConnect();
                break;
        }
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        beaconManager.unbind(this);
    }

    @Override
    public void onBeaconServiceConnect() {
        Log.i(TAG,"1");
        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                Log.i(TAG, "2");
                if (beacons.size() > 0) {
                    Log.i(TAG, "Im Interested in this Beacon: " + beacons.iterator().next().getId1());
                }
            }
        });

        try {
            Log.i(TAG,"3");
            beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        } catch (RemoteException e) {
            Log.i(TAG,"4");
        }
    }
}

So my understanding is that this should give me the UUID from a beacon if one is nearby. But I don't understand where I have to call the onBeaconServiceConnect() Method. I tried it in the OnCreate() Method as well via Button Click but either way the app crashes.

I really want to know what I'm doing wrong and what I'm not understanding here. Im thankfull for any help!

MassaTime
  • 11
  • 1
  • 4

1 Answers1

0

A few tips:

  1. You don't call the onBeaconServiceConnect() method -- it gets called by the operating system automatically as a result of the call to bind()

  2. If you are not seeing this call automatically happen, make sure you have manifest merging enabled in your app (if using Eclipse). Otherwise you won't have the proper service declaration in your manifest. See here for more info.

  3. Try changing BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this); to beaconManager = BeaconManager.getInstanceForApplication(this); Otherwise you never initialize the class variable.

Community
  • 1
  • 1
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Ok wasn't aware of that thanks! I'm using android studio. Do I still have to change something in the Manifest.xml? – MassaTime May 24 '16 at 18:18
  • So it's still crashing after my Log.i(TAG; "1"), any Idea why? I have in my gradle dependencies this compile project(':android-beacon-library-2.8.1') instead of compile 'org.altbeacon:android-beacon-library:2+' which is told in the example. But if I change that the gradle build fails. – MassaTime May 24 '16 at 22:15
  • See my tip #3, just added. – davidgyoung May 24 '16 at 23:20
  • Thanks for that! It's not crashing anymore. But still not detecting my beacons... Do I need to ask for permission ass told here: http://altbeacon.github.io/android-beacon-library/requesting_permission.html ? But if I try to inlcude this it give me a bunch of non-expected token errors – MassaTime May 25 '16 at 05:35