编程,如何识别,如果一盏明灯属于埃迪斯通或iBeacon显示? [英] Programmatically, How to identify if a beacon belongs to Eddystone or iBeacon?

查看:419
本文介绍了编程,如何识别,如果一盏明灯属于埃迪斯通或iBeacon显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Android应用程序扫描使用蓝牙LEscanner BLE。现在,我需要我的应用程序,以确定是否灯塔属于iBeacon显示或埃迪斯通。到目前为止,我成功的在确定UUID,MajorId,iBeacon显示的MinorId通过解析AD框架。

I have created an android application to scan for BLE using Bluetooth LEscanner. Now that I need my app to identify if a beacon belongs to iBeacon or Eddystone. So far, I'm successful in determining UUID,MajorId,MinorId of ibeacon by parsing the AD frame.

推荐答案

这是比较容易阅读广告的字节,如果你知道所有字段的字节偏移。下面两个code代码展示了你如何分析这些了。首先说明如何在做到这一点你自己的 onLeScan 使用的 Android的灯塔图书馆,第二个显示如何从头开始推出自己的。

It's relatively easy to read the bytes of the advertisements if you know the byte offsets of all the fields. Two code snippets below show you how you can parse these out. The first shows how you can do this in your own onLeScan callback using the Android Beacon Library, and the second shows how you can roll your own from scratch.

要解释的布局是如何工作的,看看下面的code。它使用它处理所有的解析为一个可配置的布局Android的灯塔Libray的 BeaconParser 类。 (即使你想如在第二code段推出自己的,这是值得期待的布局前pressions让你知道他们是如何工作的。这位前$ P $以下pssions显示细节显示AltBeacon这是非常相似的iBeacon显示。AltBeacon因为有与讨论它的实现没有知识产权的限制,两者AltBeacon和埃迪斯通是开源的标准。)

To explain how the layouts work, look at the code below. It uses the Android Beacon Libray's BeaconParser class which handles all of the parsing for a configurable layout. (Even if you want to roll your own as shown in the second code snippet, it is worthwhile looking at the layout expressions so you know how they work. The expressions below show the details for AltBeacon which is very similar to iBeacon. AltBeacon is shown because there are no intellectual property restrictions with discussing its implementation. Both AltBeacon and Eddystone are open source standards.)

第一布局前pression表明AltBeacon(再次非常类似于iBeacon显示)具有三个标识符(i的前pressions)。第一个(称为UUID在iBeacon显示)是从字节去16字节偏移4-19。第二个(被称为主要的iBeacon显示)是从字节变为2字节偏移20-21。第三个(被称为轻微的iBeacon显示)是从字节变为2字节偏移22-23。

The first layout expression shows that AltBeacon (again very similar to iBeacon) has three identifiers ("i" expressions). The first one (known as UUID in iBeacon) is 16 bytes that goes from byte offset 4-19. The second one (known as major on iBeacon) is 2 bytes that goes from byte offset 20-21. The third one (known as minor on iBeacon) is 2 bytes that goes from byte offset 22-23.

第二布局前pression表明的Eddystone-UID是具有0xfeaa的一个16位服务的UUID随后是为0x00的匹配字节code进行服务广告。它有两个标识符,所述第一被称为从字节偏移4-13命名空间标识符。第二标识符被称为从字节偏移14-19的实例标识符

The second layout expression shows that Eddystone-UID is a service advertisement that has a 16-bit service UUID of 0xfeaa which is followed by a matching byte code of 0x00. It has two identifiers, the first known as the "namespace identifier" from byte offsets 4-13. The second identifier is known as the "instance identifier" from byte offsets 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();
                }
            }
        }

开源的Andr​​oid灯塔库处理所有面向大家介绍可变长度的PDU的详细信息,可以稍微改变扫描响应中的字节偏移。你可以看到它是如何<一个源$ C ​​$ C href=\"https://github.com/AltBeacon/android-beacon-library/blob/master/src/main/java/org/altbeacon/beacon/BeaconParser.java\"相对=nofollow> BeaconParser 在这里工作。

如果你想完全从头开始推出自己的,最简单的方法是简单地遍历找你要查找的模式的字节数,然后解析出根据偏移量字节的兴趣。 (Android的灯塔图书馆使用的居然解析个人的PDU一个更强大和复杂的方法。)但循环技术仍然有效。

If you want to roll your own completely from scratch, the easiest way is to simply loop through the bytes looking for the pattern you want to find, then parse out the bytes of interest based on the offsets. (The Android Beacon Library uses a more robust and sophisticated method of actually parsing individual PDUs.) But the looping technique still works.

   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
                }

            }
        }
    }

同样,code以上说明如何解析开源AltBeacons(知识产权原因)。要解析iBeacons,你需要为它做BeaconLayout谷歌搜索,并进行小的调整上述code。

Again, the code above shows how to parse open source AltBeacons (for intellectual property reasons). To parse iBeacons, you'll need to do a Google search for its BeaconLayout, and make small adjustments to the code above.

这篇关于编程,如何识别,如果一盏明灯属于埃迪斯通或iBeacon显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