- Home /
Unity 5 seems to can't find my Android Plugin
So, I wanted to create an Android Plugin for Unity to use Bluetooth and here is what I did:
Crated an Android Project in Eclipse with minimum API 8, target API 21 and compile with API 22
Wrote this class
package com.example.pluginabau;
import java.util.HashMap;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
public class DeviceHandler {
private BluetoothAdapter myBluetoothAdapter;
private HashMap<String, RobotDevice> pairedDevices = new HashMap<String, RobotDevice>();
public DeviceHandler()
{
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> tmp = myBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : tmp)
{
if (device.getName().startsWith("HC"))
{
pairedDevices.put(device.getAddress(), new RobotDevice(device));
}
}
connectBT();
}
public void connectBT()
{
if (!myBluetoothAdapter.isEnabled())
{
myBluetoothAdapter.enable();
}
}
public String prova() {
String str = "";
for (BluetoothDevice device : myBluetoothAdapter.getBondedDevices()) {
str += device.getName() + 0;
}
return str;
}
}
Checked the project as a library (I also had to include "appcompat_v7" as well.
Exported the project as .jar
In Unity 5 created the folders Assets->Plugins->Android and put the .jar into Android folder.
Set Project Settings->Editor->Device to Any Android Device
Created this C# script
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class Prova : MonoBehaviour {
// Use this for initialization
void Start () {
AndroidJavaObject jo = new AndroidJavaObject("com.example.pluginabau.DeviceHandler");
string myText= jo.Call<string>("prova");
TextMesh mesh= GameObject.Find("New Text").GetComponent<TextMesh>();
mesh.text = myText;
}
// Update is called once per frame
void Update () {
}
}
I simply want my 3D Text object set to the value returned by the java method "prova". I'm also using Unity Remote 4 on my Nexus 5 (Android version 5.1.1) for testing porpuse, but everytime i press Play i get this error on the creation of the AndroidJavaObject:
Exception: JNI: Init'd AndroidJavaClass with null ptr!
UnityEngine.AndroidJavaClass..ctor (IntPtr jclass) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:539)
UnityEngine.AndroidJavaObject.get_JavaLangClass () (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:517)
UnityEngine.AndroidJavaObject.FindClass (System.String name) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:508)
UnityEngine.AndroidJavaObject._AndroidJavaObject (System.String className, System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:110)
UnityEngine.AndroidJavaObject..ctor (System.String className, System.Object[] args) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/AndroidJava.gen.cs:17)
Other things that may be useful to know:
USB Debugging and Allow Mock Locations options are enabled
Device is correctly recognized (checked on Eclipse and with prompt command "adb devices")
I also tried to build the apk and to run it directly on the device, but nothing changes
The project was originally created on Unity 4.x and upgraded when I opened it with Unity 5.
Any ideas on what the problem can be?
Thank you very much in advance
Your answer
Follow this Question
Related Questions
Android/Unity/Eclipse Help 0 Answers
JDK invalid HOME Deploy for Android on Unity 5.5.4. 0 Answers
use own sound recorder android library in unity 1 Answer
Failed to re-package resources after adding unity in-app purchase service 1 Answer
Admob plugin error: Java.Lang.ClassNotFoundException: com.google.android.gms.ads.mobileads 2 Answers