3

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?

5
  • "but then nothing happens and i dont really understand why." What's supposed to happen? By the way, it would be better to make your TurnOnBluetooth function return bool instead of string unless you plan to make it return something else in the future.
    – Programmer
    Apr 15, 2018 at 15:14
  • My unity app is changing text displayed on a phone screen based on java string return. So calling testMessage() is changing it to " I AM WORKING" But the TurnOnBluetooth() is not changing anything so there is probably some error there. Apr 15, 2018 at 15:25
  • 1
    Check if there is a log when you this. Maybe a log with an error
    – Programmer
    Apr 15, 2018 at 15:29
  • okay so ther is this exception I/Unity: AndroidJavaException: java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither user 10069 nor current process has android.permission.BLUETOOTH_ADMIN. But i cant find how should i properly set that permission in unity? Apr 15, 2018 at 16:58
  • I was expecting that to be the problem but was waiting for you to confirm before posting answer. Check my answer.
    – Programmer
    Apr 15, 2018 at 17:05

1 Answer 1

3

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.

3
  • See this github page if you don't know which tag to place the permission in. It shows what the complete permission should look like.
    – Programmer
    Apr 15, 2018 at 17:18
  • 1
    Thank you very much. It works and also i have learned how to debug. Apr 15, 2018 at 17:24
  • Knowing how to debug is very important when making Unity Android plugins. Glad I was able to help and happy coding!
    – Programmer
    Apr 15, 2018 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.