使用 Beacons-Android 库时,Eddystone 信标不工作
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
问题是模拟的信标未被信标扫描仪识别。我真的很感谢你的帮助。谢谢。
我意识到这个问题是关于使用 beacons-android 库构建发射器的,因此更好的答案是展示如何使它与该库一起正常工作。
但是,如果未找到解决方案并且 OP 愿意使用 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 开源项目的首席开发人员。
我尝试使用 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
问题是模拟的信标未被信标扫描仪识别。我真的很感谢你的帮助。谢谢。
我意识到这个问题是关于使用 beacons-android 库构建发射器的,因此更好的答案是展示如何使它与该库一起正常工作。
但是,如果未找到解决方案并且 OP 愿意使用 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 开源项目的首席开发人员。