1
votes

i'm using eddystone beacon and android beacon library(AltLib), I'm a beginner user with these beacons; and for my project. i want to get the UUID of the beacon and to display it on a EditView or TextView. So i tried some code but no way!I obtained an endless execution... AS :

public class BeaconActivity extends ActionBarActivity implements BeaconConsumer {

    public static final String TAG = "BeaconsEverywhere";
    private BeaconManager beaconManager;

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

        beaconManager = BeaconManager.getInstanceForApplication(this);

        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"));

        beaconManager.bind(this);

    }

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

    @Override
    public void onBeaconServiceConnect() {
        final Region region = new Region("myBeaons", Identifier.parse("<replaceBySomeUIID>"), null, null);

        beaconManager.setMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                try {
                    Log.d(TAG, "didEnterRegion");
                    beaconManager.startRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void didExitRegion(Region region) {
                try {
                    Log.d(TAG, "didExitRegion");
                    beaconManager.stopRangingBeaconsInRegion(region);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void didDetermineStateForRegion(int i, Region region) {

            }
        });

        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                for(Beacon oneBeacon : beacons) {
                    Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
                }
            }
        });

        try {
            beaconManager.startMonitoringBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    @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);
    }
}

Suddenly I do not understand...! Can someone help me on this part?

1

1 Answers

1
votes

A few points:

  • Eddystone-UID beacons don't have UUIDs (16 byte universally unique identifiers) as identifiers. Instead, they have a 10 byte Namespace Id and a 6 byte Instance Id. With the Android Beacon Library, beacon.getId1() returns the 10 byte Namespace Id and beacon.getId2() returns the 6 byte instance Id.

  • To match Eddystone-UID beacons, you need to set up a beacon parser for that beacon type. Like this:

    beaconManager.getBeaconParsers().add(new BeaconParser()
        .setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
    
  • If you want to match all Eddystone beacons regardless of identifiers, so you can read the identifiers of any beacon nearby, set up your region with all identifiers set to null like this:

    final Region region = new Region("myBeacons", null, null, null);
    
  • Then when any Eddystone-UID beacon is detected by ranging, you can print out its identifier to the log with the code you have already:

    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            for(Beacon oneBeacon : beacons) {
                Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
            }
      }
    
  • If you want to add the detected identifiers to an EditView named myEditText, you need to make sure it is already part of your Activity's layout. If it is, you could do so like this:

    final String line = "Namespace id: " + oneBeacon.getId1() + ", Instance Id:" + oneBeacon.getId2());
    
    runOnUiThread(new Runnable() {
        public void run() {
            EditText editText = (EditText)BeaconActivity.this
                    .findViewById(R.id.myEditText);
            editText.append(line+"\n");                                 
        }
    });