我想使用 AltBeacon 库来检测 Radius Networks 的 RadBeacon 标签。要了解该库的工作原理,我想使用 AltBeacon 的参考应用程序。
我在示例应用程序中实现了此代码。将 AltBeacon 库添加到项目中后,我可以运行该项目。
就像我之前提到的,我喜欢从 Radius Networks 检测 IBeacons RadBeacon 标签。我知道我需要使用 BeaconParser。我的问题是 RadBeacon 标记的 BeaconParser 的外观。这是 Estimote Beacon 的 IBeacon 解析器。
当我调试这个项目并查看 logcat 内部时,我可以看到以下消息:
"Beacon detected: id1: "UUID of RadBeacon" id2= "Major of RadBeacon" id3:"Minor of RadBeacon"".
"looking for ranging region mathes for this beacon"
"got record"
"This is not a matching Beacon advertisment."
我需要如何更改 BeaconParser?这是我的源代码
public class FindRadBeaconActivity extends ActionBarActivity implements BeaconConsumer{
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
protected static final String TAG = "RangingActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_rad_beacon);
// check bluetooth
verifyBluetooth();
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.debug = true;
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.find_rad_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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see an beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
} catch (RemoteException e) { }
}
private void verifyBluetooth() {
try {
if (!BeaconManager.getInstanceForApplication(this).checkAvailability()) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Bluetooth not enabled");
builder.setMessage("Please enable bluetooth in settings and restart this application.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
finish();
System.exit(0);
}
});
builder.show();
}
}
catch (RuntimeException e) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Bluetooth LE not available");
builder.setMessage("Sorry, this device does not support Bluetooth LE.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
finish();
System.exit(0);
}
});
builder.show();
}
}
}