gogoWebsite

Android BLE Bluetooth cannot scan data and solutions

Updated to 2 days ago

I have encountered many pitfalls during the development of Bluetooth Low Energy (BLE). As a record, I will summarize the problems encountered in the development process about turning on scanning.

During the development of low-power Bluetooth, due to the continuous upgrade and optimization of the Android system, the development of BLE will also encounter different problems during the upgrade process. A summary of the problem of opening scanning is as follows:

  • Unsuccessful problem of turning off the scan and then turning on the scan during development

During the development process, you will inevitably encounter the problem of turning off scan and opening scan and repeating the operation, but at some times, the problem of not being able to turn on the scan after closing.

E/: App '' is scanning too frequently 

The problem at this time is that you enable the scanning function too frequently, which causes the system to be unable to bear it. In this case, you need to stop for at least 2 seconds after turning off the scanning, and then call the enable scanning method again, and the activation method will be successfully enabled.

  • Issues that no data can be scanned after turning on Bluetooth scanning in Android 6.0 and above

It can run perfectly in versions before Android 6.0, but when it is replaced by mobile phones with Android 6.0 and above, it cannot search for data when running.

This is because Bluetooth Low Energy has added distance detection function in Android 6.0 and above systems, so the positioning function permission needs to be enabled when scanning, and in higher versions, the precise positioning permission needs to be enabled.

 

    <uses-permission android:name=".ACCESS_COARSE_LOCATION" />
    <uses-permission android:name=".ACCESS_FINE_LOCATION" />

Dynamic permissions are also required in the code

//Configuration is also required in the manifest file
         if (.SDK_INT >= Build.VERSION_CODES.M &&
                 checkSelfPermission(.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                 checkSelfPermission(.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                 checkSelfPermission() != PackageManager.PERMISSION_GRANTED ) {
             requestPermissions(new String[]{.ACCESS_COARSE_LOCATION, .ACCESS_FINE_LOCATION,
                             ,}, PERMISSIONS_REQUEST_CODE_ACCESS);
         }

After solving various permission problems, there is no problem scanning before Android 6.0, and the problem of not scanning data afterwards will be solved.

  • Scan problem that the background mode of Android 8.1 and above system cannot be enabled

In Android 8.1 and above systems, there is no problem in scanning Bluetooth under normal conditions, but when the App is in the background, the scanning method cannot be enabled and the following prompts are provided

        : Cannot start unfiltered scan in screen-off. This scan will be resumed later: 9

This is because the scan method you enable does not set a scan filter. If you enable scanning in background mode in Android 8.1 and above, you must associate the scan filter to run perfectly in background mode.

//Set Bluetooth scanning filter collection
     private List<ScanFilter> scanFilterList;
     //Set Bluetooth scanning filter
     private scanFilterBuilder;
     //Set Bluetooth scanning settings
     private scanSettingBuilder;

  
     private List<ScanFilter> buildScanFilters() {
         scanFilterList = new ArrayList<>();
         // Filter the device you want to connect to through the service uuid Filter Search GATT service UUID
         scanFilterBuilder = new ();
         ParcelUuid parcelUuidMask = ("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
         ParcelUuid parcelUuid = ("00001800-0000-1000-8000-00805f9b34fb");
         (parcelUuid, parcelUuidMask);
         (());
         return scanFilterList;
     }

     private ScanSettings buildScanSettings() {
         scanSettingBuilder = new ();
         //Set the scanning mode of Bluetooth LE scan.
         //Scan with the highest duty cycle.  It is recommended to use this mode to run in the foreground only when the application is in this mode
         (ScanSettings.SCAN_MODE_LOW_LATENCY);
         //Set the matching mode of the Bluetooth LE scanning filter hardware matching
         //In active mode, hw determines the match faster even if the signal strength is weak. There are few sightings/matches over a period of time.
         (ScanSettings.MATCH_MODE_AGGRESSIVE);
         //Set the callback type of Bluetooth LE scan
         // Trigger a callback for each Bluetooth ad that matches the filtering criteria.  If no filter is active, all ad packages are reported
         (ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
         return ();
     }


     //Call the scanning method to enable
     ().startScan(buildScanFilters(), buildScanSettings(), mLeScanCallback);

Set the corresponding parameter settings according to the actual situation, and call the scanning method that adds filter settings and scan settings, that is, the scanning function can still be enabled in the background state.

This article records the problems encountered in different versions of the opening scan encountered during the development process and the solutions.