You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义WifiP2pGroup密码?实现无密码或设为00000000

How to Create Open WiFi P2P Group or Set Fixed Password (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-side WifiP2pGroup object. 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:

  1. Declare Required Permissions
    Make sure your AndroidManifest.xml includes 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+ -->
    
  2. Build a Custom WifiP2pConfig for Open Group
    When calling WifiP2pManager.createGroup(), pass a WifiP2pConfig configured 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):

  1. Use Reflection to Set Passphrase in WifiP2pConfig
    The default WifiP2pConfig doesn’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
        }
    });
    
  2. Verify the Password
    After group creation, confirm the password was applied by retrieving the WifiP2pGroup object:

    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,提问作者박주언

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:35:03