Search in sources :

Example 21 with ScanFilter

use of android.bluetooth.le.ScanFilter in project android_frameworks_base by ResurrectionRemix.

the class BluetoothAdapter method startLeScan.

/**
     * Starts a scan for Bluetooth LE devices, looking for devices that
     * advertise given services.
     *
     * <p>Devices which advertise all specified services are reported using the
     * {@link LeScanCallback#onLeScan} callback.
     *
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
     *
     * @param serviceUuids Array of services to look for
     * @param callback the callback LE scan results are delivered
     * @return true, if the scan was started successfully
     * @deprecated use {@link BluetoothLeScanner#startScan(List, ScanSettings, ScanCallback)}
     *             instead.
     */
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean startLeScan(final UUID[] serviceUuids, final LeScanCallback callback) {
    if (DBG)
        Log.d(TAG, "startLeScan(): " + Arrays.toString(serviceUuids));
    if (callback == null) {
        if (DBG)
            Log.e(TAG, "startLeScan: null callback");
        return false;
    }
    BluetoothLeScanner scanner = getBluetoothLeScanner();
    if (scanner == null) {
        if (DBG)
            Log.e(TAG, "startLeScan: cannot get BluetoothLeScanner");
        return false;
    }
    synchronized (mLeScanClients) {
        if (mLeScanClients.containsKey(callback)) {
            if (DBG)
                Log.e(TAG, "LE Scan has already started");
            return false;
        }
        try {
            IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
            if (iGatt == null) {
                // BLE is not supported
                return false;
            }
            ScanCallback scanCallback = new ScanCallback() {

                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                    if (callbackType != ScanSettings.CALLBACK_TYPE_ALL_MATCHES) {
                        // Should not happen.
                        Log.e(TAG, "LE Scan has already started");
                        return;
                    }
                    ScanRecord scanRecord = result.getScanRecord();
                    if (scanRecord == null) {
                        return;
                    }
                    if (serviceUuids != null) {
                        List<ParcelUuid> uuids = new ArrayList<ParcelUuid>();
                        for (UUID uuid : serviceUuids) {
                            uuids.add(new ParcelUuid(uuid));
                        }
                        List<ParcelUuid> scanServiceUuids = scanRecord.getServiceUuids();
                        if (scanServiceUuids == null || !scanServiceUuids.containsAll(uuids)) {
                            if (DBG)
                                Log.d(TAG, "uuids does not match");
                            return;
                        }
                    }
                    callback.onLeScan(result.getDevice(), result.getRssi(), scanRecord.getBytes());
                }
            };
            ScanSettings settings = new ScanSettings.Builder().setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
            List<ScanFilter> filters = new ArrayList<ScanFilter>();
            if (serviceUuids != null && serviceUuids.length > 0) {
                // Note scan filter does not support matching an UUID array so we put one
                // UUID to hardware and match the whole array in callback.
                ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(serviceUuids[0])).build();
                filters.add(filter);
            }
            scanner.startScan(filters, settings, scanCallback);
            mLeScanClients.put(callback, scanCallback);
            return true;
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
    }
    return false;
}
Also used : ParcelUuid(android.os.ParcelUuid) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanSettings(android.bluetooth.le.ScanSettings) ScanFilter(android.bluetooth.le.ScanFilter) ArrayList(java.util.ArrayList) ScanCallback(android.bluetooth.le.ScanCallback) UUID(java.util.UUID) RemoteException(android.os.RemoteException) ScanRecord(android.bluetooth.le.ScanRecord) RequiresPermission(android.annotation.RequiresPermission)

Example 22 with ScanFilter

use of android.bluetooth.le.ScanFilter in project android-beacon-library by AltBeacon.

the class CycledLeScannerForLollipop method startScan.

@Override
protected void startScan() {
    if (!isBluetoothOn()) {
        LogManager.d(TAG, "Not starting scan because bluetooth is off");
        return;
    }
    List<ScanFilter> filters = new ArrayList<ScanFilter>();
    ScanSettings settings;
    if (mBackgroundFlag && !mMainScanCycleActive) {
        LogManager.d(TAG, "starting filtered scan in SCAN_MODE_LOW_POWER");
        settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build();
        filters = new ScanFilterUtils().createScanFiltersForBeaconParsers(mBeaconManager.getBeaconParsers());
    } else {
        LogManager.d(TAG, "starting non-filtered scan in SCAN_MODE_LOW_LATENCY");
        settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)).build();
    }
    postStartLeScan(filters, settings);
}
Also used : ScanSettings(android.bluetooth.le.ScanSettings) ScanFilter(android.bluetooth.le.ScanFilter) ArrayList(java.util.ArrayList)

Example 23 with ScanFilter

use of android.bluetooth.le.ScanFilter in project android-beacon-library by AltBeacon.

the class ScanFilterUtils method createScanFiltersForBeaconParsers.

