我制作了蓝牙应用程序。它在Nougat
,Marshmallow
,Jelly Bean
,KitKat
中运行良好,但出于合理原因,当Lollipop
按钮(发现Discover
时,它会在Android Discover
中崩溃单击所有可发现的设备)。
以下是点击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);
}
}
按钮 -
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.");
}
}
CheckBTPermissions() -
<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" />
清单文件中的权限 -
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'
}
的build.gradle -
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)
我自己没有在Lollipop上测试过这个应用程序,因为我没有在Android L上运行任何设备。我从我的一个朋友那里了解了崩溃。
编辑:我设法安排了日志。
<toggle ng-click="toggleChanged($event)" style="v3-toggle" ng-model="myModel" on="on" off="" size="btn-xs"></toggle>
答案 0 :(得分:4)
异常说明了一切,方法checkSelfPermission仅在API 23之后可用。如果您想使用API 21检查权限,请使用Android支持库和ContextCompat.checkSelfPermission
编辑:此外,只有从API 23开始才需要蓝牙的位置权限。
答案 1 :(得分:4)
所以我以某种方式安排了日志,发现即使是在Lollipop上运行的设备,以下条件评估为真
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
//code here
}
因此,该应用在点击发现按钮时崩溃。
所以,我刚刚将上述条件改为 -
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
}
该应用程序现在在Lollipop设备上运行良好。
为什么早些时候它会在Lollipop设备上崩溃?
所以,我朋友的设备在Android版本5.1.1上运行,这就是为什么条件评估为真。由于checkSelfPermission
是在API级别23(Android M)中引入的,因此该应用无法找到它并因此崩溃。
答案 2 :(得分:1)
在API 23中添加了运行时权限系统,因此您不应该检查该API下的权限。 logcat告诉您,它无法找到API 21中不存在的方法。