HarmonyOS Wi-Fi连接用户操作监听全解析:从系统弹框到Promise回调

引言:Wi-Fi连接中的用户确认机制

在HarmonyOS应用开发中,Wi-Fi连接功能是许多应用的基础需求。当应用尝试连接到候选Wi-Fi网络时,系统出于安全考虑会弹出确认对话框,询问用户是否信任并建立连接。传统的wifiManager.connectToCandidateConfig接口虽然能够触发连接流程,但开发者无法直接获取用户对系统弹框的操作结果------用户究竟是点击了"连接"还是"取消"。

这种信息缺失导致应用无法根据用户选择做出相应的逻辑处理,比如记录连接偏好、提供备选网络建议或显示连接状态提示。随着HarmonyOS API 20的发布,全新的wifiManager.connectToCandidateConfigWithUserAction接口彻底解决了这一问题,通过Promise异步回调机制让开发者能够精准捕获用户的操作响应。

一、传统方法的局限性分析

1.1 传统连接流程的问题

在API 20之前,开发者使用connectToCandidateConfig接口连接Wi-Fi网络时,会遇到以下典型问题:

复制代码
// 传统连接方式 - 无法获取用户操作结果
import wifiManager from '@ohos.wifiManager';

async function connectToWiFiTraditional(config: wifiManager.WifiDeviceConfig) {
  try {
    // 调用连接接口
    await wifiManager.connectToCandidateConfig(config);
    
    // 问题:这里无法知道用户是否确认了连接
    // 即使连接失败,也不清楚是用户取消还是其他原因
    
    console.log('连接请求已发送,但用户操作未知');
  } catch (error) {
    console.error('连接过程出错:', error);
    // 无法区分错误类型:用户取消 vs 网络错误
  }
}

1.2 用户操作盲区的影响

由于无法获取用户对系统弹框的操作结果,开发者面临以下挑战:

  1. 状态管理困难:应用无法准确记录连接状态

  2. 用户体验不佳:无法提供针对性的反馈提示

  3. 逻辑处理不完整:连接失败时无法区分原因

  4. 数据统计缺失:无法分析用户连接行为模式

二、新API:connectToCandidateConfigWithUserAction详解

2.1 接口设计理念

API 20引入的connectToCandidateConfigWithUserAction接口采用了现代化的Promise异步回调设计,具有以下核心特点:

  • 用户操作反馈:通过Promise返回用户对系统弹框的操作结果

  • 类型安全:使用枚举类型明确区分不同操作结果

  • 错误处理完善:提供详细的错误码和错误信息

  • 向后兼容:在支持新API的设备上自动使用新机制

2.2 接口定义与参数说明

复制代码
// 用户操作结果枚举
enum UserActionResult {
  CONFIRMED = 0,      // 用户确认连接
  CANCELLED = 1,      // 用户取消连接
  TIMEOUT = 2,        // 操作超时
  ERROR = 3           // 发生错误
}

// 连接结果接口
interface ConnectResult {
  action: UserActionResult;  // 用户操作结果
  networkId?: number;        // 连接成功时的网络ID
  errorCode?: number;        // 错误码(如果有)
  errorMessage?: string;     // 错误信息(如果有)
}

// 新接口定义
interface WifiManager {
  connectToCandidateConfigWithUserAction(
    config: WifiDeviceConfig,
    timeout?: number
  ): Promise<ConnectResult>;
}

三、完整实现方案

3.1 基础连接实现

下面是一个完整的使用新API连接Wi-Fi网络的示例:

复制代码
import wifiManager from '@ohos.wifiManager';
import { BusinessError } from '@ohos.base';

class WiFiConnectionManager {
  private wifiManager: wifiManager.WifiManager;
  
  constructor() {
    // 获取Wi-Fi管理器实例
    this.wifiManager = wifiManager.getWifiManager();
  }
  
