Android pit [8]-Android10 Bluetooth scan failed

Preface

Recently encountered a problem paradox, the development of APP in Huawei glory V20 found it impossible to scan Bluetooth devices tests. The mobile phone system Bluetooth can be scanned. Interrupted debugging and found that its scan callback was not executed... Repeated attempts failed.
At first I thought it was a problem specific to this model, and then I accidentally saw it Android10. This is also the only mobile phone currently available for testing, considering the increasingly subdivided Android and strict permission control. I opened the privacy change and finally got free. Android 10

Insert picture description here

the reason

Insert picture description here
The Bluetooth functions are as follows, you can find that common scans are required...
Insert picture description here

Solution

Modify target version

解决不了问题,就解决提出问题的人。

Since it is the subdivision of permissions brought about by the version upgrade, then we directly modify it build.gradleand change it targetSdkVersionto 27, and the problem is solved.

android {
    
    
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
    
    
        applicationId "com.example.felicitysolar_debug"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 2
        versionName "3.6"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    
    
        release {
    
    
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
    
    
        checkReleaseBuilds false
        abortOnError false
    }

}

Of course, this is obviously a slant forward!

Apply for precise positioning permission

Here comes the orthodox approach:

First, you need AndroidManifest.xmlto add the following permissions:

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

In addition, you need to dynamically apply for permissions. Here is a sample of Huawei:

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
    
    
            Log.i(TAG, "sdk < 28 Q");
            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
    
                String[] strings =
                        {
    
    Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
                ActivityCompat.requestPermissions(this, strings, 1);
            }
        } else {
    
    
            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this,
                    "android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
    
    
                String[] strings = {
    
    android.Manifest.permission.ACCESS_FINE_LOCATION,
                        android.Manifest.permission.ACCESS_COARSE_LOCATION,
                        "android.permission.ACCESS_BACKGROUND_LOCATION"};
                ActivityCompat.requestPermissions(this, strings, 2);
            }
        }

Since there is no way to force GPS positioning to be turned on, here is a soft reminder:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
    
    
            LocationManager alm = (LocationManager)BLEActivity.this.getSystemService(Context.LOCATION_SERVICE);
            if (!alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)){
    
    
                Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
            }
        }

A set of combined punches goes down and the problem is solved.

Guess you like

Origin blog.csdn.net/weixin_40774605/article/details/104860565