检索的Andr​​oid BluetoothLE设备信息(主要/次要/识别/ ProximityUUID)上扫描? [英] Retrieving Android BluetoothLE device information (Major / minor / identifier / ProximityUUID) on scan?

查看:325
本文介绍了检索的Andr​​oid BluetoothLE设备信息(主要/次要/识别/ ProximityUUID)上扫描?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在环顾四周,不幸的是,Android的ibeacon库已经去precated,所以我试图做到这一点本土。我已经实现了BluetoothAdapter.LeScanCallback和内置onLeScan()方法将火当设备被拿起。我想读取该设备的ProximityUUID,主要和次要的特点和标识。我不知道如何获取这些信息了Android的对象BluetoothDevice类的。

I've been looking around and unfortunately the android ibeacon library has been deprecated, so I am attempting to do this native. I have implemented the BluetoothAdapter.LeScanCallback and the built in onLeScan() method that will fire when a device is picked up. I would like to read in that device's ProximityUUID, major and minor characteristics and identifier. I'm not sure how to get that information out of the Android object BluetoothDevice.

我如何提取该信息(ProximityUUID,主要,次要,和放大器;识别特征)?从Android BluetoothDevice类,或有另一种方式来做到这一点。

How do I extract that information (ProximityUUID, major, minor, & identifier characteristics) from the Android BluetoothDevice, or is there another way to do it?

谢谢!

推荐答案

您可以参考此的帖子要充分了解这些字节指的 LeScanCallback 的。 这是我的code解析所需的全部信息:

you can refer this post to fully understand what those bytes means in LeScanCallback . And this is my code to parse all information needed:

// an object with all information embedded from LeScanCallback data
public class ScannedBleDevice implements Serializable {
    // public BluetoothDevice BLEDevice;

    /**
    * Returns the hardware address of this BluetoothDevice.
    * <p>
    * For example, "00:11:22:AA:BB:CC".
    * 
    * @return Bluetooth hardware address as string
    */
    public String MacAddress;

    public String DeviceName;
    public double RSSI;
    public double Distance;

    public byte[] CompanyId;
    public byte[] IbeaconProximityUUID;
    public byte[] Major;
    public byte[] Minor;
    public byte Tx;

    public long ScannedTime;
}

// use this method to parse those bytes and turn to an object which defined proceeding.
// the uuidMatcher works as a UUID filter, put null if you want parse any BLE advertising data around.
private ScannedBleDevice ParseRawScanRecord(BluetoothDevice device,
        int rssi, byte[] advertisedData, byte[] uuidMatcher) {
    try {
        ScannedBleDevice parsedObj = new ScannedBleDevice();
        // parsedObj.BLEDevice = device;
        parsedObj.DeviceName = device.getName();
        parsedObj.MacAddress = device.getAddress();
        parsedObj.RSSI = rssi;
        List<UUID> uuids = new ArrayList<UUID>();
        int skippedByteCount = advertisedData[0];
        int magicStartIndex = skippedByteCount + 1;
        int magicEndIndex = magicStartIndex
                + advertisedData[magicStartIndex] + 1;
        ArrayList<Byte> magic = new ArrayList<Byte>();
        for (int i = magicStartIndex; i < magicEndIndex; i++) {
            magic.add(advertisedData[i]);
        }

        byte[] companyId = new byte[2];
        companyId[0] = magic.get(2);
        companyId[1] = magic.get(3);
        parsedObj.CompanyId = companyId;

        byte[] ibeaconProximityUUID = new byte[16];
        for (int i = 0; i < 16; i++) {
            ibeaconProximityUUID[i] = magic.get(i + 6);
        }

        if (uuidMatcher != null) {
            if (ibeaconProximityUUID.length != uuidMatcher.length) {
                Log.e(LOG_TAG,
                        "Scanned UUID: "
                                + Util.BytesToHexString(
                                        ibeaconProximityUUID, " ")
                                + " filtered by UUID Matcher "
                                + Util.BytesToHexString(uuidMatcher, " ")
                                + " with length requirment.");
                return null;
            }

            for (int i = 0; i < 16; i++) {
                if (ibeaconProximityUUID[i] != uuidMatcher[i]) {
                    Log.e(LOG_TAG,
                            "Scanned UUID: "
                                    + Util.BytesToHexString(
                                            ibeaconProximityUUID, " ")
                                    + " filtered by UUID Matcher "
                                    + Util.BytesToHexString(uuidMatcher,
                                            " "));
                    return null;
                }
            }

        }

        parsedObj.IbeaconProximityUUID = ibeaconProximityUUID;

        byte[] major = new byte[2];
        major[0] = magic.get(22);
        major[1] = magic.get(23);
        parsedObj.Major = major;

        byte[] minor = new byte[2];
        minor[0] = magic.get(24);
        minor[1] = magic.get(25);
        parsedObj.Minor = minor;

        byte tx = 0;
        tx = magic.get(26);
        parsedObj.Tx = tx;

        parsedObj.ScannedTime = new Date().getTime();
        return parsedObj;
    } catch (Exception ex) {
        Log.e(LOG_TAG, "skip one unknow format data...");
        // Log.e(LOG_TAG,
        // "Exception in ParseRawScanRecord with advertisedData: "
        // + Util.BytesToHexString(advertisedData, " ")
        // + ", detail: " + ex.getMessage());
        return null;
    }
}

这篇关于检索的Andr​​oid BluetoothLE设备信息(主要/次要/识别/ ProximityUUID)上扫描?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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