【发布时间】:2014-06-02 21:43:02
【问题描述】:
我正在开发一种用于宣传制造商特定数据的 BLE 传感器。是否有示例代码演示了如何在 Android 中接收广告数据包并解析其负载?
【问题讨论】:
标签: android bluetooth-lowenergy
我正在开发一种用于宣传制造商特定数据的 BLE 传感器。是否有示例代码演示了如何在 Android 中接收广告数据包并解析其负载?
【问题讨论】:
标签: android bluetooth-lowenergy
这就是我要找的东西:
BLE 扫描 API BluetoothAdapter.startLeScan(ScanCallback) 需要用于扫描结果的回调函数。该方法需要如下所示:
private BluetoothAdapter.LeScanCallback ScanCallback =
new BluetoothAdapter.LeScanCallback()onLeScan(final BluetoothDevice device,
int rssi,
final byte[] scanRecord)
{...}
scanRecord 变量是一个字节数组,其中包含广告数据包负载。
根据 BLE 规范,payload 的结构非常简单,如下所示:
数据包最长可达 47 个字节,包括:
对于广告通信通道,访问地址始终为 0x8E89BED6。
PDU 又具有自己的标头(2 个字节:有效负载的大小及其类型 - 设备是否支持连接等)和实际有效负载(最多 37 个字节)。
最后,payload的前6个字节是设备的MAC地址,实际信息最多可以有31个字节。
实际信息的格式如下:
第一个字节是数据的长度,第二个字节是类型,然后是数据。
这是一种聪明的方法,允许任何应用程序在不关心内容的情况下跳过整个数据记录。
这是确定广告数据包内容的示例代码:
parseAdvertisementPacket(final byte[] scanRecord) {
byte[] advertisedData = Arrays.copyOf(scanRecord, scanRecord.length);
int offset = 0;
while (offset < (advertisedData.length - 2)) {
int len = advertisedData[offset++];
if (len == 0)
break;
int type = advertisedData[offset++];
switch (type) {
case 0x02: // Partial list of 16-bit UUIDs
case 0x03: // Complete list of 16-bit UUIDs
while (len > 1) {
int uuid16 = advertisedData[offset++] & 0xFF;
uuid16 |= (advertisedData[offset++] << 8);
len -= 2;
uuids.add(UUID.fromString(String.format(
"%08x-0000-1000-8000-00805f9b34fb", uuid16)));
}
break;
case 0x06:// Partial list of 128-bit UUIDs
case 0x07:// Complete list of 128-bit UUIDs
// Loop through the advertised 128-bit UUID's.
while (len >= 16) {
try {
// Wrap the advertised bits and order them.
ByteBuffer buffer = ByteBuffer.wrap(advertisedData,
offset++, 16).order(ByteOrder.LITTLE_ENDIAN);
long mostSignificantBit = buffer.getLong();
long leastSignificantBit = buffer.getLong();
uuids.add(new UUID(leastSignificantBit,
mostSignificantBit));
} catch (IndexOutOfBoundsException e) {
// Defensive programming.
Log.e("BlueToothDeviceFilter.parseUUID", e.toString());
continue;
} finally {
// Move the offset to read the next uuid.
offset += 15;
len -= 16;
}
}
break;
case 0xFF: // Manufacturer Specific Data
Log.d(TAG, "Manufacturer Specific Data size:" + len +" bytes" );
while (len > 1) {
if(i < 32) {
MfgData[i++] = advertisedData[offset++];
}
len -= 1;
}
Log.d(TAG, "Manufacturer Specific Data saved." + MfgData.toString());
break;
default:
offset += (len - 1);
break;
}
}
感谢
mass 为我指明了正确的方向!
【讨论】:
ADPayloadParser in nv-bluetooth 解析广告数据包的有效负载并返回 AD 结构列表。 AD结构格式在《Bluetooth Core Specification 4.2》的“11 ADVERTISING AND SCAN RESPONSE DATA FORMAT”中有描述。
以下代码sn -p是onLeScan方法的实现示例。
public void onLeScan(
BluetoothDevice device, int rssi, byte[] scanRecord)
{
// Parse the payload of the advertising packet.
List<ADStructure> structures =
ADPayloadParser.getInstance().parse(scanRecord);
// For each AD structure contained in the advertising packet.
for (ADStructure structure : structures)
{
if (structure instanceof IBeacon)
{
// iBeacon packet was found.
handleIBeacon((IBeacon)structure);
}
......
}
}
您可以为您的制造商特定格式注册自己的解析器到 ADPayloadParser。有关详细信息,请参阅以下链接。
博客:http://darutk-oboegaki.blogspot.jp/2015/03/ibeacon-as-kind-of-ad-structures.html
GitHub:https://github.com/TakahikoKawasaki/nv-bluetooth
JavaDoc:http://takahikokawasaki.github.io/nv-bluetooth/
Maven:http://search.maven.org/#search|ga|1|a%3A%22nv-bluetooth%22
【讨论】:
编辑 21.02.2016
我在下面链接的库似乎已被移动; 见https://github.com/AltBeacon/android-beacon-library
您可以先使用 Android iBeacon 库。
有一个参考应用程序可用于基础知识和模拟数据。 ~https://github.com/RadiusNetworks/ibeacon-reference-android~
一旦您启动并运行它,您可能希望导入该库并在您的真实设备上使用它,网站上还有一些示例代码: http://developer.radiusnetworks.com/ibeacon/android/samples.html
【讨论】: