gpt4 book ai didi

java - wifi p2p 中的 channel

转载 作者:行者123 更新时间:2023-12-01 21:58:38 25 4
gpt4 key购买 nike

我正在尝试在android中使用wifi p2p,我正在尝试使用这个android项目:wifiDirectDemo ,但是当我运行应用程序时,android 会显示以下消息:

Unfortunately, com.example.ferch.chupipandiparty has stopped

我正在调试,当代码到达我在 Activity 中创建 channel 的行时,问题就开始了:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conexion_p2p);

intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null);

}

应用程序在类 Parcel.java 中抛出异常:

public final void writeException(Exception e) {
int code = 0;
if (e instanceof SecurityException) {
code = EX_SECURITY;
} else if (e instanceof BadParcelableException) {
code = EX_BAD_PARCELABLE;
} else if (e instanceof IllegalArgumentException) {
code = EX_ILLEGAL_ARGUMENT;
} else if (e instanceof NullPointerException) {
code = EX_NULL_POINTER;
} else if (e instanceof IllegalStateException) {
code = EX_ILLEGAL_STATE;
} else if (e instanceof NetworkOnMainThreadException) {
code = EX_NETWORK_MAIN_THREAD;
} else if (e instanceof UnsupportedOperationException) {
code = EX_UNSUPPORTED_OPERATION;
}
writeInt(code);
StrictMode.clearGatheredViolations();
if (code == 0) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RuntimeException(e);
}
writeString(e.getMessage());
}

我已在 Manifest.xml 中设置此权限:

<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />

日志猫:

12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty D/AndroidRuntime: Shutting down VM
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: FATAL EXCEPTION: main
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: Process: com.example.ferch.chupipadiparty, PID: 21442
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ferch.chupipadiparty/com.example.ferch.chupipadiparty.conexionP2P}: java.lang.SecurityException: WifiP2pService: Neither user 10115 nor current process has android.permission.ACCESS_WIFI_STATE.
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5294)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: Caused by: java.lang.SecurityException: WifiP2pService: Neither user 10115 nor current process has android.permission.ACCESS_WIFI_STATE.
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1546)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1499)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.net.wifi.p2p.IWifiP2pManager$Stub$Proxy.getMessenger(IWifiP2pManager.java:113)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.net.wifi.p2p.WifiP2pManager.getMessenger(WifiP2pManager.java:1379)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.net.wifi.p2p.WifiP2pManager.initialize(WifiP2pManager.java:886)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at com.example.ferch.chupipadiparty.conexionP2P.onCreate(conexionP2P.java:49)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5294) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
12-01 20:50:33.943 21442-21442/com.example.ferch.chupipadiparty E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

我正在做 google wifiDirect 的代码

最佳答案

可以肯定的是,在抛出错误之前就已经有了这个

ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_WIFI_STATE)

关于java - wifi p2p 中的 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34028971/

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