Bluetooth application does not work on devices running on Lollipop

I made a bluetooth application. It works great in Nougat , Marshmallow , Jelly Bean , KitKat , but for some reason it crashes in Android Lollipop when the Discover button is pressed (which detects all detected devices).

Here is the method that starts when the Discover button is clicked -

 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 the 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 application on Lollipop myself, as I do not have a device running Android L. I found out about the crash of my friend.

EDIT: I was able 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) 
+7
java android bluetooth
source share
3 answers

So, I sorted the logs somehow and even found for devices running on Lollipop, the following condition evaluated true

 if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){ //code here } 

and because of this, the application crashed when the Discover button was clicked.

So, I just changed this condition to

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ } 

Now the application works fine on Lollipop devices.

Why did it crash into Lollipop devices before?

So, my mobile device was running on Android version 5.1.1, and therefore the condition evaluated true. And since checkSelfPermission was introduced in API 23 (Android M), the application could not find it and thus crashed.

+4
source share

The exception says this, the checkSelfPermission method is only available by API 23. If you want to check the resolution using API 21, use the Android support library and ContextCompat.checkSelfPermission

Edit: In addition, hosted permission for Bluetooth is only required from API 23 onwards.

+4
source share

The Runtime API system was added in API 23, so you should not check permissions in this API. And logcat tells you that it cannot find a method that did not exist in API 21.

+1
source share

All Articles