在开发Quest2应用的时候需要用到蓝牙权限,但是一直报错AndroidJavaException: java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10076 nor current process has android.permission.BLUETOOTH.
01-24 20:40:47.280 26380 26405 E Unity : AndroidJavaException: java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10076 nor current process has android.permission.BLUETOOTH.
01-24 20:40:47.280 26380 26405 E Unity : java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10076 nor current process has android.permission.BLUETOOTH.
01-24 20:40:47.280 26380 26405 E Unity : at android.os.Parcel.readException(Parcel.java:1692)
01-24 20:40:47.280 26380 26405 E Unity : at android.os.Parcel.readException(Parcel.java:1645)
01-24 20:40:47.280 26380 26405 E Unity : at android.bluetooth.IBluetooth$Stub$Proxy.isEnabled(IBluetooth.java:864)
01-24 20:40:47.280 26380 26405 E Unity : at android.bluetooth.BluetoothAdapter.isEnabled(BluetoothAdapter.java:622)
01-24 20:40:47.280 26380 26405 E Unity : at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
01-24 20:40:47.280 26380 26405 E Unity : at com.unity3d.player.UnityPlayer.access$300(Unknown Source)
01-24 20:40:47.280 26380 26405 E Unity : at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source)
01-24 20:40:47.280 26380 26405 E Unity : at android.os.Handler.dispatchMessage(Handler.java:98)
01-24 20:40:47.280 26380 26405 E Unity : at android.os.Looper.loop(Looper.java:154)
01-24 20:40:47.280 26380 26405 E Unity : at com.unity3d.player.UnityPlayer$e.run(Unknown Source)
01-24 20:40:47.280 26380 26405 E Unity : at UnityEngine.AndroidJNISafe.CheckException () [0x0008d] in /Users/bokken/buildslave/unity/build/Modules/AndroidJNI/AndroidJNISafe
看报错是缺少蓝牙权限,但是明明在AndroidManifest.xml加上了蓝牙权限了呀,后来查了下,原来是Oculus商店禁止提交蓝牙应用,所以Unity在打包的时候自动将蓝牙权限移除了。这个可以在Unity打包的apk解包后看到权限已经不见了。
如何解决呢,也很简单,使用以下脚本,它将在打包的时候自动将被删的蓝牙权限重新添加到AndroidManifest中。只需将其放入名为“Editor”的文件夹中,然后它将在 OculusBuildProcessor 之后运行。
using System.Xml;
using UnityEditor;
using UnityEditor.Android;
using UnityEditor.XR.Oculus;
#if UNITY_ANDROID
internal class OculusManifestBTFixer : IPostGenerateGradleAndroidProject {
static readonly string k_AndroidURI = "http://schemas.android.com/apk/res/android";
static readonly string k_AndroidManifestPath = "/src/main/AndroidManifest.xml";
void CreateNameValueElementsInTag(XmlDocument doc, string parentPath, string tag,
string firstName, string firstValue, string secondName = null, string secondValue = null, string thirdName = null, string thirdValue = null) {
var xmlNodeList = doc.SelectNodes(parentPath + "/" + tag);
// don't create if the firstValue matches
foreach (XmlNode node in xmlNodeList) {
foreach (XmlAttribute attrib in node.Attributes) {
if (attrib.Value == firstValue) {
return;
}
}
}
XmlElement childElement = doc.CreateElement(tag);
childElement.SetAttribute(firstName, k_AndroidURI, firstValue);
if (secondValue != null) {
childElement.SetAttribute(secondName, k_AndroidURI, secondValue);
}
if (thirdValue != null) {
childElement.SetAttribute(thirdName, k_AndroidURI, thirdValue);
}
var xmlParentNode = doc.SelectSingleNode(parentPath);
if (xmlParentNode != null) {
xmlParentNode.AppendChild(childElement);
}
}
public void OnPostGenerateGradleAndroidProject(string path) {
if (!OculusBuildTools.OculusLoaderPresentInSettingsForBuildTarget(BuildTargetGroup.Android))
return;
var manifestPath = path + k_AndroidManifestPath;
var manifestDoc = new XmlDocument();
manifestDoc.Load(manifestPath);
string nodePath = "/manifest";
CreateNameValueElementsInTag(manifestDoc, nodePath, "uses-permission", "name", "android.permission.BLUETOOTH");
manifestDoc.Save(manifestPath);
}
public int callbackOrder { get { return 20000; } }
}
#endif
举一反三:如果你希望在打包后自动修改或添加东西到AndroidManifest,也可以使用如下方式。
参考链接:SecurityException when accessing Bluetooth API - Oculus Community - 850586
|