Ответ: Вызов сторонних методов Android
Разобрался кароче я с этой хренью почти, пришлось писать на Java(.
Скрипт JavaClass:
PHP код:
package org.example.ScriptBridge;
import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle;
import android.util.Log; import java.io.File;
public class JavaClass { private Activity mActivity; public JavaClass(Activity currentActivity) { Log.i("JavaClass", "Constructor called with currentActivity = " + currentActivity); mActivity = currentActivity; } // we could of course do this straight from native code using JNI, but this is an example so.. ;) public String getActivityCacheDir() { // calling Context.getCacheDir(); // http://developer.android.com/reference/android/content/Context.html#getCacheDir() // File cacheDir = mActivity.getCacheDir(); String path = cacheDir.getPath(); Log.i("JavaClass", "getActivityCacheDir returns = " + path); return path; } public boolean initBluetooth(){ BluetoothAdapter BT = BluetoothAdapter.getDefaultAdapter(); if (BT == null) { return false; } if(!BT.isEnabled()) { Intent enableBtIntent = new Intent(BT.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); //Toast.makeText(getApplicationContext(), "Enabling Bluetooth!!", Toast.LENGTH_LONG).show(); } return true; } }
Что бы его собрать пришлось скачать ANT http://ant.apache.org
*замечу что ANT не собирал почему-то с startActivityForResult;
C# модифицировал в(100% с косяками):
PHP код:
using UnityEngine; using System.Collections; using System.Runtime.InteropServices; using System;
public class CallJavaCode : MonoBehaviour {
private IntPtr JavaClass; private int btOpen; void Start () { // attach our thread to the java vm; obviously the main thread is already attached but this is good practice.. JavaVM.AttachCurrentThread();
// first we try to find our main activity.. IntPtr cls_Activity = JNI.FindClass("com/unity3d/player/UnityPlayer"); int fid_Activity = JNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;"); IntPtr obj_Activity = JNI.GetStaticObjectField(cls_Activity, fid_Activity); Debug.Log("obj_Activity = " + obj_Activity); // create a JavaClass object... IntPtr cls_JavaClass = JNI.FindClass("org/example/ScriptBridge/JavaClass"); int mid_JavaClass = JNI.GetMethodID(cls_JavaClass, "<init>", "(Landroid/app/Activity;)V"); IntPtr obj_JavaClass = JNI.NewObject(cls_JavaClass, mid_JavaClass, obj_Activity); Debug.Log("JavaClass object = " + obj_JavaClass);
// create a global reference to the JavaClass object and fetch method id(s).. JavaClass = JNI.NewGlobalRef(obj_JavaClass); btOpen = JNI.GetMethodID(cls_JavaClass, "initBluetooth", "()Z"); // Debug.Log("JavaClass global ref = " + JavaClass); // Debug.Log("JavaClass method id = " + getActivityCacheDir); }
private string cacheDir = "Push to BT test"; void OnGUI () { if (GUI.Button(new Rect (15, 125, 450, 100), cacheDir)) { bool cache = BTOPEN(); cacheDir = cache.ToString(); } } private bool BTOPEN() { // again, make sure the thread is attached.. JavaVM.AttachCurrentThread();
int ret = JNI.CallBooleanMethod(JavaClass, btOpen); if(ret == 0) return false; return true; }
}
Прога запукается, но вылетает при нажатии на кнопку.
Теперь вопрос, как все-таки это сделать?(Хотя бы включить BT)
JDKmans,помогите;)
|