如何自定义WifiP2pGroup密码?实现无密码或设为00000000
Hey there, I’ve tackled similar WiFi P2P customization hurdles before, so let’s break down how to solve your problem step by step.
Why Directly Calling setPassphrase() Doesn’t Work
First, let’s clear up why using the hidden WifiP2pGroup.setPassphrase() method (even with an unlocked android.jar) fails:
- WiFi P2P group configurations are tightly controlled by the system’s
WifiP2pService, not just the client-sideWifiP2pGroupobject. The hidden method only modifies the local object’s state, not the actual system-managed group settings. - System-level security checks prevent arbitrary password modifications after a group is created—most changes need to be applied during group initialization.
Solution 1: Create an Open (Password-Free) WiFi P2P Group
To create a public, password-free P2P group, you need to explicitly configure the security type before creating the group. Here’s how:
Declare Required Permissions
Make sure yourAndroidManifest.xmlincludes these permissions:<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" /> <!-- For Android 12+ -->Build a Custom
WifiP2pConfigfor Open Group
When callingWifiP2pManager.createGroup(), pass aWifiP2pConfigconfigured for open security:WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null); WifiP2pConfig config = new WifiP2pConfig(); config.groupOwnerIntent = 15; // Higher value increases chance of becoming group owner config.securityType = WifiP2pConfig.SECURITY_OPEN; // Enables password-free group manager.createGroup(channel, config, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { // Open group created successfully } @Override public void onFailure(int reason) { // Handle failure (e.g., security restrictions on some devices) } });Note: Some Android devices or versions may restrict open P2P groups due to security policies. If this fails, move to Solution 2.
Solution 2: Set Fixed Password (00000000)
If open groups aren’t feasible, you can force a fixed password by configuring it during group creation (instead of modifying an existing group):
Use Reflection to Set Passphrase in
WifiP2pConfig
The defaultWifiP2pConfigdoesn’t expose a public setter for passphrase, but you can use reflection to set it before creating the group:WifiP2pConfig config = new WifiP2pConfig(); config.groupOwnerIntent = 15; try { Field passphraseField = WifiP2pConfig.class.getDeclaredField("passphrase"); passphraseField.setAccessible(true); passphraseField.set(config, "00000000"); // Set security type to WPA2 PSK (required for password-protected groups) Field securityTypeField = WifiP2pConfig.class.getDeclaredField("securityType"); securityTypeField.setAccessible(true); securityTypeField.set(config, WifiP2pConfig.SECURITY_PSK); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } // Create group with the custom config manager.createGroup(channel, config, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { // Group created with fixed password } @Override public void onFailure(int reason) { // Handle creation failure } });Verify the Password
After group creation, confirm the password was applied by retrieving theWifiP2pGroupobject:manager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() { @Override public void onGroupInfoAvailable(WifiP2pGroup group) { if (group != null) { String currentPassphrase = group.getPassphrase(); // Check if it matches "00000000" } } });
Important Notes
- System Signature Requirement: On some Android versions, modifying WiFi P2P settings may require your app to be signed with the system signature. Non-system apps may face restrictions here.
- Compatibility: WiFi P2P APIs vary across Android versions—test your code on target devices to ensure it works as expected.
内容的提问来源于stack exchange,提问作者박주언

