背景

  • Platform: RK3399
  • OS: Android7.1.2
  • Kernel: v4.4.103

在项目中,因使用的WIFI模块的性能限制,在作为AP热点时候不能带很多台station,所以需要限制AP最大连接数。

解决

热点配置文件/data/misc/wifi/hostapd.conf里面带上max_num_sta参数限制最大连接数,但Android原生代码不包含此参数, 该文件最终是由/system/netd/server/SoftapController.cpp生成。

  • Android7.1路径(Android O会有所不同):
    /system/netd/server/SoftapController.cpp

添加头文件:

#include <cutils/properties.h> //环境变量操作头文件

添加处理代码:

//start add for sta limit 20200817,
    int max_num_sta = 0; //0为不限制
    char buf[PROPERTY_VALUE_MAX] = {'\0',};
    if (property_get("sys.softap.limit", buf, "0")) {
        max_num_sta = atoi(buf);
        if(max_num_sta != 0) {
            if(max_num_sta < 0 || max_num_sta > 255)
                max_num_sta = 0;
        }
    }
    
	std::string wbuf;
	if(max_num_sta) {
		std::string wbuf_tmp(StringPrintf("interface=%s\n"
				"driver=nl80211\n"
				"ctrl_interface=/data/misc/wifi/hostapd\n"
				"ssid=%s\n"
				"channel=%d\n"
				"ieee80211n=1\n"
				"hw_mode=%c\n"
				"ignore_broadcast_ssid=%d\n"
				"wowlan_triggers=any\n"
				"max_num_sta=%d\n",
				argv[2], argv[3], channel, (channel <= 14) ? 'g' : 'a', hidden, max_num_sta));
		wbuf = wbuf_tmp;
	}
	else {
		std::string wbuf_tmp(StringPrintf("interface=%s\n"
				"driver=nl80211\n"
				"ctrl_interface=/data/misc/wifi/hostapd\n"
				"ssid=%s\n"
				"channel=%d\n"
				"ieee80211n=1\n"
				"hw_mode=%c\n"
				"ignore_broadcast_ssid=%d\n"
				"wowlan_triggers=any\n",
				argv[2], argv[3], channel, (channel <= 14) ? 'g' : 'a', hidden));
		wbuf = wbuf_tmp;
	}
	//end add for sta limit 20200817,zdd

在系统环境变量中(system.prop)添加:

sys.softap.limit=3

参考

https://blog.csdn.net/laoguanhua/article/details/82810386