简体   繁体   English

如何过滤广播扩展广告消息的 BLE 设备?

[英]How to filter BLE devices that are advertising extended advertising message?

I have implemented the following BLE scan callback,我已经实现了以下 BLE 扫描回调,

private final ScanCallback mScanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        Log.d(TAG,"onScanResult: " +result.toString());
        runOnUiThread(() -> {
            if (result.getDevice().getName() != null && getString(R.string.unknown_device_text).compareTo(result.getDevice().getName().toLowerCase()) != 0) {
                mLeDeviceListAdapter.addDevice(result.getDevice());
                mLeDeviceListAdapter.notifyDataSetChanged();
            }
        });
        super.onScanResult(callbackType, result);
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        Log.d(TAG,"onBatchScanResults: " +results.toString());
        super.onBatchScanResults(results);
    }

    @Override
    public void onScanFailed(int errorCode) {
        Log.d(TAG,"onScanFailed: errorCode: " +errorCode);
        super.onScanFailed(errorCode);
    }
};

However, in this callback I am not getting BLE devices that are advertising extended message.但是,在此回调中,我没有收到正在广告扩展消息的 BLE 设备。 In contrast, in same place nRF app shows extended devices in their list.相反,在同一个地方,nRF 应用程序在其列表中显示了扩展设备。

Here is my scan method,这是我的扫描方法,

private void scanLeDevice() {

    List<ScanFilter> filters = new ArrayList<>();
    ScanFilter.Builder scanFilterBuilder = new ScanFilter.Builder();
    filters.add(scanFilterBuilder.build());

    ScanSettings.Builder settingsBuilder = new ScanSettings.Builder();
    settingsBuilder.setPhy(ScanSettings.PHY_LE_ALL_SUPPORTED);

    final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            bluetoothLeScanner.stopScan(mScanCallback);
            Log.d(TAG, "scanLeDevice stopScan called");
        }
    }, SCAN_PERIOD);            

    bluetoothLeScanner.startScan(filters, settingsBuilder.build(), mScanCallback);
}

So, how can I filter and find the devices with extended advertising capabilities.那么,我如何过滤和找到具有扩展广告功能的设备。

In order to only filter extended advertisements, you need to use the setLegacy(false) method.为了只过滤扩展广告,您需要使用setLegacy(false)方法。 By default this is set to true , which is why you need to change it when setting up your scan settings.默认情况下,它设置为true ,这就是为什么您需要在设置扫描设置时更改它的原因。

Have a look at the links below for more information:-请查看以下链接以获取更多信息:-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM