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);
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)