Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Frusciante · Jun 04, 2015 at 02:56 PM · androidunity 5pluginjava

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges