Я пытаюсь создать плагин Android для Unity, чтобы получать информацию о WiFi с устройства Android. Когда я пытаюсь запустить приложение на моем устройстве Android, происходит сбой.Дайте мне знать, если я делаю что-то не так или все неправильно, все предложения приветствуются.
Целевая версия SDK: 21 работает на Android версии 8.0.0
Спасибо! Местоположение файла манифеста : AndroidProjects \ pluginlibrary \ src \ main
Файл манифеста
`<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pluginlibrary" >
<!--dangerous permission-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>`
Класс плагинов Android
package com.example.pluginlibrary;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;
import com.unity3d.player.UnityPlayerActivity;
import java.util.List;
public class PluginTest extends UnityPlayerActivity {
private Context context;
WifiManager wifiManager;
private static PluginTest instance;
public PluginTest() {
this.instance = this;
}
public static PluginTest instance() {
if (instance == null) {
instance = new PluginTest();
}
return instance;
}
public void setContext(Context context) {
this.context = context;
}
public void ShowMessage(String message) {
Toast.makeText(this.context, message, Toast.LENGTH_SHORT).show();
}
public String GetWifiInformation() {
Log.d("Unity", "GetWifiInformation: ");
wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
BroadcastReceiver wifiScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
boolean success = intent.getBooleanExtra(
WifiManager.EXTRA_RESULTS_UPDATED, false);
if (success) {
scanSuccess();
} else {
// scan failure handling
scanFailure();
}
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
context.registerReceiver(wifiScanReceiver, intentFilter);
boolean success = wifiManager.startScan();
if (!success) {
// scan failure handling
scanFailure();
}
Log.d("Unity", "Getring return value");
return "";
}
private void scanSuccess() {
List<ScanResult> results = wifiManager.getScanResults();
// get the BSSID from the scan results
}
private void scanFailure() {
// handle failure: new scan did NOT succeed
// consider using old scan results: these are the OLD results!
List<ScanResult> results = wifiManager.getScanResults();
}
public void GetMessageFromUnity() {
Log.d("Unity", "GetMessageFromUnity: ");
}
}
`
Android Plugin Class continued
Класс плагинов Android Продолжение
Журнал ошибок `
09-29 13:48:00.339 25438-25455/? A/zygote: java_vm_ext.cc:504] JNI DETECTED ERROR IN APPLICATION: JNI FindClass called with pending exception java.lang.SecurityException: WifiService: Neither user 10140 nor current process has android.permission.CHANGE_WIFI_STATE.
java_vm_ext.cc:504] at void android.os.Parcel.readException(int, java.lang.String) (Parcel.java:1959)
java_vm_ext.cc:504] at void android.os.Parcel.readException() (Parcel.java:1905)
java_vm_ext.cc:504] at void android.net.wifi.IWifiManager$Stub$Proxy.startScan(android.net.wifi.ScanSettings, android.os.WorkSource, java.lang.String) (IWifiManager.java:1607)
java_vm_ext.cc:504] at boolean android.net.wifi.WifiManager.startScan(android.os.WorkSource) (WifiManager.java:1838)
java_vm_ext.cc:504] at boolean android.net.wifi.WifiManager.startScan() (WifiManager.java:1829)
java_vm_ext.cc:504] at java.lang.String com.example.pluginlibrary.PluginTest.GetWifiInformation() (PluginTest.java:84)
java_vm_ext.cc:504] at boolean com.unity3d.player.UnityPlayer.nativeRender() ((null):-2)
java_vm_ext.cc:504] at boolean com.unity3d.player.UnityPlayer.c(com.unity3d.player.UnityPlayer) ((null):-1)
java_vm_ext.cc:504] at boolean com.unity3d.player.UnityPlayer$e$2.queueIdle() ((null):-1)
java_vm_ext.cc:504] at android.os.Message android.os.MessageQueue.next() (MessageQueue.java:394)
java_vm_ext.cc:504] at void android.os.Looper.loop() (Looper.java:142)
java_vm_ext.cc:504] at void com.unity3d.player.UnityPlayer$e.run() ((null):-1)
java_vm_ext.cc:504]
java_vm_ext.cc:504] in call to FindClass
java_vm_ext.cc:504] from boolean com.unity3d.player.UnityPlayer.nativeRender()
java_vm_ext.cc:504] "UnityMain" prio=5 tid=13 Runnable
java_vm_ext.cc:504] | group="main" sCount=0 dsCount=0 flags=0 obj=0x181c05f8 self=0xdd87ea00
java_vm_ext.cc:504] | sysTid=25455 nice=0 cgrp=default sched=0/0 handle=0xcffff970
java_vm_ext.cc:504] | state=R schedstat=( 692579348 31631464 1032 ) utm=53 stm=16 core=6 HZ=100
java_vm_ext.cc:504] | stack=0xcfefd000-0xcfeff000 stackSize=1038KB
java_vm_ext.cc:504] | held mutexes= "mutator lock"(shared held)
java_vm_ext.cc:504] native: #00 pc 002c4afb /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+130)
java_vm_ext.cc:504] native: #01 pc 003565a1 /system/lib/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMapb+200)
java_vm_ext.cc:504] native: #02 pc 00352a73 /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMapb+34)
java_vm_ext.cc:504] native: #03 pc 0022fff5 /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+736)
java_vm_ext.cc:504] native: #04 pc 002303eb /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+58)
java_vm_ext.cc:504] native: #05 pc 000d286f /system/lib/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+46)
java_vm_ext.cc:504] native: #06 pc 000d2407 /system/lib/libart.so (_ZN3art11ScopedCheck11CheckThreadEP7_JNIEnv+354)
java_vm_ext.cc:504] native: #07 pc 000d0c05 /system/lib/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE+604)
java_vm_ext.cc:504] native: #08 pc 000bd8f7 /system/lib/libart.so (_ZN3art8CheckJNI9FindClassEP7_JNIEnvPKc+450)
java_vm_ext.cc:504] native: #09 pc 00452378 /data/app/com.CJ.AndroidPLugin-BqvsV8_-skC366qUka3LQA==/lib/arm/libunity.so (???)
java_vm_ext.cc:504] native: #10 pc 00442108 /data/app/com.CJ.AndroidPLugin-BqvsV8_-skC366qUka3LQA==/lib/arm/libunity.so (???)
java_vm_ext.cc:504] native: #11 pc 0000296b /data/app/com.CJ.AndroidPLugin-BqvsV8_-skC366qUka3LQA==/oat/arm/base.odex (Java_com_unity3d_player_UnityPlayer_nativeRender__+74)
java_vm_ext.cc:504] at com.unity3d.player.UnityPlayer.nativeRender(Native method)
java_vm_ext.cc:504] at com.unity3d.player.UnityPlayer.c(unavailable:-1)
java_vm_ext.cc:504] at com.unity3d.player.UnityPlayer$e$2.queueIdle(unavailable:-1)
java_vm_ext.cc:504] at android.os.MessageQueue.next(MessageQueue.java:394)
java_vm_ext.cc:504] at android.os.Looper.loop(Looper.java:142)
java_vm_ext.cc:504] at com.unity3d.player.UnityPlayer$e.run(unavailable:-1)
java_vm_ext.cc:504]
Код Unity `
public class PluginWrapper : MonoBehaviour {
private AndroidJavaObject androidInstance = null;
private AndroidJavaObject activityContext = null;
// Use this for initialization
void Start()
{
if (androidInstance == null)
{
using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}
using (AndroidJavaObject pluginClass = new AndroidJavaObject("com.example.pluginlibrary.PluginTest"))
{
if (pluginClass != null)
{
androidInstance = pluginClass.CallStatic<AndroidJavaObject>("instance");
androidInstance.Call("setContext", activityContext);
androidInstance.Call("GetMessageFromUnity");
androidInstance.Call<string>("GetWifiInformation");
activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
androidInstance.Call("ShowMessage", "Plugin Active!");
}));
}
}
}
//androidJavaClass = new AndroidJavaObject("com.example.pluginlibrary.PluginTest");
//androidJavaClass.Call("GetMessageFromUnity");
}
`