How do I force android WiFi Direct group owner to use 2.4 GHz instead of 5 GHz
0
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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.
Answers
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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.
-----------------------------------------------------------------------
saikrishna279 led me in the right direction. However, creating the AP before changing the channel didn't work for me. The frequency change was ignored.
Create the group owner after adjusting the frequency like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | try { int channel_setting = 1 ; Method setWifiP2pChannels = manager.getClass().getMethod( "setWifiP2pChannels" , WifiP2pManager.Channel. class , int . class , int . class , WifiP2pManager.ActionListener. class ); setWifiP2pChannels.invoke(manager, channel, 0 , channel_setting, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { createGroupOwner(); Log.d(TAG, "Changed channel (" + channel + ") succeeded" ); // change the channel if it does not work } @Override public void onFailure( int reason) { // TODO enter log here Toast.makeText(_activity, "Failed to change to 2.4GHz" , Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { e.printStackTrace(); } |
This prevented any inconsistencies and gave me a 2GHz response every time, running sudo iw dev wlo1 scan | grep -B 10 -A 20 "SSID: DIRECT" in terminal to see the changes.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效