  /**
   * 连接到候选Wi-Fi网络(支持用户操作监听)
   * @param config Wi-Fi配置
   * @param timeout 超时时间(毫秒),默认30秒
   * @returns 连接结果
   */
  async connectToCandidateWithUserAction(
    config: wifiManager.WifiDeviceConfig,
    timeout: number = 30000
  ): Promise<ConnectResult> {
    try {
      console.log('开始连接Wi-Fi网络:', config.ssid);
      
      // 调用新API连接网络
      const result = await this.wifiManager.connectToCandidateConfigWithUserAction(config, timeout);
      
      // 根据用户操作结果处理
      switch (result.action) {
        case UserActionResult.CONFIRMED:
          console.log('用户确认连接,网络ID:', result.networkId);
          await this.handleConnectionSuccess(result.networkId!);
          break;
          
        case UserActionResult.CANCELLED:
          console.log('用户取消连接');
          await this.handleUserCancellation();
          break;
          
        case UserActionResult.TIMEOUT:
          console.log('连接操作超时');
          await this.handleConnectionTimeout();
          break;
          
        case UserActionResult.ERROR:
          console.error('连接发生错误:', result.errorCode, result.errorMessage);
          await this.handleConnectionError(result.errorCode!, result.errorMessage);
          break;
      }
      
      return result;
      
    } catch (error) {
      const err = error as BusinessError;
      console.error('连接过程异常:', err.code, err.message);
      throw this.wrapConnectionError(err);
    }
  }
  
  /**
   * 处理连接成功
   */
  private async handleConnectionSuccess(networkId: number): Promise<void> {
    // 更新应用状态
    this.updateConnectionStatus('connected', networkId);
    
    // 保存连接记录
    await this.saveConnectionRecord(networkId);
    
    // 通知UI更新
    this.notifyUI('connection_success', { networkId });
    
    // 可选:验证网络连通性
    await this.verifyNetworkConnectivity();
  }
  
  /**
   * 处理用户取消
   */
  private async handleUserCancellation(): Promise<void> {
    // 更新应用状态
    this.updateConnectionStatus('cancelled');
    
    // 记录用户行为
    this.logUserAction('cancelled_connection');
    
    // 通知UI更新
    this.notifyUI('user_cancelled');
    
    // 可选:提供备选网络建议
    await this.suggestAlternativeNetworks();
  }
  
  /**
   * 处理连接超时
   */
  private async handleConnectionTimeout(): Promise<void> {
    // 更新应用状态
    this.updateConnectionStatus('timeout');
    
    // 通知UI更新
    this.notifyUI('connection_timeout');
    
    // 可选:重试连接
    await this.retryConnection();
  }
  
  /**
   * 处理连接错误
   */
  private async handleConnectionError(errorCode: number, errorMessage?: string): Promise<void> {
    // 根据错误码进行不同处理
    const errorType = this.classifyError(errorCode);
    
    // 更新应用状态
    this.updateConnectionStatus('error', undefined, errorType);
    
    // 记录错误日志
    this.logConnectionError(errorCode, errorMessage);
    
    // 通知UI更新
    this.notifyUI('connection_error', { 
      errorCode, 
      errorMessage,
      errorType 
    });
    
    // 可选:显示错误指导
    await this.showErrorGuidance(errorCode);
  }
  
  /**
   * 创建Wi-Fi配置
   */
  createWifiConfig(
    ssid: string,
    password: string,
    securityType: wifiManager.WifiSecurityType = wifiManager.WifiSecurityType.WPA_PSK
  ): wifiManager.WifiDeviceConfig {
    return {
      ssid: ssid,
      bssid: '', // 可选,指定BSSID
      preSharedKey: password,
      isHiddenSsid: false, // 是否为隐藏网络
      securityType: securityType,
      creatorUid: 0, // 系统自动填充
      disableReason: 0,
      netId: 0, // 连接成功后系统分配
      randomMacType: 0, // MAC地址随机化类型
      randomMacAddr: '', // 随机MAC地址
      ipType: 0, // IP分配类型
      staticIp: {
        ipAddress: 0,
        gateway: 0,
        dnsServers: [],
        domains: []
      }
    };
  }
}

3.2 权限配置详解

要使用Wi-Fi连接功能,必须在应用的配置文件中声明相应权限:

复制代码
// src/main/module.json5
{
  "module": {
    "name": "entry",
    "type": "entry",
    "description": "$string:module_desc",
    "mainElement": "EntryAbility",
    "deviceTypes": [
      "phone",
      "tablet"
    ],
    "deliveryWithInstall": true,
    "installationFree": false,
    "pages": "$profile:main_pages",
    "abilities": [
      {
        "name": "EntryAbility",
        "srcEntry": "./ets/entryability/EntryAbility.ets",
        "description": "$string:EntryAbility_desc",
        "icon": "$media:icon",
        "label": "$string:EntryAbility_label",
        "startWindowIcon": "$media:icon",
        "startWindowBackground": "$color:start_window_background",
        "exported": true,
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ]
      }
    ],
    "requestPermissions": [
      {
        "name": "ohos.permission.SET_WIFI_INFO",
        "reason": "$string:set_wifi_info_reason",
        "usedScene": {
          "abilities": [
            "EntryAbility"
          ],
          "when": "always"
        }
      },
      {
        "name": "ohos.permission.GET_WIFI_INFO",
        "reason": "$string:get_wifi_info_reason",
        "usedScene": {
          "abilities": [
            "EntryAbility"
          ],
          "when": "always"
        }
      },
      {
        "name": "ohos.permission.MANAGE_WIFI_CONNECTION",
        "reason": "$string:manage_wifi_connection_reason",
        "usedScene": {
          "abilities": [
            "EntryAbility"
          ],
          "when": "always"
        }
      }
    ]
  }
}

权限说明表:

权限名称 用途 是否必须
ohos.permission.SET_WIFI_INFO 配置Wi-Fi设备信息,连接网络必需
ohos.permission.GET_WIFI_INFO 获取Wi-Fi信息,扫描网络等 推荐
ohos.permission.MANAGE_WIFI_CONNECTION 管理Wi-Fi连接 推荐

3.3 运行时权限申请

对于敏感权限,还需要在运行时向用户申请:

复制代码
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';

class PermissionManager {
  private context: common.UIAbilityContext;
  
  constructor(context: common.UIAbilityContext) {
    this.context = context;
  }
  
  /**
   * 申请Wi-Fi相关权限
   */
  async requestWifiPermissions(): Promise<boolean> {
    const permissions: Array<string> = [
      'ohos.permission.SET_WIFI_INFO',
      'ohos.permission.GET_WIFI_INFO',
      'ohos.permission.MANAGE_WIFI_CONNECTION'
    ];
    
    try {
      const atManager = abilityAccessCtrl.createAtManager();
      
      // 检查权限状态
      for (const permission of permissions) {
        const grantStatus = await atManager.checkAccessToken(this.context.tokenId, permission);
        
        if (grantStatus !== abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
          console.log(`权限 ${permission} 未授予,开始申请`);
          
          // 申请权限
          const requestResult = await atManager.requestPermissionsFromUser(
            this.context,
            [permission]
          );
          
          if (requestResult.authResults[0] !== 0) {
            console.error(`权限 ${permission} 申请被拒绝`);
            return false;
          }
        }
      }
      
      console.log('所有Wi-Fi权限已授予');
      return true;
      
    } catch (error) {
      const err = error as BusinessError;
      console.error('权限申请失败:', err.code, err.message);
      return false;
    }
  }
  
  /**
   * 检查特定权限
   */
  async checkPermission(permission: string): Promise<boolean> {
    try {
      const atManager = abilityAccessCtrl.createAtManager();
      const grantStatus = await atManager.checkAccessToken(this.context.tokenId, permission);
      return grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED;
    } catch (error) {
      console.error('检查权限失败:', error);
      return false;
    }
  }
}

四、高级功能实现

4.1 连接状态管理

实现一个完整的Wi-Fi连接状态管理器:

复制代码
enum ConnectionState {
  DISCONNECTED = 'disconnected',
  CONNECTING = 'connecting',
  WAITING_USER = 'waiting_user',
  CONNECTED = 'connected',
  FAILED = 'failed',
  CANCELLED = 'cancelled'
}

interface ConnectionEvent {
  state: ConnectionState;
  networkId?: number;
  ssid?: string;
  errorCode?: number;
  errorMessage?: string;
  timestamp: number;
}

class WiFiConnectionStateManager {
  private currentState: ConnectionState = ConnectionState.DISCONNECTED;
  private connectionHistory: ConnectionEvent[] = [];
  private stateListeners: Array<(event: ConnectionEvent) => void> = [];
  
  /**
   * 更新连接状态
   */
  updateState(
    state: ConnectionState,
    networkId?: number,
    ssid?: string,
    errorCode?: number,
    errorMessage?: string
  ): void {
    const previousState = this.currentState;
    this.currentState = state;
    
    const event: ConnectionEvent = {
      state,
      networkId,
      ssid,
      errorCode,
      errorMessage,
      timestamp: Date.now()
    };
    
    // 记录历史
    this.connectionHistory.push(event);
    
    // 限制历史记录数量
    if (this.connectionHistory.length > 100) {
      this.connectionHistory = this.connectionHistory.slice(-100);
    }
    
    console.log(`连接状态变更: ${previousState} -> ${state}`, {
      networkId,
      ssid,
      errorCode,
      errorMessage
    });
    
    // 通知监听器
    this.notifyStateChange(event);
  }
  
  /**
   * 获取当前状态
   */
  getCurrentState(): ConnectionState {
    return this.currentState;
  }
  
  /**
   * 获取连接历史
   */
  getConnectionHistory(): ConnectionEvent[] {
    return [...this.connectionHistory];
  }
  
  /**
   * 添加状态监听器
   */
  addStateListener(listener: (event: ConnectionEvent) => void): void {
    this.stateListeners.push(listener);
  }
  
  /**
   * 移除状态监听器
   */
  removeStateListener(listener: (event: ConnectionEvent) => void): void {
    const index = this.stateListeners.indexOf(listener);
    if (index > -1) {
      this.stateListeners.splice(index, 1);
    }
  }
  
  /**
   * 通知状态变化
   */
  private notifyStateChange(event: ConnectionEvent): void {
    this.stateListeners.forEach(listener => {
      try {
        listener(event);
      } catch (error) {
        console.error('状态监听器执行错误:', error);
      }
    });
  }
  
  /**
   * 获取最近的成功连接
   */
  getLastSuccessfulConnection(): ConnectionEvent | undefined {
    return this.connectionHistory
      .reverse()
      .find(event => event.state === ConnectionState.CONNECTED);
  }
  
  /**
   * 统计连接成功率
   */
  getConnectionSuccessRate(): number {
    const totalAttempts = this.connectionHistory.filter(
      event => event.state === ConnectionState.CONNECTED || 
               event.state === ConnectionState.FAILED ||
               event.state === ConnectionState.CANCELLED
    ).length;
    
    const successfulAttempts = this.connectionHistory.filter(
      event => event.state === ConnectionState.CONNECTED
    ).length;
    
    return totalAttempts > 0 ? (successfulAttempts / totalAttempts) * 100 : 0;
  }
}

4.2 用户操作分析

收集和分析用户操作数据,优化连接体验:

复制代码
interface UserActionAnalytics {
  totalConnections: number;
  confirmedConnections: number;
  cancelledConnections: number;
  averageDecisionTime: number;
  cancellationReasons: Map<string, number>;
  connectionSuccessRate: number;
}

class UserActionAnalyzer {
  private analytics: UserActionAnalytics = {
    totalConnections: 0,
    confirmedConnections: 0,
    cancelledConnections: 0,
    averageDecisionTime: 0,
    cancellationReasons: new Map(),
    connectionSuccessRate: 0
  };
  
  private decisionStartTimes: Map<string, number> = new Map();
  
  /**
   * 记录用户决策开始
   */
  recordDecisionStart(ssid: string): void {
    this.decisionStartTimes.set(ssid, Date.now());
  }
  
  /**
   * 记录用户操作结果
   */
  recordUserAction(
    ssid: string,
    action: UserActionResult,
    reason?: string
  ): void {
    const startTime = this.decisionStartTimes.get(ssid);
    const decisionTime = startTime ? Date.now() - startTime : 0;
    
    this.analytics.totalConnections++;
    
    switch (action) {
      case UserActionResult.CONFIRMED:
        this.analytics.confirmedConnections++;
        break;
        
      case UserActionResult.CANCELLED:
        this.analytics.cancelledConnections++;
        if (reason) {
          const count = this.analytics.cancellationReasons.get(reason) || 0;
          this.analytics.cancellationReasons.set(reason, count + 1);
        }
        break;
    }
    
    // 更新平均决策时间
    this.updateAverageDecisionTime(decisionTime);
    
    // 更新连接成功率
    this.updateSuccessRate();
    
    // 清理记录
    this.decisionStartTimes.delete(ssid);
    
    console.log('用户操作记录:', {
      ssid,
      action,
      decisionTime,
      reason
    });
  }
  
  /**
   * 更新平均决策时间
   */
  private updateAverageDecisionTime(newTime: number): void {
    const totalConnections = this.analytics.totalConnections;
    const currentAverage = this.analytics.averageDecisionTime;
    
    this.analytics.averageDecisionTime = 
      (currentAverage * (totalConnections - 1) + newTime) / totalConnections;
  }
  
  /**
   * 更新连接成功率
   */
  private updateSuccessRate(): void {
    const total = this.analytics.totalConnections;
    const successful = this.analytics.confirmedConnections;
    
    this.analytics.connectionSuccessRate = total > 0 ? 
      (successful / total) * 100 : 0;
  }
  
  /**
   * 获取分析报告
   */
  getAnalyticsReport(): UserActionAnalytics {
    return {
      ...this.analytics,
      cancellationReasons: new Map(this.analytics.cancellationReasons)
    };
  }
  
  /**
   * 获取最常见的取消原因
   */
  getTopCancellationReasons(limit: number = 3): Array<[string, number]> {
    return Array.from(this.analytics.cancellationReasons.entries())
      .sort((a, b) => b[1] - a[1])
      .slice(0, limit);
  }
  
  /**
   * 重置分析数据
   */
  resetAnalytics(): void {
    this.analytics = {
      totalConnections: 0,
      confirmedConnections: 0,
      cancelledConnections: 0,
      averageDecisionTime: 0,
      cancellationReasons: new Map(),
      connectionSuccessRate: 0
    };
    this.decisionStartTimes.clear();
  }
}

五、错误处理与最佳实践

5.1 完整的错误处理框架

复制代码
enum WiFiErrorCode {
  // 系统错误码
  PERMISSION_DENIED = 201,
  NETWORK_UNAVAILABLE = 202,
  CONFIG_INVALID = 203,
  CONNECTION_TIMEOUT = 204,
  AUTHENTICATION_FAILED = 205,
  
  // 业务错误码
  USER_CANCELLED = 1001,
  NETWORK_NOT_FOUND = 1002,
  PASSWORD_INCORRECT = 1003,
  UNSUPPORTED_SECURITY = 1004
}

class WiFiError extends Error {
  constructor(
    public code: WiFiErrorCode,
    message: string,
    public originalError?: BusinessError
  ) {
    super(message);
    this.name = 'WiFiError';
  }
  
