在WifiP2pGroup中,如何为自己设置密码

时间:2018-04-18 06:09:46

标签: android wifip2p

我在api下面植入我的代码并制作app。

https://android.googlesource.com/platform/frameworks/base/+/cd92588/wifi/java/android/net/wifi/p2p

但是我想为自己设置密码,但在WifiP2pGroup.java中,setPassphrase设置为hide,即使我使用setPassphrase函数(我发现android.jar隐藏了函数被打开),我无法更改它自己。

问题是我想公开我的wifi(没有密码),如果我不能,我想把密码改为00000000。

我该怎么做?

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.p2p.IWifiP2pManager;
import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;
import android.widget.Toast;

public final class WifiDirectShare {

private final String TAG = getClass().getSimpleName();

private Context applicationContext;
private WifiP2pManager p2pManager;
private WifiP2pManager.Channel channel;
private GroupCreatedListener listener;


public WifiDirectShare(Context context, GroupCreatedListener listener) {
    this.listener = listener;
    applicationContext = context.getApplicationContext();
}

public void start() {
    initP2p(applicationContext);
    startP2pGroup();
}

public void stop() {

    try {
        p2pManager.removeGroup(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.i(TAG, "P2p removeGroup success");
            }
            @Override
            public void onFailure(int reason) {
                Log.i(TAG, "P2p removeGroup failed. Reason : "+reason);
            }
        });
    } catch (Exception e) {/*ignore*/}
}

@SuppressLint("ServiceCast")
private void initP2p(Context context) {
    if (this.p2pManager == null || this.channel == null) {
        this.p2pManager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
        this.channel = this.p2pManager.initialize(applicationContext, context.getMainLooper(),
                new WifiP2pManager.ChannelListener() {
                    public void onChannelDisconnected() {
                        Log.i(TAG, "P2p channel initialization failed");
                    }
                });
    }
}

private void startP2pGroup() {
    Thread createGroupThread = new Thread(createGroupRunnable);
    createGroupThread.start();
}

private Runnable createGroupRunnable = new Runnable() {
    @Override
    public void run() {
        p2pManager.createGroup(channel, new WifiP2pManager.ActionListener() {
            public void onFailure(int error) {
                Log.i(TAG, "createGroup failed. Error code : "+error);
            }
            public void onSuccess() {
                p2pManager.requestGroupInfo(channel, groupInfoListener);
            }
        });
    }
};

private WifiP2pManager.GroupInfoListener groupInfoListener = new WifiP2pManager.GroupInfoListener() {
    public void onGroupInfoAvailable(WifiP2pGroup group) {
        if (group != null) {
            if(group.isGroupOwner()) {
                Log.i(TAG, "group created with ssid = "+group.getNetworkName() +
                        "\n and password = "+group.getPassphrase());
                if(listener != null)
                    listener.onGroupCreated(group.getNetworkName(), group.getPassphrase());

            }
        } else
            p2pManager.requestGroupInfo(channel, groupInfoListener);
    }
};

public interface GroupCreatedListener {
    void onGroupCreated(String ssid, String password);
}
}

mainActivity调用部分

    share = new WifiDirectShare(MainActivity.this, this);
    share.start();

0 个答案:

没有答案