androidwifi-direct

How do I force android WiFi Direct group owner to use 2.4 GHz instead of 5 GHz


I've been trying to connect WiFi legacy devices to my group owner using WiFi Direct. The legacy devices only support 2.4 GHz so when I try to scan APs from the devices they don't find the android phone group owner. I do however see the group owner from my desktop and other devices. I've done a iw dev wlo1 scan on my Linux machine where the only difference I can see is the frequency of the group owner is at freq: 5500. If I place the android device in AP hotspot mode the frequency switches over to 2.4 GHz where the devices also find this on scanning.

Can I toggle the band in code? There's a builder class android.net.wifi.p2p.WifiP2pConfig.Builder https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pConfig.Builder that seems to have this functionality but I can't access it since it's a new release API level 29. Any advice would be appreciated, I don't see why the bands aren't settable.

Used to create the group owner:

private void createGroupOwner() {
    manager.createGroup(channel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Toast.makeText(MainActivity.this, "Group Created", Toast.LENGTH_SHORT).show();
            manager.requestGroupInfo(channel, getGroupOwner());
        }

        @Override
        public void onFailure(int reason) {
            Toast.makeText(MainActivity.this, "Failed to connect", Toast.LENGTH_SHORT).show();
        }
    });
}

I would like to add a config such as manager.createGroup(channel, config, ... but doesn't seem the band can be set in the config for API < 29. I've tried on a low-end Android device and the device uses 2.4GHz for the WiFi Direct network but on a high-end device is used 5GHz is used.


Solution

  • You can change the frequency of the Group SoftAP by changing the channel it's operating on.

    2.4GHz band uses 1-11 Channels (Depends on the country).

    5GHz band uses 32 and above.

    You can achieve this by using reflection to access the setWifiP2pChannels() which is a hidden function.

    Here is the code i use to set wifi channels:

    Method setWifiP2pChannels = wifiP2pManager.getClass()
                        .getMethod("setWifiP2pChannels", WifiP2pManager.Channel.class, int.class, int.class, WifiP2pManager.ActionListener.class);
                setWifiP2pChannels.invoke(wifiP2pManager, p2pChannel, 0, channel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        Log.d(TAG, "Changed channel (" + channel + ") succeeded");
                    }
    
                    @Override
                    public void onFailure(int reason) {
                        Log.e(TAG, "Changed channel (" + channel + ")  failed");
                    }
                });
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                Log.e(TAG, "Changed channel (" + channel + ")  failed(EXCEPTION) ", e);
            }
    

    Make sure that the Group AP is running before you change the channel.