public List<ScanFilter> createScanFiltersForBeaconParsers(List<BeaconParser> beaconParsers) {
    List<ScanFilter> scanFilters = new ArrayList<ScanFilter>();
    // hardware manufacturers
    for (BeaconParser beaconParser : beaconParsers) {
        List<ScanFilterData> sfds = createScanFilterDataForBeaconParser(beaconParser);
        for (ScanFilterData sfd : sfds) {
            ScanFilter.Builder builder = new ScanFilter.Builder();
            if (sfd.serviceUuid != null) {
                // Use a 16 bit service UUID in a 128 bit form
                String serviceUuidString = String.format("0000%04X-0000-1000-8000-00805f9b34fb", sfd.serviceUuid);
                String serviceUuidMaskString = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
                ParcelUuid parcelUuid = ParcelUuid.fromString(serviceUuidString);
                ParcelUuid parcelUuidMask = ParcelUuid.fromString(serviceUuidMaskString);
                if (LogManager.isVerboseLoggingEnabled()) {
                    LogManager.d(TAG, "making scan filter for service: " + serviceUuidString + " " + parcelUuid);
                    LogManager.d(TAG, "making scan filter with service mask: " + serviceUuidMaskString + " " + parcelUuidMask);
                }
                builder.setServiceUuid(parcelUuid, parcelUuidMask);
            } else {
                builder.setServiceUuid(null);
                builder.setManufacturerData((int) sfd.manufacturer, sfd.filter, sfd.mask);
            }
            ScanFilter scanFilter = builder.build();
            if (LogManager.isVerboseLoggingEnabled()) {
                LogManager.d(TAG, "Set up a scan filter: " + scanFilter);
            }
            scanFilters.add(scanFilter);
        }
    }
    return scanFilters;
}
Also used : ParcelUuid(android.os.ParcelUuid) BeaconParser(org.altbeacon.beacon.BeaconParser) ScanFilter(android.bluetooth.le.ScanFilter) ArrayList(java.util.ArrayList)

Example 24 with ScanFilter

use of android.bluetooth.le.ScanFilter in project android_frameworks_base by crdroidandroid.

the class ScanFilterTest method testsetNameFilter.

@SmallTest
public void testsetNameFilter() {
    ScanFilter filter = mFilterBuilder.setDeviceName("Ped").build();
    assertTrue("setName filter fails", filter.matches(mScanResult));
    filter = mFilterBuilder.setDeviceName("Pem").build();
    assertFalse("setName filter fails", filter.matches(mScanResult));
}
Also used : ScanFilter(android.bluetooth.le.ScanFilter) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 25 with ScanFilter

use of android.bluetooth.le.ScanFilter in project android_frameworks_base by crdroidandroid.

the class ScanFilterTest method testsetServiceDataFilter.

@SmallTest
public void testsetServiceDataFilter() {
    byte[] setServiceData = new byte[] { 0x50, 0x64 };
    ParcelUuid serviceDataUuid = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
    ScanFilter filter = mFilterBuilder.setServiceData(serviceDataUuid, setServiceData).build();
    assertTrue("service data filter fails", filter.matches(mScanResult));
    byte[] emptyData = new byte[0];
    filter = mFilterBuilder.setServiceData(serviceDataUuid, emptyData).build();
    assertTrue("service data filter fails", filter.matches(mScanResult));
    byte[] prefixData = new byte[] { 0x50 };
    filter = mFilterBuilder.setServiceData(serviceDataUuid, prefixData).build();
    assertTrue("service data filter fails", filter.matches(mScanResult));
    byte[] nonMatchData = new byte[] { 0x51, 0x64 };
    byte[] mask = new byte[] { (byte) 0x00, (byte) 0xFF };
    filter = mFilterBuilder.setServiceData(serviceDataUuid, nonMatchData, mask).build();
    assertTrue("partial service data filter fails", filter.matches(mScanResult));
    filter = mFilterBuilder.setServiceData(serviceDataUuid, nonMatchData).build();
    assertFalse("service data filter fails", filter.matches(mScanResult));
}
Also used : ParcelUuid(android.os.ParcelUuid) ScanFilter(android.bluetooth.le.ScanFilter) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

ScanFilter (android.bluetooth.le.ScanFilter)52 SmallTest (android.test.suitebuilder.annotation.SmallTest)30 ParcelUuid (android.os.ParcelUuid)19 ScanSettings (android.bluetooth.le.ScanSettings)11 BluetoothLeScanner (android.bluetooth.le.BluetoothLeScanner)10 ArrayList (java.util.ArrayList)9 RequiresPermission (android.annotation.RequiresPermission)5 TargetApi (android.annotation.TargetApi)5 ScanCallback (android.bluetooth.le.ScanCallback)5 ScanRecord (android.bluetooth.le.ScanRecord)5 ScanResult (android.bluetooth.le.ScanResult)5 Message (android.os.Message)5 Parcel (android.os.Parcel)5 RemoteException (android.os.RemoteException)5 UUID (java.util.UUID)5 NonNull (android.support.annotation.NonNull)3 QueryBuilder (de.greenrobot.dao.query.QueryBuilder)2 BeaconParser (org.altbeacon.beacon.BeaconParser)1