wifi p2p中的频道

时间:2015-12-01 19:55:50

标签: java android wifi-direct

我正在尝试在android中使用wifi p2p,我正在尝试使用这个android proyect:wifiDirectDemo,但是当我运行应用程序android时告诉我这条消息:

  

不幸的是,com.example.ferch.chupipandiparty已停止

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

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的代码作为

2 个答案:

答案 0 :(得分:2)

在抛出错误之前有这个,确定

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

答案 1 :(得分:1)

这似乎是一个许可问题。你的stacktrace清楚地说明了这一点:

  

用户10115和当前进程都没有   android.permission.ACCESS_WIFI_STATE