1

我正在使用eddystone 信标android 信标库(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);
    }
}

突然不明白了……!有人可以在这方面帮助我吗?

4

1 回答 1

1

几点:

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

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

    beaconManager.getBeaconParsers().add(new BeaconParser()
        .setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
    
  • 如果您想匹配所有Eddystone 信标而不考虑标识符,以便您可以读取附近任何信标的标识符,请将您的区域设置为将所有标识符设置为 null,如下所示:

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

    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");                                 
        }
    });
    
于 2016-03-23T01:20:06.780 回答