在Android中获取BLE Beacon的Tx功能 [英] Get Tx Power of BLE Beacon in Android

查看:97
本文介绍了在Android中获取BLE Beacon的Tx功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android设备上获得BLE信标的发射功率.

I want to get the Tx power of a BLE beacon with an Android device.

我在这里定义了分配给Tx电源的编号.

I defined the assigned number for Tx power here.

public class AssignedNumbers {
    ...
    public static final byte TXPOWER = 0x0A;
    ...
}

然后我制作了一个函数来在此处获取Tx功率.

Then I made a function to get Tx power here.

public class AdvertisingData {
    ...
    public static Integer getTxPowerLevel(byte[] scanRecord) {
    // Check for BLE 4.0 TX power
    int pos = findCodeInBuffer(scanRecord, AssignedNumbers.TXPOWER);
    if (pos > 0) {
      return Integer.valueOf(scanRecord[pos]);
    }
    return null;
    }
    ...
    private static int findCodeInBuffer(byte[] buffer, byte code) {
        final int length = buffer.length;
        int i = 0;
        while (i < length - 2) {
            int len = buffer[i];
            if (len < 0) {
                return -1;
            }

            if (i + len >= length) {
                return -1;
            }

            byte tcode = buffer[i + 1];
            if (tcode == code) {
                return i + 2;
            }

            i += len + 1;
        }

        return -1;
    }
    ...
}

最后,我放了一行代码来检查Tx功率.

Finally, I put a line of code to check the Tx power.

private BluetoothAdapter.LeScanCallback mLeScanCallback = 
        new BluetoothAdapter.LeScanCallback() {
            ...
            System.out.println("TxPower: " + AdvertisingData.getTxPowerLevel(scanRecord));
            ...
};

但是,结果显示如下.

04-22 16:34:14.249:I/System.out(29133):TxPower:空

04-22 16:34:14.249: I/System.out(29133): TxPower: null

onScanResult()的日志为

The log of onScanResult() is

04-22 16:34:14.247:D/BluetoothLeScanner(29133):onScanResult()- ScanResult {mDevice = 90:59:AF:0F:31:01,mScanRecord = ScanRecord [mAdvertiseFlags = 6,mServiceUuids = null,mManufacturerSpecificData = {76 = [2,21, -43、117、98、71、87,-94、67、68,-111、93,-107,-103、73、121、64,-89、0, 0,0,0,-60]},mServiceData = {0000180a-0000-1000-8000-00805f9b34fb = [1、49、15 -81、89,-112,-60、0、0、0、0]},mTxPowerLevel = -2147483648, mDeviceName = pebBLE],mRssi = -79,mTimestampNanos = 8204624857836}

04-22 16:34:14.247: D/BluetoothLeScanner(29133): onScanResult() - ScanResult{mDevice=90:59:AF:0F:31:01, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=null, mManufacturerSpecificData={76=[2, 21, -43, 117, 98, 71, 87, -94, 67, 68, -111, 93, -107, -103, 73, 121, 64, -89, 0, 0, 0, 0, -60]}, mServiceData={0000180a-0000-1000-8000-00805f9b34fb=[1, 49, 15, -81, 89, -112, -60, 0, 0, 0, 0]}, mTxPowerLevel=-2147483648, mDeviceName=pebBLE], mRssi=-79, mTimestampNanos=8204624857836}

如何获得正确的Tx功率值?该值应为4、0或-23(dBm).

How to get the right value of the Tx power? The values should be 4, 0, or -23(dBm).

推荐答案

您在上面说过信标-我认为您实际上是在尝试获取iBeacon校准的传输功率,该功率不同于GAP 0x0A. iBeacon TxPower只是广告中制造数据的一部分.广告包的完整明细-什么是iBeacon蓝牙配置文件

You say beacon above - I think you're really trying to get iBeacon calibrated transmission power, which is different from the GAP 0x0A. iBeacon TxPower is just part of the manufacture data in the advertisement. There is a full breakdown of the advertising packet here - What is the iBeacon Bluetooth Profile

您可以看到scanRecord中有两个可变大小的部分,并且TxPower是最后一个字节(不以0A开头,就像fitbit在此示例中一样

You can see that there are two variable size sections in the scanRecord, and that the TxPower is the last byte (not preceded by a 0A, as fitbit does in this example http://j2abro.blogspot.com/2014/06/analyzing-bluetooth-advertising-with.html).

查看您的onScanResult,我相信校准后的TxPower为-60,这是1米处的rssi测量值.这篇文章中有一个反向工程TxPower以米为单位的距离的示例-了解ibeacon距离

Looking at your onScanResult, I believe the calibrated TxPower is the -60, which is the rssi measurement at 1 meter. There's an example of reverse engineering TxPower to distance in meters in this post - Understanding ibeacon distancing

这篇关于在Android中获取BLE Beacon的Tx功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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