Eddystone UID:具有 10 字节命名空间组件和 6 字节实例组件的唯一静态 ID。
在onScanResult 中,您可以提取 Eddystone uid 数据包,如下所示
override fun onScanResult(callbackType: Int, result: ScanResult) {
val scanRecord = result.scanRecord
if (scanRecord != null) {
val serviceUuids = scanRecord.serviceUuids
if (serviceUuids != null && serviceUuids.size > 0 && serviceUuids.contains(
eddystoneServiceId
)
) {
val serviceData = scanRecord.getServiceData(eddystoneServiceId)
if (serviceData != null && serviceData.size > 18) {
val eddystoneUUID =
Utils.toHexString(Arrays.copyOfRange(serviceData, 2, 18))
val namespace = String(eddystoneUUID.toCharArray().sliceArray(0..19))
val instance = String(
eddystoneUUID.toCharArray()
.sliceArray(20 until eddystoneUUID.toCharArray().size)
)
Log.e("DINKAR", "Namespace:$namespace Instance:$instance")
}
}
}
}
scanRecord:广告和扫描响应的组合
serviceUuids:广告中用于识别蓝牙 GATT 服务的服务 UUID 列表。
eddystoneServiceId:Eddystone UID 的服务 UUID,即“0000FEAA-0000–1000–8000–00805F9B34FB”
serviceData:与 serviceUuid 关联的服务数据字节数组,在我们的例子中是 eddystoneServiceId
eddystoneUID 数据包信息在 serviceData 中从索引 2 到 18,我们需要使用实用方法将此字节数组转换为 Hex 字符串。
命名空间为 10 个字节,以 eddystoneUID 的 20 个字符开头
instanceId为6个字节,即eddystoneUID的剩余12个字符
向您展示如何将字节数组转换为十六进制字符串的示例
private val HEX = "0123456789ABCDEF".toCharArray()
fun toHexString(bytes: ByteArray): String {
if (bytes.isEmpty()) {
return ""
}
val hexChars = CharArray(bytes.size * 2)
for (j in bytes.indices) {
val v = (bytes[j].toInt() and 0xFF)
hexChars[j * 2] = HEX[v ushr 4]
hexChars[j * 2 + 1] = HEX[v and 0x0F]
}
return String(hexChars)
}
我写了一篇关于解析Eddystone UID和iBeacon的博客,你可以参考一下here
另外,您可以参考完整的工作示例应用程序来扫描 Eddystone UID、iBeacon 和普通蓝牙外围设备here