Forum Discussion
harrz-s
5 years agoExplorer
SecurityException when accessing Bluetooth API
I'm trying to access the Bluetooth API within a Unity project, but I end up with a SecurityException: 01-24 20:40:47.280 26380 26405 E Unity : AndroidJavaException: java.lang.SecurityException: N...
harrz-s
4 years agoExplorer
Yeah, just found that out as well. However, your change might get overwritten when you update the package.
You can use the following script, which will re-add the bluetooth permission to the manifest. Just put it into a folder called "Editor", then it will run after the 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; } }
}
#endiftgaze
4 years agoExplorer
Hello harrz-s,
I am also running into this problem. Could you please explain where I need to put this file and what to name it?
Thank you,
tgaze
- PawSix4 years agoExplorer
So what worked for me is creating a folder called "Editor" in the "Assets" folder. Then putting the script they mentioned there. 🙂 Unity doesn't allow me to create a script in the original folder I tried which was the "Packages/Oculus XR Plugin/Editor" folder.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 8 months ago
- 1 month ago
- 2 months ago
- 3 months ago