codecamp

鸿蒙OS WLAN消息通知

场景介绍

WLAN 消息通知(Notification)是 HarmonyOS 内部或者与应用之间跨进程通讯的机制,注册者在注册消息通知后,一旦符合条件的消息被发出,注册者即可接收到该消息并获取消息中附带的信息。

接口说明

描述 通知名 附加参数
WLAN状态 usual.event.wifi.POWER_STATE active_state
WLAN扫描 usual.event.wifi.SCAN_FINISHED scan_state
WLAN RSSI变化 usual.event.wifi.RSSI_VALUE rssi_value
WLAN连接状态 usual.event.wifi.CONN_STATE conn_state
Hotspot状态 usual.event.wifi.HOTSPOT_STATE hotspot_active_state
Hotspot连接状态 usual.event.wifi.WIFI_HS_STA_JOIN usual.event.wifi.WIFI_HS_STA_LEAVE -
P2P状态 usual.event.wifi.p2p.STATE_CHANGE p2p_state
P2P连接状态 usual.event.wifi.p2p.CONN_STATE_CHANGE linked_infonet_infogroup_info
P2P设备列表变化 usual.event.wifi.p2p.PEERS_STATE_CHANGE peers_list
P2P搜索状态变化 usual.event.wifi.p2p.PEER_DISCOVERY_STATE_CHANGE peers_discovery
P2P当前设备变化 usual.event.wifi.p2p.CURRENT_DEVICE_CHANGE p2p_device
P2P群组信息变化 usual.event.wifi.p2p.GROUP_STATE_CHANGED -

开发步骤

  1. 构建消息通知接收者 WifiEventSubscriber。

  1. 注册 WLAN 变化消息。

  1. WifiEventSubscriber 接收并处理 WLAN 广播消息。

   // 构建消息接收者/注册者
   class WifiEventSubscriber extends CommonEventSubscriber {
       WifiEventSubscriber (CommonEventSubscribeInfo info) {
           super(info);
       }

    
       @Override
       public void onReceiveEvent(CommonEventData commonEventData) {
           if (WifiEvents.EVENT_ACTIVE_STATE.equals(commonEventData.getIntent().getAction())) {
               // 获取附带参数
               IntentParams params = commonEventData.getIntent().getParams();
               if (params == null) {
                   return;
               }
               int wifiState= (int) params.getParam(WifiEvents.PARAM_ACTIVE_STATE);

               
               if (wifiState== WifiEvents.STATE_ACTIVE) { // 处理WLAN被打开消息
                   HiLog.info(LABEL, false, "Receive WifiEvents.STATE_ACTIVE %{public}d", wifiState);
               } else if (wifiState == WifiEvents.STATE_INACTIVE) { // 处理WLAN被关闭消息  
                   HiLog.info(LABEL, false, "Receive WifiEvents.STATE_INACTIVE %{public}d", wifiState);
               } else { // 处理WLAN异常状态
                   HiLog.info(LABEL, false, "Unknown wifi state");
               }
           }
       }
   }

    
   // 注册消息
   MatchingSkills match = new MatchingSkills();
   // 增加获取WLAN状态变化消息
   filter.addEvent(WifiEvents.EVENT_ACTIVE_STATE);
   CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(match);
   subscribeInfo.setPriority(100); 
   WifiEventSubscriber subscriber = new WifiEventSubscriber(subscribeInfo);

    
   try {
       CommonEventManager.subscribeCommonEvent(subscriber);
   } catch (RemoteException e) {
       HiLog.warn(LABEL, false, "subscribe in wifi events failed!");
   }
鸿蒙OS P2P功能章
鸿蒙OS WLAN概述
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

鸿蒙OS 开发

鸿蒙OS 术语

鸿蒙OS Java API参考

鸿蒙OS ohos.aafwk.ability

鸿蒙OS ohos.aafwk.abilityjet.activedata

鸿蒙OS ohos.aafwk.content

鸿蒙OS java.lang

鸿蒙OS java.Util

鸿蒙OS java.Util class

鸿蒙OS ohos.data.dataability

鸿蒙OS ohos.data.dataability class

鸿蒙OS ohos.agp.components

鸿蒙OS ohos.agp.components interface

鸿蒙OS ohos.agp.components class

鸿蒙OS ohos.global.configuration

鸿蒙OS java.io

鸿蒙OS ohos.data.resultset

鸿蒙OS ohos.data.resultset interface

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }