首先,最好的方法是不要更改,而只是检索 WifiP2p 设置,然后使用蓝牙或 NFC 等不同的通道将它们传递给连接的旧设备(不支持 WifiP2p 的设备,因为只有你需要密码)。二维码也可以使用。
上一条消息向您展示了如何获取 SSID 和密码。密码不能更改,但是 SSID 可以。Wifi Direct 规范将 SSID 设置为“DIRECT__”,其中 xy 是设置期间随机生成的一些字母。所以你不能改变这个前缀“DIRECT”和这两个字母,因为字母和密码都是在内部库中生成的,只有一个只读副本被传递回应用程序。
但是,您可以使用反射 API 更改 SSID 前缀之后的内容。
private void openWifiDirectChannel(String name){
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null);
//Use reflection API to get the hidden method for setting the P2P network name
try {
Method m = manager.getClass().getMethod(
"setDeviceName",
WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class );
m.invoke(manager, channel, name, new WifiP2pManager.ActionListener() {
public void onSuccess() {
//Code for Success in changing name
}
public void onFailure(int reason) {
//Code to be done while name change Fails
}
});
} catch (Exception e)
e.printStackTrace();
}