是否可以减少扫描 BLE 设备的时间?

Posted

技术标签:

【中文标题】是否可以减少扫描 BLE 设备的时间?【英文标题】:Is possible to reduce the time to scan BLE devices? 【发布时间】:2017-05-31 15:37:08 【问题描述】:

我有这个代码开始扫描 BLE 设备:

Log.i("timeChar", "Begin");
customBluetoothManager.scanLeDevice(true);

然后调用scanLeDevice(),管理蓝牙适配器开始BLE扫描:

public void scanLeDevice(final boolean enable) 
        mHandler = new Handler();

        if (enable) 
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() 
                @Override
                public void run() 
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                
            , SCAN_PERIOD);

            mBluetoothAdapter.startLeScan(mLeScanCallback);
         else 
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        
    

现在,通过扫描找到的每个设备的回调:

// Device scan callback.
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() 
                @Override
                public void onLeScan(final BluetoothDevice device, int rssi,
                                     byte[] scanRecord) 

                    String name = device.getName();
                    if (name != null && name.compareTo(bluetoothDeviceName) == 0) 
                            Log.i("timeChar", "Device found");
                    
                
            ;

每次我要扫描我的 BLE 设备(具有 Peripheral 角色)时,大约需要 11 秒才能检测到它...

05-31 17:32:39.139 27545-9668/app I/timeChar: Begin
05-31 17:32:50.149 27545-27545/app I/timeChar: Device found

有什么办法可以减少这个时间?

【问题讨论】:

在自我宣传时,我发现这篇关于 BLE 的文章很有趣:atmosphere.anaren.com/wiki/… 感谢您的维基!我去看看:) 此链接无效。有新链接吗? 【参考方案1】:

BLE 扫描和设备广告是每隔一段时间完成的。如果广告之间的间隔很长,比如说一秒钟,那么 android 可能会错过它。为了从应用程序端解决这个问题:

//global ScanSettings settings;
ScanSettings.Builder settingBuilder = new ScanSettings.Builder();
settingBuilder.setScanMode(SCAN_MODE_LOW_LATENCY);
settings = settingBuilder.build();

您稍后将其传递给扫描方法

mBluetoothAdapter.getBluetoothLeScanner().startScan(null, settings, mLeScanCallback);

【讨论】:

【参考方案2】:

为此,您应该考虑:

    在 BLE 信标中设置的定义间隔。为了节省电池电量,您可以定义 BLE 信标发送的每个数据包之间的时间间隔(从 100 毫秒到几秒)。因此首先检查你的信标的时间间隔是多少。

    Android 操作系统中设置的时间,用于指示您已收到新数据包(回调函数)。正如 Marcin 所指出的,您可以这样做很简单:

scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY ).build();

【讨论】:

以上是关于是否可以减少扫描 BLE 设备的时间?的主要内容,如果未能解决你的问题,请参考以下文章

iOS - 在后台扫描 BLE 设备

使用基于广告服务 UUID 的扫描过滤器扫描 BLE 外围设备

iOS 应用程序在后台扫描 BLE 设备

BLE 设备应无需扫描即可连接

Android 4.4:低功耗蓝牙;无需扫描 BLE 设备即可连接

Android代码不扫描BLE设备CC2650