Reputation: 119
I am trying to do bluetooth communication in unity project through android plugin and at the beginning I want to turn on bluetooth.
The java code look like this
package com.example.unityplugin;
import android.bluetooth.BluetoothAdapter;
public class PluginClass {
public static String testMessage(){
return "I AM WORKING";
}
public static String TurnOnBluetooth(){
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
return "BLUETOOTH ON";
} else {
return "WAS ON";
}
}
return "no bluetooth adapter";
}
}
And in Unity is as simple as
void Start () {
textMEsh = GetComponent<TextMesh>();
var plugin = new AndroidJavaClass("com.example.unityplugin.PluginClass");
textMEsh.text = plugin.CallStatic<string>("testMessage");
textMEsh.text = plugin.CallStatic<string>("TurnOnBluetooth");
}
So the text displaying in app changes after first method "testMessage" to "I AM WORKING" but then nothing happens and I don't really understand why. Bluetooth is not turning on and I see the following error from the log:
I/Unity: AndroidJavaException: java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither user 10069 nor current process has android.permission.BLUETOOTH_ADMIN.
How should I properly set that permission in Unity?
Upvotes: 3
Views: 2880
Reputation: 125245
That's a permission error. You need to add the bluetooth permission into Unity.
1.Go to <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk
, Copy the AndroidManifest.xml
file to your <ProjectName>Assets\Plugins\Android
.
If <ProjectName>Assets\Plugins\Android
doesn't exist yet, create it. The spelling of the folder is case sensitive and must be spelt right.
2.Open the copied Manifest file from <ProjectName>Assets\Plugins\Android
and add your manifest.
Add the following permission to it:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
3.Save the AndroidManifest modification, Build and Run.
Unity will now include the bluetooth permission in the final build.
Upvotes: 3