2

我希望在不使用 Proximity Beacon API 或 Nearby Messages API 的情况下检测 Eddystone Ul 和 uid。我希望使用本地 android 库,如 BluetoothAdapter 或 BluetoothGatt 或 BluetoothGap 来解析 eddystone 帧。这可行吗?如果是这样,如何以及如果它不可行,那么还有什么替代方案?

4

3 回答 3

4

以下是获取有关 Eddystone AFAIK 信息的最简单方法。

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

    // For each AD structure contained in the advertisement packet.
    for (ADStructure structure : structures)
    {
        // If the AD structure represents Eddystone UID.
        if (structure instanceof EddystoneUID)
        {
            // Eddystone UID
            EddystoneUID es = (EddystoneUID)structure;

            Log.d(TAG, "Tx Power = "     + es.getTxPower());
            Log.d(TAG, "Namespace ID = " + es.getNamespaceIdAsString());
            Log.d(TAG, "Instance ID = "  + es.getInstanceIdAsString());
            Log.d(TAG, "Beacon ID = "    + es.getBeaconIdAsString());

            // As byte arrays if you want.
            byte[] namespaceId = es.getNamespaceId();
            byte[] instanceId  = es.getInstanceId();
            byte[] beaconId    = es.getBeaconId();
        }
        // If the AD structure represents Eddystone URL.
        else if (structure instanceof EddystoneURL)
        {
            // Eddystone URL
            EddystoneURL es = (EddystoneURL)structure;

            Log.d(TAG, "Tx Power = " + es.getTxPower());
            Log.d(TAG, "URL = "      + es.getURL());
        }
        // If the AD structure represents Eddystone TLM.
        else if (structure instanceof EddystoneTLM)
        {
            // Eddystone TLM
            EddystoneTLM es = (EddystoneTLM)structure;

            Log.d(TAG, "TLM Version = "         + es.getTLMVersion());
            Log.d(TAG, "Battery Voltage = "     + es.getBatteryVoltage());
            Log.d(TAG, "Beacon Temperature = "  + es.getBeaconTemperature());
            Log.d(TAG, "Advertisement Count = " + es.getAdvertisementCount());
            Log.d(TAG, "Elapsed Time = "        + es.getElapsedTime());
        }
    }
}

如果您使用nv-bluetooth,则无需了解有关Eddystone 规范的详细信息。

摇篮

dependencies {
    compile 'com.neovisionaries:nv-bluetooth:1.7'
}

JavaDoc

JavaDoc

备忘录

Eddystone TLM中的信标温度以有符号定点表示法表示在撰写本文时,如何检测 Eddystone 兼容信标(Android 信标库)中的代码示例并未显示如何将数据提取为浮点数。另一方面,EddystoneTLMnv-bluetooth 中的类具有如下所示的方法,因此您不必解码定点符号。

public float getBeaconTemperature();

同样,EddystoneURL类有一个方法可以将 URL 获取为URL.

public URL getURL();

因此,当您使用 Android 信标库时,您不必执行以下步骤。

String url = UrlBeaconUrlCompressor.uncompress(beacon.getId1().toByteArray());

nv-bluetooth 将与 Eddystone 相关的数据结构实现为继承树,如下所示。这种合适的继承树在其他库中是很难找到的。

ADStructure
  |
  +-- ServiceData
        |
        +-- Eddystone
              |
              +-- EddystoneUID
              |
              +-- EddystoneURL
              |
              +-- EddystoneTLM

正确继承树的好处之一是方法被放置在正确的位置。像这样:

ADStructure
  |  // AD Structure Length - 1
  |  int getLength();
  |
  |  // AD Type
  |  int getType();
  |
  |  // AD Data
  |  byte[] getData();
  |
  +-- ServiceData
  |     |  // Service UUID
  |     |  UUID getServiceUUID();
  |     |
  |     +-- Eddystone
  |           |  // Eddystone Frame Type
  |           |  FrameType getFrameType();
  |           |
  |           +-- EddystoneUID
  |           |     // Tx Power
  |           |     int getTxPower();
  |           |
  |           |     // Namespace ID (byte[])
  |           |     byte[] getNamespaceId();
  |           |
  |           |     // Instance ID (byte[])
  |           |     byte[] getInstanceId();
  |           |
  |           |     // Beacon ID (byte[])
  |           |     byte[] getBeaconId();
  |           |
  |           |     // Namespace ID (String)
  |           |     String getNamespaceIdAsString();
  |           |
  |           |     // Instance ID (String)
  |           |     String getInstanceIdAsString();
  |           |
  |           |     // Beacon ID (String)
  |           |     String getBeaconIdAsString();
  |           |
  |           +-- EddystoneURL
  |           |     // Tx Power
  |           |     int getTxPower();
  |           |
  |           |     // URL
  |           |     URL getURL();
  |           |
  |           +-- EddystoneTLM
  |                 // TLM Version
  |                 int getTLMVersion();
  |
  |                 // Battery Voltage
  |                 int getBatteryVoltage();
  |
  |                 // Beacon Temperature
  |                 float getBeaconTemperature();
  |
  |                 // Advertisement Count
  |                 long getAdvertisementCount();
  |
  |                 // Elapsed Time
  |                 long getElapsedTime();
  |
  +-- ADManufacturerSpecific
  |      |  // Company ID
  |      |  int getCompanyId();
  |      |
  |      +-- IBeacon
  |      |     // Major Number
  |      |     int getMajor();
  |      |
  |      |     (abbrev)
  |      |
  |      +-- Ucode
  |      |     // Ucode
  |      |     String getUcode();
  |      |
  |      |     (abbrev)
于 2015-09-30T16:59:23.753 回答
3

所有 Eddystone 数据都包含在蓝牙 4.0(“低能耗”、“BLE”)广告数据包中,因此不需要BluetoothGattor BluetoothGap。改为使用BluetoothLeScanner。在onScanResult回调中,您可以通过以下方式访问广告数据:

// assuming `result` is the ScanResult passed to the `onScanResult` callback
byte[] rawData = result
    .getScanRecord()
    .getServiceData(ParcelUuid.fromString("0000FEAA-0000-1000-8000-00805F9B34FB"));

然后,您需要根据 Eddystone 规范解析字节:

UID: https://github.com/google/eddystone/tree/master/eddystone-uid
URL: https://github.com/google/eddystone/tree/master/eddystone-url

Eddystone 存储库中还包含一个示例项目,因此您可以从那里开始,也许可以重用一些代码:

https://github.com/google/eddystone/tree/master/tools/eddystone-validator

于 2015-09-29T08:10:40.313 回答
0

onLeScan您可以使用原生 Android回调检测 Eddystone-UID 信标。我已经发布了示例代码,展示了如何在此处的答案中执行此操作:

https://stackoverflow.com/a/32799901/1461050

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] == 0xaa && scanRecord[startByte+1] == 0xfe &&
                    scanRecord[startByte+2] == 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
            }
        }

    }
}

这样做的关键是理解和解析每种信标类型的字节布局。这就是开源Android 信标库通过为每个信标定义布局表达式以帮助解析来解析多种信标类型(如 Eddystone)的方式。即使您想推出自己的代码,您也可以查看它的源代码并使用它来了解它是如何工作的。我在上面链接的答案中发布了有关此解析如何发生的描述。

于 2015-09-29T12:46:36.733 回答