FileDocCategorySizeDatePackage
ScanFilter.javaAPI DocAndroid 5.1 API22866Thu Mar 12 22:22:10 GMT 2015android.bluetooth.le

ScanFilter

public final class ScanFilter extends Object implements android.os.Parcelable
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:

  • Service UUIDs which identify the bluetooth gatt services running on the device.
  • Name of remote Bluetooth LE device.
  • Mac address of the remote device.
  • Service data which is the data associated with a service.
  • Manufacturer specific data which is the data associated with a particular manufacturer.
  • see
    ScanResult
    see
    BluetoothLeScanner

    Fields Summary
    private final String
    mDeviceName
    private final String
    mDeviceAddress
    private final android.os.ParcelUuid
    mServiceUuid
    private final android.os.ParcelUuid
    mServiceUuidMask
    private final android.os.ParcelUuid
    mServiceDataUuid
    private final byte[]
    mServiceData
    private final byte[]
    mServiceDataMask
    private final int
    mManufacturerId
    private final byte[]
    mManufacturerData
    private final byte[]
    mManufacturerDataMask
    public static final Creator
    CREATOR
    A {@link android.os.Parcelable.Creator} to create {@link ScanFilter} from parcel.
    Constructors Summary
    private ScanFilter(String name, String deviceAddress, android.os.ParcelUuid uuid, android.os.ParcelUuid uuidMask, android.os.ParcelUuid serviceDataUuid, byte[] serviceData, byte[] serviceDataMask, int manufacturerId, byte[] manufacturerData, byte[] manufacturerDataMask)

            mDeviceName = name;
            mServiceUuid = uuid;
            mServiceUuidMask = uuidMask;
            mDeviceAddress = deviceAddress;
            mServiceDataUuid = serviceDataUuid;
            mServiceData = serviceData;
            mServiceDataMask = serviceDataMask;
            mManufacturerId = manufacturerId;
            mManufacturerData = manufacturerData;
            mManufacturerDataMask = manufacturerDataMask;
        
    Methods Summary
    public intdescribeContents()

            return 0;
        
    public booleanequals(java.lang.Object obj)

            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            ScanFilter other = (ScanFilter) obj;
            return Objects.equals(mDeviceName, other.mDeviceName) &&
                    Objects.equals(mDeviceAddress, other.mDeviceAddress) &&
                            mManufacturerId == other.mManufacturerId &&
                    Objects.deepEquals(mManufacturerData, other.mManufacturerData) &&
                    Objects.deepEquals(mManufacturerDataMask, other.mManufacturerDataMask) &&
                    Objects.deepEquals(mServiceDataUuid, other.mServiceDataUuid) &&
                    Objects.deepEquals(mServiceData, other.mServiceData) &&
                    Objects.deepEquals(mServiceDataMask, other.mServiceDataMask) &&
                    Objects.equals(mServiceUuid, other.mServiceUuid) &&
                    Objects.equals(mServiceUuidMask, other.mServiceUuidMask);
        
    public java.lang.StringgetDeviceAddress()

            return mDeviceAddress;
        
    public java.lang.StringgetDeviceName()
    Returns the filter set the device name field of Bluetooth advertisement data.

    
                         
        
           
            return mDeviceName;
        
    public byte[]getManufacturerData()

            return mManufacturerData;
        
    public byte[]getManufacturerDataMask()

            return mManufacturerDataMask;
        
    public intgetManufacturerId()
    Returns the manufacturer id. -1 if the manufacturer filter is not set.

            return mManufacturerId;
        
    public byte[]getServiceData()

            return mServiceData;
        
    public byte[]getServiceDataMask()

            return mServiceDataMask;
        
    public android.os.ParcelUuidgetServiceDataUuid()

            return mServiceDataUuid;
        
    public android.os.ParcelUuidgetServiceUuid()
    Returns the filter set on the service uuid.

            return mServiceUuid;
        
    public android.os.ParcelUuidgetServiceUuidMask()

            return mServiceUuidMask;
        
    public inthashCode()

            return Objects.hash(mDeviceName, mDeviceAddress, mManufacturerId, mManufacturerData,
                    mManufacturerDataMask, mServiceDataUuid, mServiceData, mServiceDataMask,
                    mServiceUuid, mServiceUuidMask);
        
    public booleanmatches(ScanResult scanResult)
    Check if the scan filter matches a {@code scanResult}. A scan result is considered as a match if it matches all the field filters.

            if (scanResult == null) {
                return false;
            }
            BluetoothDevice device = scanResult.getDevice();
            // Device match.
            if (mDeviceAddress != null
                    && (device == null || !mDeviceAddress.equals(device.getAddress()))) {
                return false;
            }
    
            ScanRecord scanRecord = scanResult.getScanRecord();
    
            // Scan record is null but there exist filters on it.
            if (scanRecord == null
                    && (mDeviceName != null || mServiceUuid != null || mManufacturerData != null
                            || mServiceData != null)) {
                return false;
            }
    
            // Local name match.
            if (mDeviceName != null && !mDeviceName.equals(scanRecord.getDeviceName())) {
                return false;
            }
    
            // UUID match.
            if (mServiceUuid != null && !matchesServiceUuids(mServiceUuid, mServiceUuidMask,
                    scanRecord.getServiceUuids())) {
                return false;
            }
    
            // Service data match
            if (mServiceDataUuid != null) {
                if (!matchesPartialData(mServiceData, mServiceDataMask,
                        scanRecord.getServiceData(mServiceDataUuid))) {
                    return false;
                }
            }
    
            // Manufacturer data match.
            if (mManufacturerId >= 0) {
                if (!matchesPartialData(mManufacturerData, mManufacturerDataMask,
                        scanRecord.getManufacturerSpecificData(mManufacturerId))) {
                    return false;
                }
            }
            // All filters match.
            return true;
        
    private booleanmatchesPartialData(byte[] data, byte[] dataMask, byte[] parsedData)

            if (parsedData == null || parsedData.length < data.length) {
                return false;
            }
            if (dataMask == null) {
                for (int i = 0; i < data.length; ++i) {
                    if (parsedData[i] != data[i]) {
                        return false;
                    }
                }
                return true;
            }
            for (int i = 0; i < data.length; ++i) {
                if ((dataMask[i] & parsedData[i]) != (dataMask[i] & data[i])) {
                    return false;
                }
            }
            return true;
        
    private booleanmatchesServiceUuid(java.util.UUID uuid, java.util.UUID mask, java.util.UUID data)

            if (mask == null) {
                return uuid.equals(data);
            }
            if ((uuid.getLeastSignificantBits() & mask.getLeastSignificantBits()) !=
                    (data.getLeastSignificantBits() & mask.getLeastSignificantBits())) {
                return false;
            }
            return ((uuid.getMostSignificantBits() & mask.getMostSignificantBits()) ==
                    (data.getMostSignificantBits() & mask.getMostSignificantBits()));
        
    private booleanmatchesServiceUuids(android.os.ParcelUuid uuid, android.os.ParcelUuid parcelUuidMask, java.util.List uuids)

            if (uuid == null) {
                return true;
            }
            if (uuids == null) {
                return false;
            }
    
            for (ParcelUuid parcelUuid : uuids) {
                UUID uuidMask = parcelUuidMask == null ? null : parcelUuidMask.getUuid();
                if (matchesServiceUuid(uuid.getUuid(), uuidMask, parcelUuid.getUuid())) {
                    return true;
                }
            }
            return false;
        
    public java.lang.StringtoString()

            return "BluetoothLeScanFilter [mDeviceName=" + mDeviceName + ", mDeviceAddress="
                    + mDeviceAddress
                    + ", mUuid=" + mServiceUuid + ", mUuidMask=" + mServiceUuidMask
                    + ", mServiceDataUuid=" + Objects.toString(mServiceDataUuid) + ", mServiceData="
                    + Arrays.toString(mServiceData) + ", mServiceDataMask="
                    + Arrays.toString(mServiceDataMask) + ", mManufacturerId=" + mManufacturerId
                    + ", mManufacturerData=" + Arrays.toString(mManufacturerData)
                    + ", mManufacturerDataMask=" + Arrays.toString(mManufacturerDataMask) + "]";
        
    public voidwriteToParcel(android.os.Parcel dest, int flags)

            dest.writeInt(mDeviceName == null ? 0 : 1);
            if (mDeviceName != null) {
                dest.writeString(mDeviceName);
            }
            dest.writeInt(mDeviceAddress == null ? 0 : 1);
            if (mDeviceAddress != null) {
                dest.writeString(mDeviceAddress);
            }
            dest.writeInt(mServiceUuid == null ? 0 : 1);
            if (mServiceUuid != null) {
                dest.writeParcelable(mServiceUuid, flags);
                dest.writeInt(mServiceUuidMask == null ? 0 : 1);
                if (mServiceUuidMask != null) {
                    dest.writeParcelable(mServiceUuidMask, flags);
                }
            }
            dest.writeInt(mServiceDataUuid == null ? 0 : 1);
            if (mServiceDataUuid != null) {
                dest.writeParcelable(mServiceDataUuid, flags);
                dest.writeInt(mServiceData == null ? 0 : 1);
                if (mServiceData != null) {
                    dest.writeInt(mServiceData.length);
                    dest.writeByteArray(mServiceData);
    
                    dest.writeInt(mServiceDataMask == null ? 0 : 1);
                    if (mServiceDataMask != null) {
                        dest.writeInt(mServiceDataMask.length);
                        dest.writeByteArray(mServiceDataMask);
                    }
                }
            }
            dest.writeInt(mManufacturerId);
            dest.writeInt(mManufacturerData == null ? 0 : 1);
            if (mManufacturerData != null) {
                dest.writeInt(mManufacturerData.length);
                dest.writeByteArray(mManufacturerData);
    
                dest.writeInt(mManufacturerDataMask == null ? 0 : 1);
                if (mManufacturerDataMask != null) {
                    dest.writeInt(mManufacturerDataMask.length);
                    dest.writeByteArray(mManufacturerDataMask);
                }
            }