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?

Programmer
  • 121,791
  • 22
  • 236
  • 328
user3102664
  • 95
  • 1
  • 2
  • 7
  • *"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 '18 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. – user3102664 Apr 15 '18 at 15:25
  • 1
    [Check](https://stackoverflow.com/questions/44690357/how-to-create-and-read-log-on-android-devices/44690501#44690501) if there is a log when you this. Maybe a log with an error – Programmer Apr 15 '18 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? – user3102664 Apr 15 '18 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 '18 at 17:05

1 Answers1

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • See [this](https://github.com/androidthings/sample-bluetooth-audio/blob/master/audio-sink/src/main/AndroidManifest.xml) 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 '18 at 17:18
  • 1
    Thank you very much. It works and also i have learned how to debug. – user3102664 Apr 15 '18 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 '18 at 17:27