Android iBeacon que recibe una sola señal especificada

I am receiving a range of signals from onReceive using BroadcastReceiver in my iBeaconProject. What I would like to do is to only keep track of one of the beacons (which I specify) and it's distance from my phone to the beacon. Any ideas, guys? Please help me! I'm using http://www.radiusnetworks.com. I am getting a range of signals using the following onReceive function. How do I go about doing it? Thanks all in advance!

BroadcastReceiver bReceiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        int countBea = 0; 
        if (intent.getAction().equals(intentname) && intent.getExtras() != null && intent.getExtras().containsKey(intentname)) {
            Collection<IBeacon> beaconsCol = (Collection<IBeacon>)intent.getExtras().getSerializable(intentname);

            for (IBeacon bea : beaconsCol) {

                    Log.d("beac receive!","receive! "+bea.getProximityUuid()+" "+bea.getMajor()+" "+bea.getMinor()+" "+bea.getAccuracy()+" "+bea.getProximity()+" "+bea.getRssi()+" "+bea.getTxPower());
                    countBea++;
                     if(((mainActivity)getActivity()).UUIDValue.equalsIgnoreCase(bea.getProximityUuid())
                            && ((mainActivity)getActivity()).MajorValue == bea.getMajor() 
                            && ((mainActivity)getActivity()).MinorValue == bea.getMinor()) {
                        update(bea.getProximityUuid(), +bea.getMajor(), bea.getMinor(), bea.getAccuracy());

                    } else if (((mainActivity)getActivity()).UUIDValue.equalsIgnoreCase(bea.getProximityUuid())
                            && (((mainActivity)getActivity()).MajorValue == 0 ||
                             ((mainActivity)getActivity()).MinorValue == 0)) {
                        updateNILMajorMinor();
                    } else {
                        updateMultipleBeaconsDetected();
                    }

            }
            System.out.println("COUNTBEAC " + countBea);

        }
    }
};

preguntado el 28 de mayo de 14 a las 11:05

2 Respuestas

Good to see the for-each loop.

Inside it, you can identify the beacon that you want to keep track of,

for (IBeacon bea : beaconsCol) {
    //in the following if, identify the specified beacon
    // this will remain the same for every refresh
    if(bea.getProximityUuid().equals("match it here") && bea.getMajor()==major 
        && bea.getMinor()==minor){
    //now display that beacon's proximity and accuracy
    //the same code will update a textview or notification every time

    // here you will have 1 beacon at a time, can add that to a global list
    }
}  

Can you give a precise idea for the implementation?

does your code enter onReceive periodically?

contestado el 28 de mayo de 14 a las 12:05

Hey! Thanks for your reply! I did try adding the if condition, but it keeps refreshing then receiving another batch of signals, then it doesn't keep track of that particular one that I specified. - Winona

hey..it will if you just match it with one, the beacon that you want to identify :) can you check the details of the beacon that you want to identify.. major, minor, uuid - Pararth

yes i did, but then my code checks for extra beacons detected, then it will display that multiple beacons are detected. - Winona

i am using fragments, so the onReceive is at the fragment interface cause the beacons signal received will be shared between different fragments. - Winona

you can use a global variable(or list for more beacons) then, which will be shared between fragments but will have data of only the beacon that you want - Pararth

I have never seen anything mention using the Radius Networks SDK by listening for broadcasts. Instead they ask that you implement certain interfaces and register them with an IBeaconManager.

Puede encontrar su muestras de código useful. That page contains the following snippet, which you may recognize as equivalent to the code in your question.

public class RangingActivity extends Activity implements IBeaconConsumer, RangeNotifier {

    private static final String TAG = RangingActivity.class.getName();
    private IBeaconManager iBeaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iBeaconManager = IBeaconManager.getInstanceForApplication(this);
        iBeaconManager.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iBeaconManager.unBind(this);
    }

    @Override
    public void onIBeaconServiceConnect() {
        iBeaconManager.setRangeNotifier(this);

        try {
            // edit this to match the UUID of your beacon
            // or leave null to detect everything
            String uuid = null;

            Region region = new Region("myRangingUniqueId", uuid, null, null);
            iBeaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            Log.e(TAG, "problem while starting ranging", e);
        }
    }

    @Override 
    public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
        if (!iBeacons.isEmpty()) {
            double accuracy = iBeacons.iterator().next().getAccuracy();
            Log.i(TAG, "The first iBeacon I see is about " + accuracy + " meters away.");
        }
    }

}

contestado el 28 de mayo de 14 a las 11:05

I'll try this code in a moment. Cause if I use the code you've provided, I need to change a whole lot of codes though. Thanks for your help! - Winona

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.