我正在使用eddystone信标和android信标库(AltLib),我是这些信标的初学者用户;以及我的项目。我想获取信标的UUID,并将其显示在编辑视图或文本视图上。所以我尝试了一些代码,但没有办法!我获得了无尽的处决...如:
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);
}
}
突然间我不明白了...!有人可以在这部分帮助我吗?
几点:
-
Eddystone-UID 信标没有 UUID(16 字节通用唯一标识符)作为标识符。 相反,它们具有 10 字节命名空间 ID 和 6 字节实例 ID。 使用 Android Beacon 库,
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()); } }
-
如果要将检测到的标识符添加到名为
myEditText
的EditView
,则需要确保它已是活动布局的一部分。 如果是,您可以像这样这样做: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"); } });