Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth app not working on devices running on Lollipop

I made a Bluetooth app. It works fine in Nougat, Marshmallow, Jelly Bean, KitKat but for reason, it crashes in Android Lollipop when Discover button (which discovers all the discoverable devices) is clicked.

Here is the method which gets triggered on clicking Discover button -

private void discoverDevices() {
        Log.d(TAG, "btnDiscover: Looking for unpaired devices.");

        if(mBluetoothAdapter.isDiscovering()){
            mBluetoothAdapter.cancelDiscovery();
            Log.d(TAG, "btnDiscover: Canceling discovery.");

            //check BT permissions in manifest
            checkBTPermissions();

            mBluetoothAdapter.startDiscovery();
            IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mBroadcastReceiver3, discoverDevicesIntent);
        }
        if(!mBluetoothAdapter.isDiscovering()){

            //check BT permissions in manifest
            checkBTPermissions();

            mBluetoothAdapter.startDiscovery();
            IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mBroadcastReceiver3, discoverDevicesIntent);
        }


    }

CheckBTPermissions()-

private void checkBTPermissions() {

        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
            int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
            permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
            if (permissionCheck != 0) {

                this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
            }
        }else{
            Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version 
=< LOLLIPOP.");
        }
    }

Permissions in Manifest file-

<uses-feature android:name="android.hardware.bluetooth" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />

build.gradle-

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.example.hpi5.bluetooth"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

I have not tested this app on Lollipop myself as I don't have any device running on Android L. I got to know about the crash from a friend of mine.

EDIT: I managed to arrange the logs.

07-07 19:29:00.219 30852-30852/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.hpi5.bluetooth, PID: 30852
                                                   java.lang.NoSuchMethodError: No virtual method checkSelfPermission(Ljava/lang/String;)I in class Lcom/example/hpi5/bluetooth/MainActivity; or its super classes (declaration of 'com.example.hpi5.bluetooth.MainActivity' appears in /data/app/com.example.hpi5.bluetooth-1/base.apk)
                                                       at com.example.hpi5.bluetooth.MainActivity.checkBTPermissions(MainActivity.java:243)
                                                       at com.example.hpi5.bluetooth.MainActivity.discoverDevices(MainActivity.java:230)
                                                       at com.example.hpi5.bluetooth.MainActivity.access$100(MainActivity.java:24)
                                                       at com.example.hpi5.bluetooth.MainActivity$7.onClick(MainActivity.java:189)
                                                       at android.view.View.performClick(View.java:4923)
                                                       at android.view.View$PerformClick.run(View.java:20341)
                                                       at android.os.Handler.handleCallback(Handler.java:815)
                                                       at android.os.Handler.dispatchMessage(Handler.java:104)
                                                       at android.os.Looper.loop(Looper.java:194)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5717)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
like image 872
ray an Avatar asked Apr 05 '17 14:04

ray an


People also ask

What is Lollipop update?

Welcome to Android 5.0 Lollipop—the largest and most ambitious release for Android yet! This release is packed with new features for users and thousands of new APIs for developers. It extends Android even further, from phones, tablets, and wearables, to TVs and cars.


1 Answers

The exception says it all, the method checkSelfPermission is only available on API 23 onwards. If you want to check permission with API 21, use the Android support library and ContextCompat.checkSelfPermission

Edit: Also, location permissions for Bluetooth are only required from API 23 onwards.

like image 200
Roberto Betancourt Avatar answered Sep 21 '22 07:09

Roberto Betancourt