gpt4 book ai didi

android - 如何识别Eddystone URL和uid?

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

我希望在不使用 Proximity Beacon API 或 Nearby Messages API 的情况下检测 Eddystone Ul 和 uid。我希望使用 BluetoothAdapter 或 BluetoothGatt 或 BluetoothGap 等原生 android 库来解析 eddystone 帧。这可行吗?如果是这样的话,如果不可行,那么还有什么选择呢?

最佳答案

以下是获取有关 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());
}
}
}

您不必了解 Eddystone specification 的详细信息如果你使用 nv-bluetooth .

Gradle

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

Java文档

JavaDoc

备忘录

信标温度 Eddystone TLM带符号的定点符号表示。 How to detect Eddystone-Compatible Beacons 中的代码示例(Android Beacon 库)在撰写本文时没有展示如何将数据提取为 float 。另一方面,<a href="http://takahikokawasaki.github.io/nv-bluetooth/com/neovisionaries/bluetooth/ble/advertising/EddystoneTLM.html" rel="noreferrer noopener nofollow">EddystoneTLM</a> nv-bluetooth 中的类有如下所示的方法,因此您不必解码定点符号。

public float getBeaconTemperature();

同样,<a href="http://takahikokawasaki.github.io/nv-bluetooth/com/neovisionaries/bluetooth/ble/advertising/EddystoneURL.html" rel="noreferrer noopener nofollow">EddystoneURL</a>类有一个方法来获取 URL 作为 URL .

public URL getURL();

因此,当您使用 Android Beacon Library 时,您不必执行如下所示的步骤。

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)

关于android - 如何识别Eddystone URL和uid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32836728/

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