3

我创建了一个 android 应用程序来使用蓝牙 LEscanner 扫描 BLE。现在我需要我的应用程序来识别信标是否属于 iBeacon 或 Eddystone。到目前为止,我通过解析AD帧成功确定了ibeacon的UUID、MajorId、MinorId。

4

3 回答 3

10

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

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

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

第二个布局表达式显示 Eddystone-UID 是一个服务广告,它的 16 位服务 UUID 为 0xfeaa,后跟匹配的字节码 0x00。它有两个标识符,第一个被称为“命名空间标识符”,来自字节偏移量 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 的所有细节,这些细节可以轻微改变扫描响应中的字节偏移量。您可以在此处查看其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
                }

            }
        }
    }

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

于 2015-09-26T17:25:42.220 回答
1

信标可以为 iBeacon 和Eddystone做广告。事实上,我参与的一个项目就是使用这样的信标。

如果使用nv-bluetooth库,很容易从数据包中提取 iBeacon 和 Eddystone。像这样:

// onLeScan() method of BluetoothAdapter.LeScanCallback interface.
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
{
    // Parse the payload of the advertisement packet.
    List<ADStructure> structures =
        ADPayloadParser.getInstance().parse(scanRecord);

    // For each AD structure contained in the advertising packet.
    for (ADStructure structure : structures)
    {
        // If the ADStructure instance can be cast to IBeacon.
        if (structure instanceof IBeacon)
        {
            // An iBeacon was found.
            IBeacon iBeacon = (IBeacon)structure;
            ......
        }
        // If the ADStructure instance can be cast to Eddystone.
        else if (structure instanceof Eddystone)
        {
            if (structure instanceof EddystoneUID)
            {
                // Eddystone UID
                EddystoneUID es = (EddystoneUID)structure;
                ......
            }
            else if (structure instanceof EddystoneURL)
            {
                // Eddystone URL
                EddystoneURL es = (EddystoneURL)structure;
                ......
            }
            else if (structure instanceof EddystoneTLM)
            {
                // Eddystone TLM
                EddystoneTLM es = (EddystoneTLM)structure;
                ......
            }
        }
        ......
    }

    ......
}

请注意,这android.bluetooth.le.ScanRecord是 Android 中最糟糕的 API 之一,因此最好手动解析数据包而不是使用ScanRecord.

于 2015-09-26T06:51:28.753 回答
0

请阅读有关ScanRecord https://developer.android.com/reference/android/bluetooth/le/ScanRecord.html的文档。该getManufacturerSpecificData()方法可能是您正在寻找的东西。

于 2015-09-26T05:06:15.487 回答