gpt4 book ai didi

android - java.lang.SecurityExcption : WifiService: Neither usr 10140 nor curent proces hs andrid. prmission.CHANGE_WIFI_STATE

转载 作者:行者123 更新时间:2023-11-29 23:31:45 27 4
gpt4 key购买 nike

我正在尝试为 Unity 创建一个 Android 插件以从 Android 设备获取 WiFi 信息,当我尝试在我的 Android 设备上运行该应用程序时它崩溃了。让我知道我做错了什么或完全错了,欢迎提出所有建议。

目标SDK版本:21运行在 Android 版本 8.0.0

谢谢! Manifest File Location : AndroidProjects\pluginlibrary\src\main

Manifest File

`<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 Plugin Class

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 插件类续 Android Plugin Class Continued

Error Log `

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 Code `

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");
}

`

最佳答案

这是错误的第一行也是最重要的部分:

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.

您必须复制 AndroidManifest.xml将具有权限的文件添加到您的 Unity 项目中。那应该放在 <UnityProjectName>Assets\Plugins\Android 中.如果您仍然得到上面的确切异常,则意味着您没有正确完成此操作。您必须正确拼写文件夹名称。

关于android - java.lang.SecurityExcption : WifiService: Neither usr 10140 nor curent proces hs andrid. prmission.CHANGE_WIFI_STATE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52572074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com