Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0a869c6f authored by Wei Wang's avatar Wei Wang
Browse files

Workaround for reversed mac addresses in batch scan.

b/16803363

Change-Id: I47299ed2a6191e7f05c229feb1f1f0424e486907
parent ab40a156
Loading
Loading
Loading
Loading
+16 −3
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.os.SystemClock;
@@ -49,7 +48,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@@ -975,11 +973,14 @@ public class GattService extends ProfileService {
    }

    private Set<ScanResult> parseTruncatedResults(int numRecords, byte[] batchRecord) {
        if (DBG) Log.d(TAG, "batch record " + Arrays.toString(batchRecord));
        Set<ScanResult> results = new HashSet<ScanResult>(numRecords);
        for (int i = 0; i < numRecords; ++i) {
            byte[] record = extractBytes(batchRecord, i * TRUNCATED_RESULT_SIZE,
                    TRUNCATED_RESULT_SIZE);
            byte[] address = extractBytes(batchRecord, 0, 6);
            byte[] address = extractBytes(record, 0, 6);
            // TODO: remove temp hack.
            reverse(address);
            BluetoothDevice device = mAdapter.getRemoteDevice(address);
            int rssi = record[8];
            // Timestamp is in every 50 ms.
@@ -1003,6 +1004,8 @@ public class GattService extends ProfileService {
        int position = 0;
        while (position < batchRecord.length) {
            byte[] address = extractBytes(batchRecord, position, 6);
            // TODO: remove temp hack.
            reverse(address);
            BluetoothDevice device = mAdapter.getRemoteDevice(address);
            position += 6;
            // Skip address type.
@@ -1031,6 +1034,16 @@ public class GattService extends ProfileService {
        return results;
    }

    // Reverse byte array.
    private void reverse(byte[] address) {
        int len = address.length;
        for (int i = 0; i < len / 2; ++i) {
            byte b = address[i];
            address[i] = address[len - 1 - i];
            address[len - 1 - i] = b;
        }
    }

    // Helper method to extract bytes from byte array.
    private static byte[] extractBytes(byte[] scanRecord, int start, int length) {
        byte[] bytes = new byte[length];
+2 −3
Original line number Diff line number Diff line
@@ -256,7 +256,6 @@ public class ScanManager {
        private static final int SCAN_MODE_LOW_LATENCY_WINDOW_MS = 5000;
        private static final int SCAN_MODE_LOW_LATENCY_INTERVAL_MS = 5000;


        // The logic is AND for each filter field.
        private static final int LIST_LOGIC_TYPE = 0x1111111;
        private static final int FILTER_LOGIC_TYPE = 1;
@@ -386,8 +385,8 @@ public class ScanManager {
            waitForCallback();
            int scanMode = getResultType(client.settings);
            // TODO: configure scan parameters.
            int scanIntervalUnit = 8;
            int scanWindowUnit = 8;
            int scanIntervalUnit = 2400;
            int scanWindowUnit = 2400;
            int discardRule = DISCARD_OLDEST_WHEN_BUFFER_FULL;
            int addressType = 0;
            logd("Starting BLE batch scan, scanMode -" + scanMode);