  /**
   * 获取用户友好的错误信息
   */
  getUserFriendlyMessage(): string {
    switch (this.code) {
      case WiFiErrorCode.PERMISSION_DENIED:
        return '缺少Wi-Fi连接权限,请前往设置中开启';
        
      case WiFiErrorCode.USER_CANCELLED:
        return '您已取消Wi-Fi连接';
        
      case WiFiErrorCode.AUTHENTICATION_FAILED:
        return 'Wi-Fi密码错误,请重新输入';
        
      case WiFiErrorCode.CONNECTION_TIMEOUT:
        return '连接超时,请检查网络信号';
        
      case WiFiErrorCode.NETWORK_NOT_FOUND:
        return '未找到指定的Wi-Fi网络';
        
      default:
        return '网络连接失败,请稍后重试';
    }
  }
  
  /**
   * 判断是否可重试的错误
   */
  isRetryable(): boolean {
    const retryableCodes = [
      WiFiErrorCode.CONNECTION_TIMEOUT,
      WiFiErrorCode.NETWORK_UNAVAILABLE
    ];
    
    return retryableCodes.includes(this.code);
  }
}

class WiFiErrorHandler {
  /**
   * 包装系统错误
   */
  wrapConnectionError(error: BusinessError): WiFiError {
    const errorCode = error.code;
    
    switch (errorCode) {
      case 201:
        return new WiFiError(
          WiFiErrorCode.PERMISSION_DENIED,
          'Wi-Fi权限被拒绝',
          error
        );
        
      case 202:
        return new WiFiError(
          WiFiErrorCode.NETWORK_UNAVAILABLE,
          '网络不可用',
          error
        );
        
      case 205:
        return new WiFiError(
          WiFiErrorCode.AUTHENTICATION_FAILED,
          '身份验证失败',
          error
        );
        
      default:
        return new WiFiError(
          WiFiErrorCode.CONNECTION_TIMEOUT,
          `连接失败: ${error.message}`,
          error
        );
    }
  }
  
  /**
   * 处理连接错误
   */
  async handleConnectionError(
    error: WiFiError,
    config: wifiManager.WifiDeviceConfig,
    retryCount: number = 0
  ): Promise<{ shouldRetry: boolean; retryDelay?: number }> {
    console.error('Wi-Fi连接错误处理:', {
      code: error.code,
      message: error.message,
      ssid: config.ssid,
      retryCount
    });
    
    // 记录错误日志
    await this.logError(error, config);
    
    // 判断是否应该重试
    if (error.isRetryable() && retryCount < 3) {
      const retryDelay = this.calculateRetryDelay(retryCount);
      
      console.log(`将在 ${retryDelay}ms 后重试连接`);
      return {
        shouldRetry: true,
        retryDelay
      };
    }
    
    // 不可重试或达到重试上限
    return {
      shouldRetry: false
    };
  }
  
  /**
   * 计算重试延迟
   */
  private calculateRetryDelay(retryCount: number): number {
    // 指数退避算法
    const baseDelay = 1000; // 1秒
    const maxDelay = 10000; // 10秒
    const delay = Math.min(baseDelay * Math.pow(2, retryCount), maxDelay);
    
    // 添加随机抖动
    const jitter = delay * 0.1 * (Math.random() * 2 - 1);
    
    return Math.round(delay + jitter);
  }
  
  /**
   * 记录错误日志
   */
  private async logError(error: WiFiError, config: wifiManager.WifiDeviceConfig): Promise<void> {
    const errorLog = {
      timestamp: new Date().toISOString(),
      errorCode: error.code,
      errorMessage: error.message,
      ssid: config.ssid,
      securityType: config.securityType,
      originalError: error.originalError
    };
    
    // 实际项目中可以保存到本地或上报到服务器
    console.log('错误日志:', errorLog);
  }
}

5.2 最佳实践建议

  1. 用户体验优化

    复制代码
    class WiFiUXOptimizer {
      /**
       * 在用户决策前提供网络信息
       */
      async showNetworkInfoBeforeConnection(config: wifiManager.WifiDeviceConfig): Promise<void> {
        // 显示网络信号强度
        const signalStrength = await this.getSignalStrength(config.ssid);
    
        // 显示网络安全信息
        const securityInfo = this.getSecurityInfo(config.securityType);
    
        // 显示网络速度预估
        const speedEstimate = await this.estimateNetworkSpeed(config.ssid);
    
        // 在UI中显示这些信息,帮助用户做出决策
        this.displayNetworkPreview({
          ssid: config.ssid,
          signalStrength,
          securityInfo,
          speedEstimate
        });
      }
    
      /**
       * 提供智能连接建议
       */
      async getConnectionSuggestion(config: wifiManager.WifiDeviceConfig): Promise<string> {
        const history = await this.getConnectionHistory(config.ssid);
    
        if (history.successRate > 0.8) {
          return '此网络连接稳定,推荐连接';
        } else if (history.successRate > 0.5) {
          return '此网络连接一般,可以尝试';
        } else {
          return '此网络连接较差,建议选择其他网络';
        }
      }
    }
  2. 性能优化

    复制代码
    class WiFiPerformanceOptimizer {
      private connectionCache: Map<string, ConnectionCacheEntry> = new Map();
    
      /**
       * 缓存连接配置
       */
      cacheConnectionConfig(config: wifiManager.WifiDeviceConfig): void {
        const cacheKey = this.generateCacheKey(config);
        const cacheEntry: ConnectionCacheEntry = {
          config,
          timestamp: Date.now(),
          successCount: 0,
          failureCount: 0
        };
    
        this.connectionCache.set(cacheKey, cacheEntry);
    
        // 限制缓存大小
        if (this.connectionCache.size > 50) {
          this.cleanupOldCacheEntries();
        }
      }
    
      /**
       * 获取最优连接配置
       */
      getOptimizedConfig(baseConfig: wifiManager.WifiDeviceConfig): wifiManager.WifiDeviceConfig {
        const cacheKey = this.generateCacheKey(baseConfig);
        const cachedEntry = this.connectionCache.get(cacheKey);
    
        if (cachedEntry && cachedEntry.successCount > 0) {
          // 使用缓存的成功配置
          return {
            ...baseConfig,
            // 可以基于历史记录调整参数
            randomMacType: cachedEntry.successCount > 5 ? 1 : 0
          };
        }
    
        return baseConfig;
      }
    }

六、兼容性处理

6.1 API版本适配

复制代码
class WiFiConnectionAdapter {
  private apiVersion: number;
  
  constructor() {
    // 获取系统API版本
    this.apiVersion = this.getSystemApiVersion();
  }
  
  /**
   * 自适应连接方法
   */
  async connectToWiFi(
    config: wifiManager.WifiDeviceConfig
  ): Promise<ConnectResult | void> {
    if (this.supportsUserActionAPI()) {
      // 使用新API(API 20+)
      return await this.connectWithUserAction(config);
    } else {
      // 使用旧API(API 20以下)
      return await this.connectLegacy(config);
    }
  }
  
  /**
   * 检查是否支持用户操作API
   */
  private supportsUserActionAPI(): boolean {
    // API 20及以上支持connectToCandidateConfigWithUserAction
    return this.apiVersion >= 20;
  }
  
  /**
   * 使用新API连接
   */
  private async connectWithUserAction(
    config: wifiManager.WifiDeviceConfig
  ): Promise<ConnectResult> {
    const wifiManager = wifiManager.getWifiManager();
    return await wifiManager.connectToCandidateConfigWithUserAction(config);
  }
  
  /**
   * 使用旧API连接
   */
  private async connectLegacy(
    config: wifiManager.WifiDeviceConfig
  ): Promise<void> {
    const wifiManager = wifiManager.getWifiManager();
    
    try {
      await wifiManager.connectToCandidateConfig(config);
      
      // 旧API无法获取用户操作结果
      // 可以通过其他方式推断连接状态
      await this.monitorConnectionStatus(config.ssid);
      
    } catch (error) {
      console.error('旧API连接失败:', error);
      throw error;
    }
  }
  
