繁体   English   中英

蓝牙扫描记录相同的值为 null

[英]Bluetooth Scanrecord same values are null

我试图读取信标扫描结果,一些值来了,但result.getScanRecord().getServiceUuids()总是 null。

而且我也不明白如何解析“ result.getScanRecord().getBytes() ”。 我看到了很多例子,但它无法理解或如此复杂。 我需要次要、主要和 uuid 值。

我正在使用minSdkVersion 21

最后我找到了一些东西,也许你也需要;

首先,您需要解析数据;

private String[] myFormula(byte[] data) {
    String[] newData = new String[data.length];

    for (int i = 0; i < data.length; i++) {
        if (data[i] > 0) {
            if (Integer.parseInt(data[i] + "", 16) < 9) {
                newData[i] = "0" + Integer.toHexString(data[i]);
            } else {
                newData[i] = Integer.toHexString(data[i]);
            }
        } else {
            newData[i] = Integer.toHexString(data[i] + 256);
        }
    }

    return newData;
}

获得其他数据后;

public int getTxPower() {
    Number xx = hexToDec(scanRecord[26]);

    return xx.intValue();
    //return Integer.parseInt(scanRecord[26], 16);
}

public int getMinor() {
    String s = "";
    if (!scanRecord[24].equals("100")) {
        s += scanRecord[24];
    }

    s += scanRecord[25];

    try {
        return Integer.parseInt(s, 16);
    } catch (NumberFormatException e) {
        e.printStackTrace();
        return -1;
    }
}

public int getMajor() {
    String s = "";
    if (!scanRecord[22].equals("100")) {
        s += scanRecord[22];
    }

    s += scanRecord[23];

    try {
        return Integer.parseInt(s, 16);
    } catch (NumberFormatException e) {
        e.printStackTrace();
        return -1;
    }
}

public void get allDatas(){
    scanRecord = myFormula(result.getScanRecord().getBytes());

    MINOR = getMinor();
    MAJOR = getMajor();
    TXPOWER = getTxPower();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM