gpt4 book ai didi

android - 以编程方式,如何识别信标是属于 Eddystone 还是 iBeacon?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:14 24 4
gpt4 key购买 nike

我已经创建了一个安卓应用程序来使用蓝牙 LEscanner 扫描 BLE。现在我需要我的应用程序来识别信标是属于 iBeacon 还是属于 Eddystone。至此,我通过解析AD帧成功确定了ibeacon的UUID、MajorId、MinorId。

最佳答案

如果您知道所有字段的字节偏移量,那么读取广告的字节就相对容易了。下面的两个代码 fragment 向您展示了如何解析它们。第一个显示了如何使用 Android Beacon Library 在您自己的 onLeScan 回调中执行此操作。 ,第二个展示了如何从头开始自己制作。

要解释布局的工作原理,请查看下面的代码。它使用 Android Beacon Libray 的 BeaconParser 类处理可配置布局的所有解析。 (即使你想像第二个代码 fragment 中所示那样滚动你自己的,也值得看看布局表达式,这样你就知道它们是如何工作的。下面的表达式显示了与 iBeacon 非常相似的 AltBeacon 的详细信息。显示了 AltBeacon因为讨论它的实现没有知识产权限制。AltBeacon 和 Eddystone 都是开源标准。)

第一个布局表达式显示 AltBeacon(同样与 iBeacon 非常相似)具有三个标识符(“i”表达式)。第一个(在 iBeacon 中称为 UUID)是 16 个字节,从字节偏移量 4-19 开始。第二个(在 iBeacon 上称为 major)是从字节偏移量 20-21 开始的 2 个字节。第三个(在 iBeacon 上称为次要)是从字节偏移量 22-23 开始的 2 个字节。

第二个布局表达式显示 Eddystone-UID 是一个服务广告,其 16 位服务 UUID 为 0xfeaa,后跟匹配的字节代码 0x00。它有两个标识符,第一个称为“ namespace 标识符”,来自字节偏移量 4-13。第二个标识符被称为字节偏移量 14-19 中的“实例标识符”。

    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

ArrayList<BeaconParser> beaconParsers = new ArrayList<BeaconParser>();
final String ALTBEACON_LAYOUT = "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";
final String EDDYSTONE_UID_LAYOUT = "s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19";

beaconParsers.add(new BeaconParser().setBeaconLayout(EDDYSTONE_UID_LAYOUT));
beaconParsers.add(new BeaconParser().setBeaconLayout(ALTBEACON_LAYOUT));

Beacon beacon = null;
for (BeaconParser parser : beaconParsers) {
beacon = parser.fromScanData(scanRecord,
rssi, device);

if (beacon != null) {
if (beacon.getServiceUuid() == 0xfeaa) {
// This is Eddystone, which uses a service Uuid of 0xfeaa
Identifier eddystoneNamespaceId = beacon.getId1();
Identifier eddystoneInstanceId = beacon.getId2();
}
else {
// This is another type of beacon like AltBeacon or iBeacon
Identifier uuid = beacon.getId1();
Identifier major = beacon.getId2();
Identifier minor = beacon.getId3();
}
}
}

开源 Android Beacon 库为您处理有关可变长度 PDU 的所有细节,这些 PDU 可以稍微改变扫描响应中的字节偏移量。你可以看到它的源代码 BeaconParser在这里工作。

如果您想完全从头开始,最简单的方法是简单地遍历字节以查找您想要找到的模式,然后根据偏移量解析出感兴趣的字节。 (Android Beacon 库使用更强大和更复杂的方法来实际解析单个 PDU。)但是循环技术仍然有效。

   public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

for (int startByte = 0; startByte < scanRecord.length; startByte++) {
if (scanRecord.length-startByte > 19) { // need at least 19 bytes for Eddystone-UID
// Check that this has the right pattern needed for this to be Eddystone-UID
if (scanRecord[startByte+0] == (byte)0xaa && scanRecord[startByte+1] == (byte)0xfe &&
scanRecord[startByte+2] == (byte)0x00) {
// This is an Eddystone-UID beacon.
byte[] namespaceIdentifierBytes = Arrays.copyOfRange(scanRecord, startByte+4, startByte+13);
byte[] instanceIdentifierBytes = Arrays.copyOfRange(scanRecord, startByte+14, startByte+19);
// TODO: do something with the above identifiers here
}
}
if (scanRecord.length-startByte > 24) { // need at least 24 bytes for AltBeacon
// Check that this has the right pattern needed for this to be AltBeacon
// iBeacon has a slightly different layout. Do a Google search to find it.
if (scanRecord[startByte+2] == (byte)0xbe && scanRecord[startByte+3] == (byte)0xac) {
// This is an AltBeacon
byte[] uuidBytes = Arrays.copyOfRange(scanRecord, startByte+4, startByte+19);
byte[] majorBytes = Arrays.copyOfRange(scanRecord, startByte+20, startByte+21);
byte[] minorBytes = Arrays.copyOfRange(scanRecord, startByte+22, startByte+23);
// TODO: do something with the above identifiers here
}

}
}
}

同样,上面的代码展示了如何解析开源 AltBeacon(出于知识产权原因)。要解析 iBeacon,您需要在 Google 上搜索其 BeaconLayout,并对上面的代码进行小幅调整。

关于android - 以编程方式,如何识别信标是属于 Eddystone 还是 iBeacon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com