  /**
   * 监控连接状态(旧API备用方案)
   */
  private async monitorConnectionStatus(ssid: string): Promise<void> {
    // 通过轮询或事件监听方式检查连接状态
    return new Promise((resolve, reject) => {
      let checkCount = 0;
      const maxChecks = 10;
      const checkInterval = 1000; // 1秒
      
      const checkConnection = () => {
        checkCount++;
        
        if (checkCount > maxChecks) {
          clearInterval(intervalId);
          reject(new Error('连接状态检查超时'));
          return;
        }
        
        this.checkIfConnected(ssid).then(isConnected => {
          if (isConnected) {
            clearInterval(intervalId);
            resolve();
          }
        });
      };
      
      const intervalId = setInterval(checkConnection, checkInterval);
    });
  }
}

七、总结与展望

7.1 技术总结

通过wifiManager.connectToCandidateConfigWithUserAction接口,HarmonyOS为开发者提供了完整的Wi-Fi连接用户操作监听能力。本文详细介绍了:

  1. 传统方法的局限性:无法获取用户操作结果,导致状态管理困难

  2. 新API的优势:Promise异步回调机制,精准捕获用户操作

  3. 完整实现方案:从权限配置到错误处理的完整代码示例

  4. 高级功能:状态管理、用户行为分析、性能优化

  5. 兼容性处理:多版本API适配策略

7.2 最佳实践要点

  1. 权限管理:正确声明和申请Wi-Fi相关权限

  2. 错误处理:完善的错误分类和处理机制

  3. 用户体验:提供清晰的连接状态反馈

  4. 性能优化:合理使用缓存和连接策略

  5. 数据分析:收集用户操作数据优化产品

7.3 未来展望

随着HarmonyOS的不断发展,Wi-Fi连接能力将进一步完善:

  1. 智能连接预测:基于用户习惯自动推荐网络

  2. 无缝切换:在多设备间智能切换最佳网络

  3. 安全增强:更细粒度的网络安全控制

  4. 性能优化:更快速的连接建立和恢复

通过掌握本文介绍的技术方案,开发者可以构建出更加稳定、智能、用户友好的Wi-Fi连接功能,为用户提供卓越的网络连接体验。HarmonyOS的持续演进将为开发者带来更多创新可能,推动整个生态系统向更高水平发展。

相关推荐
Lanren的编程日记4 小时前
Flutter 鸿蒙应用数据版本管理实战:版本记录+版本回退+版本对比,实现全链路数据版本控制
flutter·华为·harmonyos
我是大聪明.4 小时前
DeepSeek V4 Pro + 华为昇腾910:国产大模型落地的性能实测与深度解析
人工智能·华为
木斯佳5 小时前
HarmonyOS 本地存储实战:记账本案例改造实现日历联动
华为·harmonyos
李游Leo6 小时前
别让一张 12MB 的照片拖垮页面:ImageSource / PixelMap / ImagePacker 的工程化处理链路
harmonyos
nashane6 小时前
HarmonyOS 6学习:画中画(PiP)状态同步与场景化实战指南
学习·pip·harmonyos·harmonyos 5
@不误正业7 小时前
鸿蒙小艺智能体开放平台实战-接入系统级AI-Agent能力
人工智能·华为·harmonyos
IntMainJhy10 小时前
「Flutter三方库sqflite的鸿蒙化适配与实战指南:从入门到踩坑的本地数据库开发全记录」
数据库·flutter·华为·信息可视化·数据库开发·harmonyos
前端技术12 小时前
HarmonyOS开发:鸿蒙应用开发发展史
华为·harmonyos
忡黑梨12 小时前
eNSP_路由策略
运维·服务器·网络·华为·智能路由器·负载均衡