代码改变世界

在 Android 11 上构建 WiFi 热点并发协助(同时开启 STA + AP 模式)

2025-10-16 15:11  tlnshuju  阅读(8)  评论(0)    收藏  举报

在 Android 11 上设置 WiFi 热点分享,有以下几种方法:

1. 通过系统设置界面(图形界面)

基本步骤:

  1. 打开设置 → 网络和互联网 → 热点和网络共享

  2. Wi-Fi 热点 → 开启开关

  3. 配置热点设置

    • 网络名称(SSID)

    • 安全性(推荐 WPA2-Personal)

    • 密码(至少8位字符)

    • 频段(2.4GHz 或 5GHz)

高级选项:

bash

# 查看热点状态
adb shell dumpsys wifi | grep -A 20 -B 5 "Hotspot"

# 检查热点配置
adb shell cat /data/misc/wifi/hostapd.conf

2. 通过 ADB 命令开启热点

启用热点服务:

bash

# 启用 Wi-Fi 热点
adb shell svc wifi enable
adb shell cmd wifi set-hotspot-enabled enable

# 配置热点参数
adb shell cmd wifi set-hotspot-ssid MyHotspot
adb shell cmd wifi set-hotspot-password Password123
adb shell cmd wifi set-hotspot-security 4  # WPA2_PSK

# 启动热点
adb shell cmd wifi start-soft-ap

检查热点状态:

bash

# 查看热点接口
adb shell ip addr show ap0

# 检查热点服务状态
adb shell dumpsys wifi | grep -i "softap"
adb shell dumpsys connectivity | grep -i "tethering"

3. 通过代码实现(Android 应用)

添加权限:

xml







Java 代码示例:

java

public class WifiHotspotManager {
    private WifiManager wifiManager;
    private Context context;
    
    public WifiHotspotManager(Context context) {
        this.context = context;
        this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }
    
    // 启用热点
    public void enableHotspot(String ssid, String password) {
        if (wifiManager == null) return;
        
        // 使用 startLocalOnlyHotspot 方法(推荐)
        wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
            @Override
            public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                super.onStarted(reservation);
                // 热点启动成功
                Log.d("Hotspot", "Hotspot started successfully");
            }
            
