Fix a bug with filtering by raw bytes when such filter is not provided

See change in BluetoothDeviceFilterUtils

Bug: 30932767
Test: Call API with a BLE filter with no raw bytes filter, matching some device.
Ensure that the device eventually shows up.
Change-Id: Ia4bfd6ac7139c374ef54dfeef71fc99a5d65bcb0
This commit is contained in:
Eugene Susla
2017-03-28 15:04:12 -07:00
parent a39480a5ab
commit f0aae00cc2
3 changed files with 38 additions and 8 deletions

View File

@@ -37,7 +37,7 @@ public class BluetoothDeviceFilterUtils {
private BluetoothDeviceFilterUtils() {}
private static final boolean DEBUG = false;
private static final String LOG_TAG = "BluetoothDeviceFilterUtil";
private static final String LOG_TAG = "BluetoothDeviceFilterUtils";
@Nullable
static String patternToString(@Nullable Pattern p) {
@@ -50,8 +50,10 @@ public class BluetoothDeviceFilterUtils {
}
static boolean matches(ScanFilter filter, BluetoothDevice device) {
return matchesAddress(filter.getDeviceAddress(), device)
boolean result = matchesAddress(filter.getDeviceAddress(), device)
&& matchesServiceUuid(filter.getServiceUuid(), filter.getServiceUuidMask(), device);
if (DEBUG) debugLogMatchResult(result, device, filter);
return result;
}
static boolean matchesAddress(String deviceAddress, BluetoothDevice device) {

View File

@@ -31,6 +31,7 @@ import android.bluetooth.le.ScanResult;
import android.os.Parcel;
import android.provider.OneTimeUseBuilder;
import android.text.TextUtils;
import android.util.Log;
import com.android.internal.util.BitUtils;
import com.android.internal.util.ObjectUtils;
@@ -47,6 +48,9 @@ import java.util.regex.Pattern;
*/
public final class BluetoothLEDeviceFilter implements DeviceFilter<ScanResult> {
private static final boolean DEBUG = false;
private static final String LOG_TAG = "BluetoothLEDeviceFilter";
private static final int RENAME_PREFIX_LENGTH_LIMIT = 10;
private final Pattern mNamePattern;
@@ -145,9 +149,13 @@ public final class BluetoothLEDeviceFilter implements DeviceFilter<ScanResult> {
/** @hide */
@Override
public boolean matches(ScanResult device) {
return matches(device.getDevice())
&& BitUtils.maskedEquals(device.getScanRecord().getBytes(),
mRawDataFilter, mRawDataFilterMask);
boolean result = matches(device.getDevice())
&& (mRawDataFilter == null
|| BitUtils.maskedEquals(device.getScanRecord().getBytes(),
mRawDataFilter, mRawDataFilterMask));
if (DEBUG) Log.i(LOG_TAG, "matches(this = " + this + ", device = " + device +
") -> " + result);
return result;
}
private boolean matches(BluetoothDevice device) {

View File

@@ -110,6 +110,11 @@ public class DeviceDiscoveryService extends Service {
private final ScanCallback mBLEScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
if (DEBUG) {
Log.i(LOG_TAG,
"BLE.onScanResult(callbackType = " + callbackType + ", result = " + result
+ ")");
}
final DeviceFilterPair<ScanResult> deviceFilterPair
= DeviceFilterPair.findMatch(result, mBLEFilters);
if (deviceFilterPair == null) return;
@@ -126,6 +131,10 @@ public class DeviceDiscoveryService extends Service {
private BroadcastReceiver mBluetoothDeviceFoundBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG) {
Log.i(LOG_TAG,
"BL.onReceive(context = " + context + ", intent = " + intent + ")");
}
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
final DeviceFilterPair<BluetoothDevice> deviceFilterPair
= DeviceFilterPair.findMatch(device, mBluetoothFilters);
@@ -191,7 +200,10 @@ public class DeviceDiscoveryService extends Service {
mBLEScanFilters = CollectionUtils.map(mBLEFilters, BluetoothLEDeviceFilter::getScanFilter);
reset();
}
} else if (DEBUG) Log.i(LOG_TAG, "startDiscovery: duplicate request: " + request);
if (!ArrayUtils.isEmpty(mDevicesFound)) {
onReadyToShowUI();
}
@@ -221,6 +233,7 @@ public class DeviceDiscoveryService extends Service {
}
private void reset() {
if (DEBUG) Log.i(LOG_TAG, "reset()");
mDevicesFound.clear();
mSelectedDevice = null;
mDevicesAdapter.notifyDataSetChanged();
@@ -369,8 +382,15 @@ public class DeviceDiscoveryService extends Service {
public static <T extends Parcelable> DeviceFilterPair<T> findMatch(
T dev, @Nullable List<? extends DeviceFilter<T>> filters) {
if (isEmpty(filters)) return new DeviceFilterPair<>(dev, null);
final DeviceFilter<T> matchingFilter = CollectionUtils.find(filters, (f) -> f.matches(dev));
return matchingFilter != null ? new DeviceFilterPair<>(dev, matchingFilter) : null;
final DeviceFilter<T> matchingFilter
= CollectionUtils.find(filters, f -> f.matches(dev));
DeviceFilterPair<T> result = matchingFilter != null
? new DeviceFilterPair<>(dev, matchingFilter)
: null;
if (DEBUG) Log.i(LOG_TAG, "findMatch(dev = " + dev + ", filters = " + filters +
") -> " + result);
return result;
}
public String getDisplayName() {