使用android信标库获取eddystone信标的UUID

时间:2016-03-22 21:45:42

标签: android ibeacon-android altbeacon beacon eddystone

我正在使用 eddystone beacon android beacon library(AltLib),我是这些信标的初学者用户;对于我的项目。 我想获得信标的UUID并在EditView或TextView上显示它。 所以我尝试了一些代码,但没办法!我获得了无休止的执行...... 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);
    }
}

突然间我不明白......!有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

几点:

  • Eddystone-UID信标不具有UUID(16字节通用唯一标识符)作为标识符。相反,它们有一个10字节的命名空间标识和一个6字节的实例标识。使用Android Beacon Library,beacon.getId1()返回10字节的命名空间ID,beacon.getId2()返回6字节的实例ID。

  • 要匹配Eddystone-UID信标,您需要为该信标类型设置信标解析器。像这样:

    beaconManager.getBeaconParsers().add(new BeaconParser()
        .setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
    
  • 如果您想匹配所有 Eddystone信标而不管标识符,那么您可以读取附近任何信标的标识符,设置您所在区域的所有标识符设置为null,如下所示:< / p>

    final Region region = new Region("myBeacons", null, null, null);
    
  • 然后,当通过测距检测到任何Eddystone-UID信标时,您可以使用您已经拥有的代码将其标识符打印到日志中:

    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());
            }
      }
    
  • 如果您要将检测到的标识符添加到名为EditView的{​​{1}},则需要确保它已经是您的活动布局的一部分。如果是,你可以这样做:

    myEditText