            @Override
            public void onFailed(int reason) {
                super.onFailed(reason);
                Log.e("Hotspot", "Hotspot failed: " + reason);
            }
        }, new Handler());
    }
    
    // 传统热点配置(需要系统权限)
    private void enableLegacyHotspot() {
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = ssid;
        wifiConfig.preSharedKey = password;
        wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        
        try {
            Method method = wifiManager.getClass().getMethod("setWifiApEnabled", 
                WifiConfiguration.class, boolean.class);
            method.invoke(wifiManager, wifiConfig, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    // 检查热点状态
    public boolean isHotspotEnabled() {
        try {
            Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
            return (boolean) method.invoke(wifiManager);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

4. 系统级配置(需要 root 权限)

修改 hostapd 配置:

bash

# 查看当前热点配置
adb shell cat /data/misc/wifi/hostapd.conf

# 手动创建热点配置
adb shell echo "interface=wlan0
driver=nl80211
ssid=MyHotspot
hw_mode=g
channel=6
wpa=2
wpa_passphrase=MyPassword123
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP" > /data/misc/wifi/hostapd.conf

# 重启 hostapd 服务
adb shell setprop ctl.restart hostapd

5. 常见问题解决

A. 热点无法启动

bash

# 检查 Wi-Fi 状态
adb shell dumpsys wifi | grep "Wi-Fi is"

# 重启 Wi-Fi 服务
adb shell svc wifi disable
adb shell svc wifi enable

# 清除 Wi-Fi 配置
adb shell cmd wifi network-suggestions-set-user-approved yes

B. 权限问题

bash

# 授予应用热点权限
adb shell pm grant  android.permission.NETWORK_SETTINGS

C. 检查系统限制

bash

# 查看热点并发支持
adb shell getprop | grep wifi.concurrent.interface
adb shell getprop | grep ro.wifi.ap.interface

6. Android 11 特定注意事项

  1. 位置权限要求:Android 11 需要位置权限才能扫描和连接 Wi-Fi

  2. 后台限制:后台应用的热点功能可能受到限制

  3. 隐私保护:热点 SSID 和连接信息访问受限

检查 Android 11 特定设置:

bash

# 检查位置服务是否开启
adb shell settings get secure location_providers_allowed

# 检查 Wi-Fi 扫描限制
adb shell settings get global wifi_scan_throttle_enabled

这些方法应该能帮助你在 Android 11 上成功设置和使用 WiFi 热点分享功能。

Android11 WiFi 热点并发支持

在 Android 11 上实现 WiFi 热点并发支持(同时开启 STA + AP 模式)需要硬件、驱动和系统的多重支持。以下是详细的配置和检查方法:

1. 检查硬件和驱动支持

检查并发能力:

bash

# 检查 WiFi 芯片是否支持并发模式
adb shell dumpsys wifi | grep -i "concurrent"

# 查看支持的接口组合
adb shell dumpsys wifi | grep -A 10 -B 5 "interface"

# 检查内核驱动支持
adb shell cat /proc/net/wireless
adb shell dmesg | grep -i "wlan"

检查接口状态:

bash

# 查看所有网络接口
adb shell ip link show
adb shell ls /sys/class/net/

# 检查虚拟接口支持
adb shell iw list | grep -A 20 "Supported interface modes"

2. 系统属性配置

查看当前配置:

bash

# 检查并发相关属性
adb shell getprop | grep wifi.concurrent
adb shell getprop | grep wifi.interface
adb shell getprop | grep persist.vendor.wifi

# 常见并发属性
adb shell getprop persist.vendor.wifi.multi.interfaces
adb shell getprop wifi.concurrent.interface
adb shell getprop ro.vendor.wifi.sap.interface

设置并发支持属性:

bash

# 启用多接口支持
adb shell setprop persist.vendor.wifi.multi.interfaces "wlan0,wlan1"
adb shell setprop wifi.concurrent.interface "wlan1"

# 配置热点接口
adb shell setprop ro.vendor.wifi.sap.interface "wlan1"
adb shell setprop persist.vendor.wifi.ap.interface "wlan1"

# 设置并发模式
adb shell setprop persist.vendor.wifi.concurrent.enable 1
adb shell setprop wifi.direct.interface "p2p0"

3. WiFi 配置文件修改

修改 WiFi HAL 配置:

bash

# 查看 WiFi 配置文件
adb shell ls /vendor/etc/wifi/

# 常见的配置文件
adb shell cat /vendor/etc/wifi/wifi_hal.conf
adb shell cat /vendor/etc/wifi/wifi_concurrency_cfg.txt
adb shell cat /vendor/etc/wifi/p2p_supplicant.conf

配置示例:

ini

# /vendor/etc/wifi/wifi_concurrency_cfg.txt
# 启用 STA + AP 并发
chip_capability=sta_ap_concurrency
sta_ap_concurrency=1

# 或者更详细的配置
interface_combination=1
interface_combination=STA,AP
max_supported_concurrent_operations=2

hostapd 配置:

bash

# 创建热点配置
adb shell echo "interface=wlan1
driver=nl80211
ssid=MyAndroidAP
hw_mode=g
channel=11
wpa=2
wpa_passphrase=MyPassword123
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ignore_broadcast_ssid=0" > /data/misc/wifi/hostapd.conf

4. 代码实现并发热点

Java 代码示例:

java

public class ConcurrentHotspotManager {
    private WifiManager wifiManager;
    private Context context;
    
    public boolean isConcurrencySupported() {
        // 检查设备是否支持并发
        try {
            Method method = wifiManager.getClass().getMethod("isStaApConcurrencySupported");
            return (boolean) method.invoke(wifiManager);
        } catch (Exception e) {
            return false;
        }
    }
    
    public void enableConcurrentMode() {
        if (!isConcurrencySupported()) {
            Log.e("Hotspot", "Concurrent mode not supported");
            return;
        }
        
        // 先连接 WiFi(STA 模式)
        connectToWifi();
        
        // 然后开启热点(AP 模式)
        enableHotspot();
    }
    
    private void connectToWifi() {
        String networkSSID = "MyWiFi";
        String networkPass = "WiFiPassword";
        
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = String.format("\"%s\"", networkSSID);
        wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);
        
        int netId = wifiManager.addNetwork(wifiConfig);
        wifiManager.disconnect();
        wifiManager.enableNetwork(netId, true);
        wifiManager.reconnect();
    }
    
    private void enableHotspot() {
        // 使用 LocalOnlyHotspot(Android 8.0+)
        wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
            @Override
            public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                Log.d("Hotspot", "Concurrent hotspot started");
                // 保存 reservation 以防止热点被关闭
            }
            
            @Override
            public void onFailed(int reason) {
                Log.e("Hotspot", "Hotspot failed: " + reason);
            }
        }, null);
    }
}

5. 系统服务配置

修改系统服务:

xml




    android.hardware.wifi
    hwbinder
    1.0
    
        IWifi
        default
    

创建系统配置文件:

bash

# 在设备上创建配置脚本
echo '#!/system/bin/sh
# 启用 WiFi 并发模式
setprop wifi.interface wlan0
setprop wlan.driver.interface wlan0
setprop wifi.ap.interface wlan1
setprop persist.vendor.wifi.concurrent.enable 1
setprop persist.vendor.wifi.multi.interfaces "wlan0,wlan1"

# 重启 WiFi 服务
stop wifi
start wifi' > /vendor/bin/configure_wifi_concurrency.sh

6. 调试和验证

检查并发状态:

bash

# 验证接口状态
adb shell iw dev
adb shell ifconfig wlan0
adb shell ifconfig wlan1

# 检查热点和 STA 状态
adb shell dumpsys wifi | grep -E "(current|interface|mode)"
adb shell dumpsys connectivity | grep -i tethering

# 查看详细的 WiFi 状态
adb shell cmd wifi get-softap-supported-features
adb shell cmd wifi get-supported-features

测试并发功能:

bash

# 同时开启 STA 和 AP
adb shell svc wifi enable
adb shell cmd wifi start-soft-ap

# 检查是否同时工作
adb shell ping -I wlan0 8.8.8.8
adb shell ping -I wlan1 8.8.8.8

7. 常见问题解决

A. 并发模式不支持

bash

# 检查硬件能力
adb shell iw list | grep -i "combination"
adb shell dmesg | grep -i "concurrent"

# 回退到单接口模式
adb shell setprop persist.vendor.wifi.concurrent.enable 0

B. 接口创建失败

bash

# 检查驱动日志
adb shell dmesg | grep -i "wlan"
adb shell logcat | grep -i "hostapd"

# 重新加载驱动
adb shell rmmod wlan
adb shell insmod /vendor/lib/modules/wlan.ko

C. 资源冲突

bash

# 检查频道冲突
adb shell iwlist wlan0 channel
adb shell iwlist wlan1 channel

# 设置不同频道
adb shell iwconfig wlan0 channel 6
adb shell iwconfig wlan1 channel 11

8. Android 11 特定要求

权限配置:

xml




SELinux 策略:

bash

# 检查 SELinux 权限
adb shell getenforce
adb shell dmesg | grep -i "avc:.*wifi"

# 临时禁用 SELinux 进行测试
adb shell setenforce 0

通过以上配置,你应该能够在支持的 Android 11 设备上实现 WiFi 热点并发功能。注意这需要硬件和驱动的支持,不是所有设备都具备此能力。