简体   繁体   中英

Bluetooth Scanrecord same values are null

I tried to read beacon scanresult, some values came but result.getScanRecord().getServiceUuids() always null.

And also i dont understand that how can i parse " result.getScanRecord().getBytes() ". i saw many example but it is not understandable or so complex. i need minor, major and uuid values.

I'am using minSdkVersion 21 .

Finally I found something, maybe you need too;

firstly, you need parse to data;

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;
}

after you get other datas;

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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