简体   繁体   English

Android BLE中未调用onBatchScanResults

[英]onBatchScanResults is not called in Android BLE

I am now using the new BLE api in android developing. 我现在在android开发中使用新的BLE api。

Basic idea is using bluetooth scanning result to inflate the recyclerview(list); 基本思路是使用蓝牙扫描结果来扩充recyclerview(列表);

I followed the BLE guide on google developer 我按照谷歌开发者的BLE指南

Now I have two problem: 1. onBatchScanResults listener is never triggered, but onScanResult works well, is that because the scanner only sense 1 sensor nearby? 现在我有两个问题: onBatchScanResults监听器永远不会被触发,但是onScanResult运行良好,是因为扫描仪只能感知附近的1个传感器吗?

  1. my BLE scanner is much slower compared with other applications. 与其他应用程序相比,我的BLE扫描仪要慢得多。

The following is the two core functions' code snippet. 以下是两个核心功能的代码片段。

private void scanBLE(boolean enable) {
    final BluetoothLeScanner mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    if (enable) {
        mScanning = true;
        mBluetoothLeScanner.startScan(mScanCallback);        
    } else {
        if (mScanning) {
            mScanning = false;
            mBluetoothLeScanner.stopScan(mScanCallback);
        }
    }

    Log.i(TAG, "now the scanning state is" + mScanning);
}

// Device scan callback.
private ScanCallback mScanCallback =
        new ScanCallback() {
    public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
        addBeaconTolist(result, beaconsList);
        mAdapter.notifyDataSetChanged();
    };

    public void onScanFailed(int errorCode) {
        Log.i(TAG, "error code is:" + errorCode);
    };

    public void onBatchScanResults(java.util.List<android.bluetooth.le.ScanResult> results) {
        Log.i(TAG, "event linstener is called!!!!");
        Log.i(TAG, "batch result are:" + results);
        beaconsList.clear();
        for (int i = 0; i < results.size(); i++) {
            ScanResult result = results.get(i);
            addBeaconTolist(result, beaconsList);
        }
        mAdapter.notifyDataSetChanged();
    };

};

in MainFragment is like following: 在MainFragment中如下:

    beaconsList = new ArrayList<BeaconsInfo>();

    mAdapter = new BeaconsAdapter(beaconsList);
    mRecyclerView.setAdapter(mAdapter);

    scannBLE(true);

Whether or not you get batch results or individual results depends on your scan settings. 是否获得批处理结果或单个结果取决于您的扫描设置。

  1. In order to get batch results, you need to adjust the ScanSettings . 要获得批处理结果,您需要调整ScanSettings Check out the documentation for the ScanSettings.Builder , and try using SCAN_MODE_LOW_POWER , which batches up results. 查看ScanSettings.Builder的文档,并尝试使用SCAN_MODE_LOW_POWER来批量处理结果。 You can also try adjusting the batch interval with setReportDelay(long reportDelayMillis) ; 您还可以尝试使用setReportDelay(long reportDelayMillis)调整批处理间隔; You can see a blog post I wrote about the power benefits of these settings here. 你可以在这里看到我写的关于这些设置的强大功能的博客文章

  2. It's not totally clear what you mean by "my BLE scanner is much slower compared with other applications", but it may be that the app's UI lags because you are not updating it on the UI thread. 你的意思并不完全清楚“我的BLE扫描仪与其他应用程序相比要慢得多”,但可能是应用程序的UI滞后,因为你没有在UI线程上更新它。 Try wrapping your calls to notifyDatasetChanged like this: 尝试将您的调用包装到notifyDatasetChanged,如下所示:

     runOnUiThread(new Runnable() { @Override public void run() { mAdapter.notifyDataSetChanged(); } }); 

Try setUseHardwareBatchingIfSupported(true) . 尝试setUseHardwareBatchingIfSupported(true) This solves the problem for me on moto360 2nd gen. 这解决了我在moto360第二代的问题。 I think this is auto implemented for newer API. 我认为这是针对较新的API自动实现的。

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

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