英文:
Eddystone beacons are not working when use Beacons-Android library
问题
我尝试使用beacon-android库(链接:https://github.com/adriancretu/beacons-android#features)在Android设备上实现Eddystone信标,代码如下所示:
public class MainActivity extends AppCompatActivity {
EddystoneURL beacon = new EddystoneURL("www.github.com");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Beacons.initialize(this);
beacon.init(5, AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY, AdvertiseSettings.ADVERTISE_TX_POWER_HIGH, 0, "Eddie");
beacon.start();
UUID uuid = beacon.getUUID();
String name = beacon.getName();
Log.i("Log", "name is " + name);
Log.i("Log", uuid.toString());
String url = beacon.getURL();
Log.i("Log", url);
Log.i("Log", "App started");
int k = beacon.getActiveState();
Log.i("Log", "active state : " + k);
int powerlvl = beacon.getTxPowerLevel();
Log.i("Log", "Power Lvl : " + powerlvl);
}
}
我得到的日志如下所示:
2020-04-09 20:27:42.771 14922-14922/? I/Log: name is Eddie
2020-04-09 20:27:42.772 14922-14922/? I/Log: 21ac707d-2ef0-4578-aa52-f8b8020d97c3
2020-04-09 20:27:42.772 14922-14922/? I/Log: www.github.com
2020-04-09 20:27:42.772 14922-14922/? I/Log: App started
2020-04-09 20:27:42.772 14922-14922/? I/Log: active state : 0
2020-04-09 20:27:42.772 14922-14922/? I/Log: Power Lvl : 3
问题在于模拟的信标无法被信标扫描器识别。非常感谢您的帮助。谢谢。
英文:
I tried to Implements an Eddystone beacon on android device using beacon-android library https://github.com/adriancretu/beacons-android#features
as follows.
public class MainActivity extends AppCompatActivity {
EddystoneURL beacon = new EddystoneURL("www.github.com");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Beacons.initialize(this);
beacon.init(5, AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY,AdvertiseSettings.ADVERTISE_TX_POWER_HIGH,0,"Eddie");
beacon.start();
UUID uuid = beacon.getUUID();
String name = beacon.getName();
Log.i("Log","name is "+name);
Log.i("Log",uuid.toString());
String url = beacon.getURL();
Log.i("Log",url);
Log.i("Log","App started");
int k = beacon.getActiveState();
Log.i("Log","active state : "+k);
int powerlvl = beacon.getTxPowerLevel();
Log.i("Log","Power Lvl : "+ powerlvl);
}
}
and I get logs as follows
2020-04-09 20:27:42.771 14922-14922/? I/Log: name is Eddie
2020-04-09 20:27:42.772 14922-14922/? I/Log: 21ac707d-2ef0-4578-aa52-f8b8020d97c3
2020-04-09 20:27:42.772 14922-14922/? I/Log: www.github.com
2020-04-09 20:27:42.772 14922-14922/? I/Log: App started
2020-04-09 20:27:42.772 14922-14922/? I/Log: active state : 0
2020-04-09 20:27:42.772 14922-14922/? I/Log: Power Lvl : 3
the problem is the emulated beacon is was not identified by beacon scanners. I really appreciate the help of yours. Thank you.
答案1
得分: 1
我意识到这个问题是关于使用beacons-android库构建发射器的,因此更好的答案是展示如何使用该库正确地使其工作。
然而,如果没有找到解决方案,而且提问者愿意使用Android Beacon Library(名称相似但完全不同的库)来实现这一目标,下面的代码将可以实现:
try {
byte[] urlBytes = UrlBeaconUrlCompressor.compress("https://www.github.com");
Identifier encodedUrlIdentifier = Identifier.fromBytes(urlBytes, 0, urlBytes.length, false);
ArrayList<Identifier> identifiers = new ArrayList<Identifier>();
identifiers.add(encodedUrlIdentifier);
beacon = new Beacon.Builder()
.setIdentifiers(identifiers)
.setTxPower(-59)
.build();
BeaconParser beaconParser = new BeaconParser()
.setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT);
BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(),
beaconParser);
beaconTransmitter.startAdvertising(beacon);
} catch (MalformedURLException e) {
Log.d(TAG, "That URL cannot be parsed");
}
全面披露:我是Android Beacon Library开源项目的首席开发者。
英文:
I realize this question is about building a transmitter with the beacons-android library, so a better answer would be to show how to make this work properly with that library.
However, if no solution is found and the OP is open to using the Android Beacon Library (similarly named but completely different) to accomplish this, the code below will do it:
try {
byte[] urlBytes = UrlBeaconUrlCompressor.compress("https://www.github.com"");
Identifier encodedUrlIdentifier = Identifier.fromBytes(urlBytes, 0, urlBytes.length, false);
ArrayList<Identifier> identifiers = new ArrayList<Identifier>();
identifiers.add(encodedUrlIdentifier);
beacon = new Beacon.Builder()
.setIdentifiers(identifiers)
.setTxPower(-59)
.build();
BeaconParser beaconParser = new BeaconParser()
.setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT);
BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(),
beaconParser);
beaconTransmitter.startAdvertising(beacon);
} catch (MalformedURLException e) {
Log.d(TAG, "That URL cannot be parsed");
}
Full Disclosure: I am the lead developer on the Android Beacon Library open source project.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论