Skip to content Skip to sidebar Skip to footer

Retrieving Android Bluetoothle Device Information (major / Minor / Identifier / Proximityuuid) On Scan?

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.LeScanCall

Solution 1:

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 datapublicclassScannedBleDeviceimplementsSerializable {
    // 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;
    publicdouble RSSI;
    publicdouble Distance;

    publicbyte[] CompanyId;
    publicbyte[] IbeaconProximityUUID;
    publicbyte[] Major;
    publicbyte[] Minor;
    publicbyte Tx;

    publiclong 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 = newbyte[2];
        companyId[0] = magic.get(2);
        companyId[1] = magic.get(3);
        parsedObj.CompanyId = companyId;

        byte[] ibeaconProximityUUID = newbyte[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.");
                returnnull;
            }

            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,
                                            " "));
                    returnnull;
                }
            }

        }

        parsedObj.IbeaconProximityUUID = ibeaconProximityUUID;

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

        byte[] minor = newbyte[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());returnnull;
    }
}

Solution 2:

  1. Payloads of advertising packets should be parsed as a list of AD structures.
  2. iBeacon is a kind of AD structures.

See "iBeacon as a kind of AD structures" for details. Also, see an answer to a similar question.

Post a Comment for "Retrieving Android Bluetoothle Device Information (major / Minor / Identifier / Proximityuuid) On Scan?"