美文网首页蓝牙
手机锁屏状态下,后台服务无法扫描蓝牙设备

手机锁屏状态下,后台服务无法扫描蓝牙设备

作者: 冰珊孤雪 | 来源:发表于2019-05-07 16:24 被阅读0次

搜索蓝牙接口:

/**
     * Start Bluetooth LE scan. The scan results will be delivered through {@code callback}.
     * For unfiltered scans, scanning is stopped on screen off to save power. Scanning is
     * resumed when screen is turned on again. To avoid this, do filetered scanning by
     * using proper {@link ScanFilter}.
     * <p>
     * An app must hold
     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION ACCESS_FINE_LOCATION} permission
     * in order to get results.
     *
     * @param filters {@link ScanFilter}s for finding exact BLE devices.
     * @param settings Settings for the scan.
     * @param callback Callback used to deliver scan results.
     * @throws IllegalArgumentException If {@code settings} or {@code callback} is null.
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
    public void startScan(List<ScanFilter> filters, ScanSettings settings,
            final ScanCallback callback) {
        startScan(filters, settings, null, callback, /*callbackIntent=*/ null, null);
    }

注释上写的非常清楚:

For unfiltered scans, scanning is stopped on screen off to save power. Scanning is
* resumed when screen is turned on again. To avoid this, do filetered scanning by
* using proper {@link ScanFilter}.

没有添加过滤的情况下锁屏状态会停止扫描,所以我们只需要加上过滤条件ScanFilter即可,示例代码如下:

List<ScanFilter> scanFilters = new ArrayList<>();
            scanFilters.add(
                        new ScanFilter.Builder().setDeviceName("INPUT_YOUR_FILTER_NAME").build()
                );

          
            ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder()
                    .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                scanSettingsBuilder.setPhy(scannerParams.getPhy())
                        .setLegacy(false);
            }
            ScanSettings scanSettings = scanSettingsBuilder.build();

            try {
                // Cannot start unfiltered scan in location-off. This scan will be resumed when location is on: 6
                mBluetoothLeScanner.startScan(scanFilters, scanSettings, mScanCallback);
                return true;
            } catch (Exception e) {
                ZLogger.e(e.toString());
                return false;
            }

GattService.java

// Check if a scan record matches a specific filters.
    private boolean matchesFilters(ScanClient client, ScanResult scanResult) {
        if (client.filters == null || client.filters.isEmpty()) {
            return true;
        }
        for (ScanFilter filter : client.filters) {
            if (filter.matches(scanResult)) {
                return true;
            }
        }
        return false;
    }

ScanFilter.java

// Manufacturer data match.
        if (mManufacturerId >= 0) {
            if (!matchesPartialData(mManufacturerData, mManufacturerDataMask,
                    scanRecord.getManufacturerSpecificData(mManufacturerId))) {
                return false;
            }
        }

相关文章

  • 蓝牙问题集锦

    扫描蓝牙 手机锁屏状态下,后台服务无法扫描蓝牙设备频繁扫描,导致扫描蓝牙失败定位服务关闭时,后台无法扫描蓝牙

  • 手机锁屏状态下,后台服务无法扫描蓝牙设备

    搜索蓝牙接口: 注释上写的非常清楚: For unfiltered scans, scanning is stop...

  • iOS - 蓝牙后台运行问题

    App在连接蓝牙设备使用时,手机可能会息屏或者手动锁屏了,App进入后台模式,如果没有进行相应处理,App就不会继...

  • Android 低功耗蓝牙BLE 开发注意事项

    基本概念和问题 1、蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构...

  • Ble蓝牙广播数据包解析

    Ble蓝牙广播数据包解析 项目背景 蓝牙扫描时需做到快速连接指定设备的蓝牙,苹果手机应系统原因,无法获取手机的唯 ...

  • 蓝牙:Bluetooth API

    打开蓝牙 扫描其他蓝牙设备 查询本地蓝牙适配器的配对蓝牙设备 建立 RFCOMM 通道 通过服务发现连接到其他设备...

  • 2020-09-03 android 设备ble扫描到设备

    蓝牙扫描不到ble低功耗设备,定位权限已经打开,但是因为设备关闭了定位服务,所以导致无法搜索到设备 Cannot ...

  • iOS 蓝牙 - CoreBluetooth

    一、简介 1.蓝牙开发关键词 中心设备:就是用来扫描周围蓝牙硬件的设备,比如通过你手机的蓝牙来扫描并连接智能手环,...

  • Android蓝牙扫描

    蓝牙扫描的目的在于发现设备或者接收设备广播,设备包括经典蓝牙设备和BLE蓝牙设备,这两种设备的扫描方式不同。 经典...

  • 定位服务关闭时,后台无法扫描蓝牙

    Cannot start unfiltered scan in location-off. This scan w...

网友评论

    本文标题:手机锁屏状态下,后台服务无法扫描蓝牙设备

    本文链接:https://www.haomeiwen.com/subject/shcyoqtx.html