/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.bluetooth.le; import android.annotation.Nullable; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.os.Parcel; import android.os.ParcelUuid; import android.os.Parcelable; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.UUID; /** * Criteria for filtering result from Bluetooth LE scans. A {@link ScanFilter} allows clients to * restrict scan results to only those that are of interest to them. *
* Current filtering on the following fields are supported: *
* The {@code serviceDataMask} must have the same length of the {@code serviceData}. * * @throws IllegalArgumentException If {@code serviceDataUuid} is null or * {@code serviceDataMask} is {@code null} while {@code serviceData} is not or * {@code serviceDataMask} and {@code serviceData} has different length. */ public Builder setServiceData(ParcelUuid serviceDataUuid, byte[] serviceData, byte[] serviceDataMask) { if (serviceDataUuid == null) { throw new IllegalArgumentException("serviceDataUuid is null"); } if (mServiceDataMask != null) { if (mServiceData == null) { throw new IllegalArgumentException( "serviceData is null while serviceDataMask is not null"); } // Since the mServiceDataMask is a bit mask for mServiceData, the lengths of the two // byte array need to be the same. if (mServiceData.length != mServiceDataMask.length) { throw new IllegalArgumentException( "size mismatch for service data and service data mask"); } } mServiceDataUuid = serviceDataUuid; mServiceData = serviceData; mServiceDataMask = serviceDataMask; return this; } /** * Set filter on on manufacturerData. A negative manufacturerId is considered as invalid id. *
* Note the first two bytes of the {@code manufacturerData} is the manufacturerId. * * @throws IllegalArgumentException If the {@code manufacturerId} is invalid. */ public Builder setManufacturerData(int manufacturerId, byte[] manufacturerData) { if (manufacturerData != null && manufacturerId < 0) { throw new IllegalArgumentException("invalid manufacture id"); } mManufacturerId = manufacturerId; mManufacturerData = manufacturerData; mManufacturerDataMask = null; // clear manufacturer data mask return this; } /** * Set filter on partial manufacture data. For any bit in the mask, set it the 1 if it needs * to match the one in manufacturer data, otherwise set it to 0. *
* The {@code manufacturerDataMask} must have the same length of {@code manufacturerData}. * * @throws IllegalArgumentException If the {@code manufacturerId} is invalid, or * {@code manufacturerData} is null while {@code manufacturerDataMask} is not, * or {@code manufacturerData} and {@code manufacturerDataMask} have different * length. */ public Builder setManufacturerData(int manufacturerId, byte[] manufacturerData, byte[] manufacturerDataMask) { if (manufacturerData != null && manufacturerId < 0) { throw new IllegalArgumentException("invalid manufacture id"); } if (mManufacturerDataMask != null) { if (mManufacturerData == null) { throw new IllegalArgumentException( "manufacturerData is null while manufacturerDataMask is not null"); } // Since the mManufacturerDataMask is a bit mask for mManufacturerData, the lengths // of the two byte array need to be the same. if (mManufacturerData.length != mManufacturerDataMask.length) { throw new IllegalArgumentException( "size mismatch for manufacturerData and manufacturerDataMask"); } } mManufacturerId = manufacturerId; mManufacturerData = manufacturerData; mManufacturerDataMask = manufacturerDataMask; return this; } /** * Build {@link ScanFilter}. * * @throws IllegalArgumentException If the filter cannot be built. */ public ScanFilter build() { return new ScanFilter(mDeviceName, mDeviceAddress, mServiceUuid, mUuidMask, mServiceDataUuid, mServiceData, mServiceDataMask, mManufacturerId, mManufacturerData, mManufacturerDataMask); } } }