Question

I have mac and I've gotten macbeacon for it.

I have downloaded the free IBeacon for Android library and I am trying to write a basic activity that displays a toast once in range but it's not working...here is what I've done:

import android.os.Bundle;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.radiusnetworks.ibeacon.IBeaconConsumer;
import com.radiusnetworks.ibeacon.IBeaconManager;
import com.radiusnetworks.ibeacon.MonitorNotifier;
import com.radiusnetworks.ibeacon.Region;

public class iBeaconLocate extends ActionBarActivity implements IBeaconConsumer{

    protected static final String TAG = "RangingActivity";
    private IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_i_beacon_locate);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.i_beacon_locate, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onIBeaconServiceConnect() {
        iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {

                String test = region.getProximityUuid();

                Toast.makeText(getApplicationContext(), "Beacon: " + test + " found", Toast.LENGTH_LONG).show();
            }

            @Override
            public void didExitRegion(Region region) {
                Toast.makeText(getApplicationContext(), "Beacon: "+ region.getProximityUuid() +" lost", Toast.LENGTH_LONG).show();

            }

            @Override
            public void didDetermineStateForRegion(int state, Region region) {
                Log.i(TAG, "I have just switched from seeing/not seeing iBeacons: " + state);
            }
        });

        try {
            iBeaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
        } catch (RemoteException e) {   }
    }
}

if anyone can see anything wrong with his I'd appreciate input as I'm not sure if it's actually meant to be this simple?

I'm working within android studio and I've followed the quick startup guide I've configured it as instructed...please, any help is appreciated

Thanks in advance

Was it helpful?

Solution

The code is missing a call to bind to iBeaconManager. Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_i_beacon_locate);
    iBeaconManager.bind(this);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top