我创建了一个启用蓝牙并发现其他设备的应用程序。 在清单中我有以下内容:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
但是,在设备上存在以下异常:
11-20 08:08:47.766: E/AndroidRuntime(9380): FATAL EXCEPTION: main
11-20 08:08:47.766: E/AndroidRuntime(9380): java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10111 nor current process has android.permission.BLUETOOTH.
还有什么我必须添加到Manifest以便它可以工作?
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cajun.meet"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CajunMeetActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyULCard"
android:label="@string/app_name">
</activity>
<activity android:name=".MyULContacts"
android:label="@string/app_name">
</activity>
<service android:name = ".BluetoothExchange" android:exported="true" android:enabled="true">
</service>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</application>
</manifest>
答案 0 :(得分:10)
尝试在<application>
标记之外移动权限。如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cajun.meet"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CajunMeetActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyULCard"
android:label="@string/app_name">
</activity>
<activity android:name=".MyULContacts"
android:label="@string/app_name">
</activity>
<service android:name = ".BluetoothExchange" android:exported="true" android:enabled="true">
</service>
</application>
</manifest>
答案 1 :(得分:2)
将uses-permission标记移到application元素之外。 Uses-permission是清单的子元素,而不是应用程序。查看完整结构here。
答案 2 :(得分:2)
将其移出应用程序:
</application>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>
uses-permission
<manifest>
是{{1}}
答案 3 :(得分:1)
如果使用mashmallow版本的android,则添加此代码:
这个错误发生在安全现在在api 23(mashmallow)版本android改进了安全性他们要求许可
if (android.os.Build.VERSION.SDK_INT >= 23) {
// only for gingerbread and newer versions
String permission = Manifest.permission.READ_PHONE_STATE;
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
} else {
// Add your function here which open camera
}
} else {
// Add your function here which open camera
}
然后添加此方法 onRequestPermissionsResult
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED{ } else { Toast.makeText(getApplication(), "Permission required",Toast.LENGTH_LONG).show(); } return; } }}