OnBatchScanResults not called on Android BLE

Now I am using the new BLE api for Android development.

The basic idea is to use the result of a Bluetooth scan to inflate a recyclerview (list);

I followed the BLE guide on google developer

Now I have two problems: 1. The onBatchScanResults listener never starts, but onScanResult works well, is it because the scanner only senses 1 sensor nearby?

  1. my BLE scanner is much slower compared to other applications.

The following is a code snippet of two main functions.

 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 as follows:

  beaconsList = new ArrayList<BeaconsInfo>(); mAdapter = new BeaconsAdapter(beaconsList); mRecyclerView.setAdapter(mAdapter); scannBLE(true); 
+6
source share
2 answers

Regardless of whether you get package results or individual results, it depends on the scan settings.

  • To get batch results, you need to configure ScanSettings . Read the documentation for ScanSettings.Builder and try using SCAN_MODE_LOW_POWER , which will delete the results. You can also try adjusting the interval between batches using setReportDelay(long reportDelayMillis) ; You can see the blog post I wrote about the power benefits of these settings here.

  • It's not entirely clear what you mean by “my BLE scanner is much slower compared to other applications,” but it may happen that the user interface of the application is lagging because you are not updating it in the user interface thread. Try exchanging your calls for notifyDatasetChanged as follows:

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

Try setUseHardwareBatchingIfSupported(true) . This solves the problem for me on moto360 2nd gen. I think this is automatically implemented for the new API.

0
source

Source: https://habr.com/ru/post/978457/


All Articles